mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 10:25:52 +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
31
api/nginx/control.go
Normal file
31
api/nginx/control.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
package nginx
|
||||
|
||||
import (
|
||||
nginx2 "github.com/0xJacky/Nginx-UI/internal/nginx"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Reload(c *gin.Context) {
|
||||
output := nginx2.Reload()
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": output,
|
||||
"level": nginx2.GetLogLevel(output),
|
||||
})
|
||||
}
|
||||
|
||||
func Test(c *gin.Context) {
|
||||
output := nginx2.TestConf()
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": output,
|
||||
"level": nginx2.GetLogLevel(output),
|
||||
})
|
||||
}
|
||||
|
||||
func Restart(c *gin.Context) {
|
||||
output := nginx2.Restart()
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": output,
|
||||
"level": nginx2.GetLogLevel(output),
|
||||
})
|
||||
}
|
65
api/nginx/nginx.go
Normal file
65
api/nginx/nginx.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
package nginx
|
||||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/api"
|
||||
nginx2 "github.com/0xJacky/Nginx-UI/internal/nginx"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func BuildNginxConfig(c *gin.Context) {
|
||||
var ngxConf nginx2.NgxConfig
|
||||
if !api.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"`
|
||||
}
|
||||
|
||||
if !api.BindAndValid(c, &json) {
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("maybe_error", "nginx_config_syntax_error")
|
||||
ngxConfig := nginx2.ParseNgxConfigByContent(json.Content)
|
||||
|
||||
c.JSON(http.StatusOK, ngxConfig)
|
||||
|
||||
}
|
||||
|
||||
func FormatNginxConfig(c *gin.Context) {
|
||||
var json struct {
|
||||
Content string `json:"content" binding:"required"`
|
||||
}
|
||||
|
||||
if !api.BindAndValid(c, &json) {
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("maybe_error", "nginx_config_syntax_error")
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"content": nginx2.FmtCode(json.Content),
|
||||
})
|
||||
}
|
||||
|
||||
func Status(c *gin.Context) {
|
||||
pidPath := nginx2.GetNginxPIDPath()
|
||||
|
||||
running := true
|
||||
if fileInfo, err := os.Stat(pidPath); err != nil || fileInfo.Size() == 0 { // fileInfo.Size() == 0 no process id
|
||||
running = false
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"running": running,
|
||||
})
|
||||
}
|
||||
|
283
api/nginx/nginx_log.go
Normal file
283
api/nginx/nginx_log.go
Normal file
|
@ -0,0 +1,283 @@
|
|||
package nginx
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/0xJacky/Nginx-UI/api"
|
||||
"github.com/0xJacky/Nginx-UI/internal/helper"
|
||||
"github.com/0xJacky/Nginx-UI/internal/logger"
|
||||
nginx2 "github.com/0xJacky/Nginx-UI/internal/nginx"
|
||||
"github.com/0xJacky/Nginx-UI/settings"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/hpcloud/tail"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cast"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
PageSize = 128 * 1024
|
||||
)
|
||||
|
||||
type controlStruct struct {
|
||||
Type string `json:"type"`
|
||||
ConfName string `json:"conf_name"`
|
||||
ServerIdx int `json:"server_idx"`
|
||||
DirectiveIdx int `json:"directive_idx"`
|
||||
}
|
||||
|
||||
type nginxLogPageResp struct {
|
||||
Content string `json:"content"`
|
||||
Page int64 `json:"page"`
|
||||
}
|
||||
|
||||
func GetNginxLogPage(c *gin.Context) {
|
||||
page := cast.ToInt64(c.Query("page"))
|
||||
if page < 0 {
|
||||
page = 0
|
||||
}
|
||||
|
||||
var control controlStruct
|
||||
if !api.BindAndValid(c, &control) {
|
||||
return
|
||||
}
|
||||
|
||||
logPath, err := getLogPath(&control)
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
f, err := os.Open(logPath)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, nginxLogPageResp{})
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
logFileStat, err := os.Stat(logPath)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, nginxLogPageResp{})
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
totalPage := logFileStat.Size() / PageSize
|
||||
|
||||
if logFileStat.Size()%PageSize > 0 {
|
||||
totalPage++
|
||||
}
|
||||
|
||||
var buf []byte
|
||||
var offset int64
|
||||
if page == 0 {
|
||||
page = totalPage
|
||||
}
|
||||
|
||||
buf = make([]byte, PageSize)
|
||||
offset = (page - 1) * PageSize
|
||||
|
||||
// seek
|
||||
_, err = f.Seek(offset, io.SeekStart)
|
||||
if err != nil && err != io.EOF {
|
||||
c.JSON(http.StatusOK, nginxLogPageResp{})
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
n, err := f.Read(buf)
|
||||
|
||||
if err != nil && err != io.EOF {
|
||||
c.JSON(http.StatusOK, nginxLogPageResp{})
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, nginxLogPageResp{
|
||||
Page: page,
|
||||
Content: string(buf[:n]),
|
||||
})
|
||||
}
|
||||
|
||||
func getLogPath(control *controlStruct) (logPath string, err error) {
|
||||
switch control.Type {
|
||||
case "site":
|
||||
var config *nginx2.NgxConfig
|
||||
path := nginx2.GetConfPath("sites-available", control.ConfName)
|
||||
config, err = nginx2.ParseNgxConfig(path)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "error parsing ngx config")
|
||||
return
|
||||
}
|
||||
|
||||
if control.ServerIdx >= len(config.Servers) {
|
||||
err = errors.New("serverIdx out of range")
|
||||
return
|
||||
}
|
||||
|
||||
if control.DirectiveIdx >= len(config.Servers[control.ServerIdx].Directives) {
|
||||
err = errors.New("DirectiveIdx out of range")
|
||||
return
|
||||
}
|
||||
|
||||
directive := config.Servers[control.ServerIdx].Directives[control.DirectiveIdx]
|
||||
switch directive.Directive {
|
||||
case "access_log", "error_log":
|
||||
// ok
|
||||
default:
|
||||
err = errors.New("directive.Params neither access_log nor error_log")
|
||||
return
|
||||
}
|
||||
|
||||
if directive.Params == "" {
|
||||
err = errors.New("directive.Params is empty")
|
||||
return
|
||||
}
|
||||
|
||||
logPath = directive.Params
|
||||
|
||||
case "error":
|
||||
if settings.NginxSettings.ErrorLogPath == "" {
|
||||
err = errors.New("settings.NginxLogSettings.ErrorLogPath is empty," +
|
||||
" refer to https://nginxui.com/zh_CN/guide/config-nginx-log.html for more information")
|
||||
return
|
||||
}
|
||||
logPath = settings.NginxSettings.ErrorLogPath
|
||||
|
||||
default:
|
||||
if settings.NginxSettings.AccessLogPath == "" {
|
||||
err = errors.New("settings.NginxLogSettings.AccessLogPath is empty," +
|
||||
" refer to https://nginxui.com/zh_CN/guide/config-nginx-log.html for more information")
|
||||
return
|
||||
}
|
||||
logPath = settings.NginxSettings.AccessLogPath
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func tailNginxLog(ws *websocket.Conn, controlChan chan controlStruct, errChan chan error) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
control := <-controlChan
|
||||
|
||||
for {
|
||||
logPath, err := getLogPath(&control)
|
||||
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
|
||||
seek := tail.SeekInfo{
|
||||
Offset: 0,
|
||||
Whence: io.SeekEnd,
|
||||
}
|
||||
|
||||
if !helper.FileExists(logPath) {
|
||||
errChan <- errors.New("error log path not exists")
|
||||
return
|
||||
}
|
||||
|
||||
// Create a tail
|
||||
t, err := tail.TailFile(logPath, tail.Config{Follow: true,
|
||||
ReOpen: true, Location: &seek})
|
||||
|
||||
if err != nil {
|
||||
errChan <- errors.Wrap(err, "error tailing log")
|
||||
return
|
||||
}
|
||||
|
||||
for {
|
||||
var next = false
|
||||
select {
|
||||
case line := <-t.Lines:
|
||||
// Print the text of each received line
|
||||
if line == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
err = ws.WriteMessage(websocket.TextMessage, []byte(line.Text))
|
||||
|
||||
if err != nil && websocket.IsUnexpectedCloseError(err, websocket.CloseNormalClosure) {
|
||||
errChan <- errors.Wrap(err, "error tailNginxLog write message")
|
||||
return
|
||||
}
|
||||
case control = <-controlChan:
|
||||
next = true
|
||||
break
|
||||
}
|
||||
if next {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleLogControl(ws *websocket.Conn, controlChan chan controlStruct, errChan chan error) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
msgType, payload, err := ws.ReadMessage()
|
||||
if err != nil && websocket.IsUnexpectedCloseError(err, websocket.CloseNormalClosure) {
|
||||
errChan <- errors.Wrap(err, "error handleLogControl read message")
|
||||
return
|
||||
}
|
||||
|
||||
if msgType != websocket.TextMessage {
|
||||
errChan <- errors.New("error handleLogControl message type")
|
||||
return
|
||||
}
|
||||
|
||||
var msg controlStruct
|
||||
err = json.Unmarshal(payload, &msg)
|
||||
if err != nil {
|
||||
errChan <- errors.Wrap(err, "error ReadWsAndWritePty json.Unmarshal")
|
||||
return
|
||||
}
|
||||
controlChan <- msg
|
||||
}
|
||||
}
|
||||
|
||||
func NginxLog(c *gin.Context) {
|
||||
var upGrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
// upgrade http to websocket
|
||||
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
defer ws.Close()
|
||||
|
||||
errChan := make(chan error, 1)
|
||||
controlChan := make(chan controlStruct, 1)
|
||||
|
||||
go tailNginxLog(ws, controlChan, errChan)
|
||||
go handleLogControl(ws, controlChan, errChan)
|
||||
|
||||
if err = <-errChan; err != nil {
|
||||
logger.Error(err)
|
||||
_ = ws.WriteMessage(websocket.TextMessage, []byte(err.Error()))
|
||||
return
|
||||
}
|
||||
}
|
18
api/nginx/router.go
Normal file
18
api/nginx/router.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package nginx
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func InitRouter(r *gin.RouterGroup) {
|
||||
r.POST("ngx/build_config", BuildNginxConfig)
|
||||
r.POST("ngx/tokenize_config", TokenizeNginxConfig)
|
||||
r.POST("ngx/format_code", FormatNginxConfig)
|
||||
r.POST("nginx/reload", Reload)
|
||||
r.POST("nginx/restart", Restart)
|
||||
r.POST("nginx/test", Test)
|
||||
r.GET("nginx/status", Status)
|
||||
r.POST("nginx_log", GetNginxLogPage)
|
||||
}
|
||||
|
||||
func InitNginxLogRouter(r *gin.RouterGroup) {
|
||||
r.GET("nginx_log", NginxLog)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue