0
0
mirror of https://github.com/vx3r/wg-gen-web.git synced 2024-11-04 17:31:17 +00:00
wg-gen-web/api/v1/server/server.go

95 lines
2.2 KiB
Go
Raw Normal View History

2020-04-28 11:11:49 +00:00
package server
import (
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/auth"
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/core"
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/model"
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/version"
"golang.org/x/oauth2"
"net/http"
)
// ApplyRoutes applies router to gin Router
func ApplyRoutes(r *gin.RouterGroup) {
g := r.Group("/server")
{
g.GET("", readServer)
g.PATCH("", updateServer)
g.GET("/config", configServer)
g.GET("/version", versionStr)
}
}
func readServer(c *gin.Context) {
client, err := core.ReadServer()
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to read client")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.JSON(http.StatusOK, client)
}
func updateServer(c *gin.Context) {
var data model.Server
if err := c.ShouldBindJSON(&data); err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to bind")
c.AbortWithStatus(http.StatusUnprocessableEntity)
return
}
// get update user from token and add to server infos
oauth2Token := c.MustGet("oauth2Token").(*oauth2.Token)
oauth2Client := c.MustGet("oauth2Client").(auth.Auth)
user, err := oauth2Client.UserInfo(oauth2Token)
if err != nil {
log.WithFields(log.Fields{
"oauth2Token": oauth2Token,
"err": err,
}).Error("failed to get user with oauth token")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
data.UpdatedBy = user.Name
server, err := core.UpdateServer(&data)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to update client")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.JSON(http.StatusOK, server)
}
func configServer(c *gin.Context) {
2020-07-15 06:52:16 +00:00
configData, err := core.ReadWgConfigFile()
2020-04-28 11:11:49 +00:00
if err != nil {
log.WithFields(log.Fields{
"err": err,
2020-07-15 06:52:16 +00:00
}).Error("failed to read wg config file")
c.AbortWithStatus(http.StatusInternalServerError)
2020-04-28 11:11:49 +00:00
return
}
// return config as txt file
c.Header("Content-Disposition", "attachment; filename=wg0.conf")
c.Data(http.StatusOK, "application/config", configData)
}
func versionStr(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"version": version.Version,
})
}