mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 18:35:51 +02:00
refactor: structure of api-router directory
This commit is contained in:
parent
d272f7900f
commit
50b4fbcda4
38 changed files with 610 additions and 524 deletions
143
api/cluster/environment.go
Normal file
143
api/cluster/environment.go
Normal file
|
@ -0,0 +1,143 @@
|
|||
package cluster
|
||||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/api"
|
||||
"github.com/0xJacky/Nginx-UI/internal/analytic"
|
||||
"github.com/0xJacky/Nginx-UI/internal/environment"
|
||||
"github.com/0xJacky/Nginx-UI/model"
|
||||
"github.com/0xJacky/Nginx-UI/query"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/spf13/cast"
|
||||
"net/http"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func GetEnvironment(c *gin.Context) {
|
||||
id := cast.ToInt(c.Param("id"))
|
||||
|
||||
envQuery := query.Environment
|
||||
|
||||
env, err := envQuery.FirstByID(id)
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, env)
|
||||
}
|
||||
|
||||
func GetEnvironmentList(c *gin.Context) {
|
||||
data, err := environment.RetrieveEnvironmentList()
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"data": data,
|
||||
})
|
||||
}
|
||||
|
||||
type EnvironmentManageJson struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
URL string `json:"url" binding:"required"`
|
||||
Token string `json:"token" binding:"required"`
|
||||
OperationSync bool `json:"operation_sync"`
|
||||
SyncApiRegex string `json:"sync_api_regex"`
|
||||
}
|
||||
|
||||
func validateRegex(data EnvironmentManageJson) error {
|
||||
if data.OperationSync {
|
||||
_, err := regexp.Compile(data.SyncApiRegex)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func AddEnvironment(c *gin.Context) {
|
||||
var json EnvironmentManageJson
|
||||
if !api.BindAndValid(c, &json) {
|
||||
return
|
||||
}
|
||||
if err := validateRegex(json); err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
env := model.Environment{
|
||||
Name: json.Name,
|
||||
URL: json.URL,
|
||||
Token: json.Token,
|
||||
OperationSync: json.OperationSync,
|
||||
SyncApiRegex: json.SyncApiRegex,
|
||||
}
|
||||
|
||||
envQuery := query.Environment
|
||||
|
||||
err := envQuery.Create(&env)
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
go analytic.RestartRetrieveNodesStatus()
|
||||
|
||||
c.JSON(http.StatusOK, env)
|
||||
}
|
||||
|
||||
func EditEnvironment(c *gin.Context) {
|
||||
id := cast.ToInt(c.Param("id"))
|
||||
|
||||
var json EnvironmentManageJson
|
||||
if !api.BindAndValid(c, &json) {
|
||||
return
|
||||
}
|
||||
if err := validateRegex(json); err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
envQuery := query.Environment
|
||||
|
||||
env, err := envQuery.FirstByID(id)
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = envQuery.Where(envQuery.ID.Eq(env.ID)).Updates(&model.Environment{
|
||||
Name: json.Name,
|
||||
URL: json.URL,
|
||||
Token: json.Token,
|
||||
OperationSync: json.OperationSync,
|
||||
SyncApiRegex: json.SyncApiRegex,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
go analytic.RestartRetrieveNodesStatus()
|
||||
|
||||
GetEnvironment(c)
|
||||
}
|
||||
|
||||
func DeleteEnvironment(c *gin.Context) {
|
||||
id := cast.ToInt(c.Param("id"))
|
||||
envQuery := query.Environment
|
||||
|
||||
env, err := envQuery.FirstByID(id)
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
err = envQuery.DeleteByID(env.ID)
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
go analytic.RestartRetrieveNodesStatus()
|
||||
|
||||
c.JSON(http.StatusNoContent, nil)
|
||||
}
|
46
api/cluster/node.go
Normal file
46
api/cluster/node.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
package cluster
|
||||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/api"
|
||||
analytic2 "github.com/0xJacky/Nginx-UI/internal/analytic"
|
||||
"github.com/0xJacky/Nginx-UI/internal/upgrader"
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/shirou/gopsutil/v3/cpu"
|
||||
"github.com/shirou/gopsutil/v3/disk"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetCurrentNode(c *gin.Context) {
|
||||
if _, ok := c.Get("NodeSecret"); !ok {
|
||||
c.JSON(http.StatusNotAcceptable, gin.H{
|
||||
"message": "node secret not exist",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
runtimeInfo, err := upgrader.GetRuntimeInfo()
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
cpuInfo, _ := cpu.Info()
|
||||
memory, _ := analytic2.GetMemoryStat()
|
||||
ver, _ := upgrader.GetCurrentVersion()
|
||||
diskUsage, _ := disk.Usage(".")
|
||||
|
||||
nodeInfo := analytic2.NodeInfo{
|
||||
NodeRuntimeInfo: runtimeInfo,
|
||||
CPUNum: len(cpuInfo),
|
||||
MemoryTotal: memory.Total,
|
||||
DiskTotal: humanize.Bytes(diskUsage.Total),
|
||||
Version: ver.Version,
|
||||
}
|
||||
|
||||
stat := analytic2.GetNodeStat()
|
||||
|
||||
c.JSON(http.StatusOK, analytic2.Node{
|
||||
NodeInfo: nodeInfo,
|
||||
NodeStat: stat,
|
||||
})
|
||||
}
|
17
api/cluster/router.go
Normal file
17
api/cluster/router.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package cluster
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func InitRouter(r *gin.RouterGroup) {
|
||||
// Environment
|
||||
r.GET("environments", GetEnvironmentList)
|
||||
envGroup := r.Group("environment")
|
||||
{
|
||||
envGroup.GET("/:id", GetEnvironment)
|
||||
envGroup.POST("", AddEnvironment)
|
||||
envGroup.POST("/:id", EditEnvironment)
|
||||
envGroup.DELETE("/:id", DeleteEnvironment)
|
||||
}
|
||||
// Node
|
||||
r.GET("node", GetCurrentNode)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue