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/api.go

203 lines
4.3 KiB
Go
Raw Normal View History

2020-01-30 06:45:49 +00:00
package api
import (
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/skip2/go-qrcode"
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/core"
2020-01-30 06:45:49 +00:00
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/model"
"net/http"
)
// ApplyRoutes applies router to gin Router
func ApplyRoutes(r *gin.Engine) {
client := r.Group("/api/v1.0/client")
{
client.POST("", createClient)
client.GET("/:id", readClient)
client.PATCH("/:id", updateClient)
client.DELETE("/:id", deleteClient)
client.GET("", readClients)
client.GET("/:id/config", configClient)
2020-02-03 07:19:24 +00:00
client.GET("/:id/email", emailClient)
2020-01-30 06:45:49 +00:00
}
server := r.Group("/api/v1.0/server")
{
server.GET("", readServer)
server.PATCH("", updateServer)
}
}
func createClient(c *gin.Context) {
var data model.Client
if err := c.ShouldBindJSON(&data); err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to bind")
c.AbortWithStatus(http.StatusUnprocessableEntity)
return
}
client, err := core.CreateClient(&data)
2020-01-30 06:45:49 +00:00
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to create client")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.JSON(http.StatusOK, client)
}
func readClient(c *gin.Context) {
id := c.Param("id")
client, err := core.ReadClient(id)
2020-01-30 06:45:49 +00:00
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 updateClient(c *gin.Context) {
var data model.Client
id := c.Param("id")
if err := c.ShouldBindJSON(&data); err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to bind")
c.AbortWithStatus(http.StatusUnprocessableEntity)
return
}
client, err := core.UpdateClient(id, &data)
2020-01-30 06:45:49 +00:00
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to update client")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.JSON(http.StatusOK, client)
}
func deleteClient(c *gin.Context) {
id := c.Param("id")
err := core.DeleteClient(id)
2020-01-30 06:45:49 +00:00
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to remove client")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.JSON(http.StatusOK, gin.H{})
}
func readClients(c *gin.Context) {
clients, err := core.ReadClients()
2020-01-30 06:45:49 +00:00
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to list clients")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.JSON(http.StatusOK, clients)
}
func configClient(c *gin.Context) {
configData, err := core.ReadClientConfig(c.Param("id"))
2020-01-30 06:45:49 +00:00
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to read client config")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
formatQr := c.DefaultQuery("qrcode", "false")
if formatQr == "false" {
// return config as txt file
c.Header("Content-Disposition", "attachment; filename=wg0.conf")
c.Data(http.StatusOK, "application/config", configData)
return
2020-01-30 07:12:26 +00:00
}
// return config as png qrcode
png, err := qrcode.Encode(string(configData), qrcode.Medium, 220)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to create qrcode")
c.AbortWithStatus(http.StatusInternalServerError)
2020-01-30 06:45:49 +00:00
return
}
2020-01-30 07:12:26 +00:00
c.Data(http.StatusOK, "image/png", png)
return
2020-01-30 06:45:49 +00:00
}
2020-02-03 07:19:24 +00:00
func emailClient(c *gin.Context) {
id := c.Param("id")
err := core.EmailClient(id)
2020-02-03 07:19:24 +00:00
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to send email to client")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.JSON(http.StatusOK, gin.H{})
}
2020-01-30 06:45:49 +00:00
func readServer(c *gin.Context) {
client, err := core.ReadServer()
2020-01-30 06:45:49 +00:00
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
}
client, err := core.UpdateServer(&data)
2020-01-30 06:45:49 +00:00
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to update client")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.JSON(http.StatusOK, client)
2020-01-30 07:12:26 +00:00
}