feat: inspect configurations #69

This commit is contained in:
0xJacky 2023-01-31 22:51:46 +08:00
parent 561771cf10
commit 768da42e35
No known key found for this signature in database
GPG key ID: B6E4A6E4A561BAF0
23 changed files with 346 additions and 144 deletions

View file

@ -1,65 +1,67 @@
package api
import (
"github.com/0xJacky/Nginx-UI/server/pkg/nginx"
"github.com/gin-gonic/gin"
"net/http"
"strings"
"github.com/0xJacky/Nginx-UI/server/pkg/nginx"
"github.com/gin-gonic/gin"
"net/http"
)
func BuildNginxConfig(c *gin.Context) {
var ngxConf nginx.NgxConfig
if !BindAndValid(c, &ngxConf) {
return
}
c.Set("maybe_error", "nginx_config_syntax_error")
c.JSON(http.StatusOK, gin.H{
"content": ngxConf.BuildConfig(),
})
var ngxConf nginx.NgxConfig
if !BindAndValid(c, &ngxConf) {
return
}
c.Set("maybe_error", "nginx_config_syntax_error")
c.JSON(http.StatusOK, gin.H{
"content": ngxConf.BuildConfig(),
})
}
func TokenizeNginxConfig(c *gin.Context) {
var json struct {
Content string `json:"content" binding:"required"`
}
var json struct {
Content string `json:"content" binding:"required"`
}
if !BindAndValid(c, &json) {
return
}
if !BindAndValid(c, &json) {
return
}
c.Set("maybe_error", "nginx_config_syntax_error")
ngxConfig := nginx.ParseNgxConfigByContent(json.Content)
c.Set("maybe_error", "nginx_config_syntax_error")
ngxConfig := nginx.ParseNgxConfigByContent(json.Content)
c.JSON(http.StatusOK, ngxConfig)
c.JSON(http.StatusOK, ngxConfig)
}
func FormatNginxConfig(c *gin.Context) {
var json struct {
Content string `json:"content" binding:"required"`
}
var json struct {
Content string `json:"content" binding:"required"`
}
if !BindAndValid(c, &json) {
return
}
if !BindAndValid(c, &json) {
return
}
c.Set("maybe_error", "nginx_config_syntax_error")
c.JSON(http.StatusOK, gin.H{
"content": nginx.FmtCode(json.Content),
})
c.Set("maybe_error", "nginx_config_syntax_error")
c.JSON(http.StatusOK, gin.H{
"content": nginx.FmtCode(json.Content),
})
}
func ReloadNginx(c *gin.Context) {
output := nginx.Reload()
output := nginx.Reload()
if output != "" && strings.Contains(output, "error") {
c.JSON(http.StatusInternalServerError, gin.H{
"message": output,
})
return
}
c.JSON(http.StatusOK, gin.H{
"message": "ok",
})
c.JSON(http.StatusOK, gin.H{
"message": output,
"level": nginx.GetLogLevel(output),
})
}
func TestNginx(c *gin.Context) {
output := nginx.TestConf()
c.JSON(http.StatusOK, gin.H{
"message": output,
"level": nginx.GetLogLevel(output),
})
}