|
@@ -2,6 +2,7 @@ package api
|
|
|
|
|
|
import (
|
|
import (
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
|
+ "github.com/0xJacky/Nginx-UI/server/pkg/nginx"
|
|
"github.com/0xJacky/Nginx-UI/server/settings"
|
|
"github.com/0xJacky/Nginx-UI/server/settings"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gorilla/websocket"
|
|
"github.com/gorilla/websocket"
|
|
@@ -10,11 +11,15 @@ import (
|
|
"io"
|
|
"io"
|
|
"log"
|
|
"log"
|
|
"net/http"
|
|
"net/http"
|
|
|
|
+ "path/filepath"
|
|
)
|
|
)
|
|
|
|
|
|
type controlStruct struct {
|
|
type controlStruct struct {
|
|
- Fetch string `json:"fetch"`
|
|
|
|
- Type string `json:"type"`
|
|
|
|
|
|
+ Fetch string `json:"fetch"`
|
|
|
|
+ Type string `json:"type"`
|
|
|
|
+ ConfName string `json:"conf_name"`
|
|
|
|
+ ServerIdx int `json:"server_idx"`
|
|
|
|
+ DirectiveIdx int `json:"directive_idx"`
|
|
}
|
|
}
|
|
|
|
|
|
func tailNginxLog(ws *websocket.Conn, controlChan chan controlStruct, errChan chan error) {
|
|
func tailNginxLog(ws *websocket.Conn, controlChan chan controlStruct, errChan chan error) {
|
|
@@ -33,11 +38,40 @@ func tailNginxLog(ws *websocket.Conn, controlChan chan controlStruct, errChan ch
|
|
seek.Offset = 0
|
|
seek.Offset = 0
|
|
seek.Whence = io.SeekEnd
|
|
seek.Whence = io.SeekEnd
|
|
}
|
|
}
|
|
|
|
+ var logPath string
|
|
|
|
+ switch control.Type {
|
|
|
|
+ case "site":
|
|
|
|
+ path := filepath.Join(nginx.GetNginxConfPath("sites-available"), control.ConfName)
|
|
|
|
+ config, err := nginx.ParseNgxConfig(path)
|
|
|
|
+ if err != nil {
|
|
|
|
+ errChan <- errors.Wrap(err, "error parsing ngx config")
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if control.ServerIdx >= len(config.Servers) {
|
|
|
|
+ errChan <- errors.New("serverIdx out of range")
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ if control.DirectiveIdx >= len(config.Servers[control.ServerIdx].Directives) {
|
|
|
|
+ errChan <- 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:
|
|
|
|
+ errChan <- errors.New("directive.Params neither access_log nor error_log")
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
|
|
- logPath := settings.NginxLogSettings.AccessLogPath
|
|
|
|
|
|
+ logPath = directive.Params
|
|
|
|
|
|
- if control.Type == "error" {
|
|
|
|
|
|
+ case "error":
|
|
logPath = settings.NginxLogSettings.ErrorLogPath
|
|
logPath = settings.NginxLogSettings.ErrorLogPath
|
|
|
|
+ default:
|
|
|
|
+ logPath = settings.NginxLogSettings.AccessLogPath
|
|
}
|
|
}
|
|
|
|
|
|
// Create a tail
|
|
// Create a tail
|
|
@@ -61,7 +95,6 @@ func tailNginxLog(ws *websocket.Conn, controlChan chan controlStruct, errChan ch
|
|
return
|
|
return
|
|
}
|
|
}
|
|
case control = <-controlChan:
|
|
case control = <-controlChan:
|
|
- log.Println("control change")
|
|
|
|
next = true
|
|
next = true
|
|
break
|
|
break
|
|
}
|
|
}
|
|
@@ -125,6 +158,7 @@ func NginxLog(c *gin.Context) {
|
|
|
|
|
|
if err = <-errChan; err != nil {
|
|
if err = <-errChan; err != nil {
|
|
log.Println(err)
|
|
log.Println(err)
|
|
|
|
+ _ = ws.WriteMessage(websocket.TextMessage, []byte(err.Error()))
|
|
return
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|