2020-10-06 16:14:34 +00:00
|
|
|
package status
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-10-06 20:55:17 +00:00
|
|
|
"os"
|
2020-10-06 16:14:34 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-04-06 12:23:23 +00:00
|
|
|
"github.com/vx3r/wg-gen-web/core"
|
2020-10-06 16:14:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ApplyRoutes applies router to gin Router
|
|
|
|
func ApplyRoutes(r *gin.RouterGroup) {
|
|
|
|
g := r.Group("/status")
|
|
|
|
{
|
2020-10-06 20:55:17 +00:00
|
|
|
g.GET("/enabled", readEnabled)
|
2020-10-06 16:14:34 +00:00
|
|
|
g.GET("/interface", readInterfaceStatus)
|
|
|
|
g.GET("/clients", readClientStatus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 20:55:17 +00:00
|
|
|
func readEnabled(c *gin.Context) {
|
|
|
|
c.JSON(http.StatusOK, os.Getenv("WG_STATS_API") != "")
|
|
|
|
}
|
|
|
|
|
2020-10-06 16:14:34 +00:00
|
|
|
func readInterfaceStatus(c *gin.Context) {
|
|
|
|
status, err := core.ReadInterfaceStatus()
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"err": err,
|
|
|
|
}).Error("failed to read interface status")
|
2020-10-06 16:58:34 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, err.Error())
|
2020-10-06 16:14:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func readClientStatus(c *gin.Context) {
|
|
|
|
status, err := core.ReadClientStatus()
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"err": err,
|
|
|
|
}).Error("failed to read client status")
|
2020-10-06 16:58:34 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, err.Error())
|
2020-10-06 16:14:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, status)
|
|
|
|
}
|