|
@@ -2,78 +2,19 @@ package router
|
|
|
|
|
|
import (
|
|
import (
|
|
"bufio"
|
|
"bufio"
|
|
- "encoding/base64"
|
|
|
|
- "github.com/0xJacky/Nginx-UI/frontend"
|
|
|
|
api2 "github.com/0xJacky/Nginx-UI/server/api"
|
|
api2 "github.com/0xJacky/Nginx-UI/server/api"
|
|
- "github.com/0xJacky/Nginx-UI/server/model"
|
|
|
|
- "github.com/gin-contrib/static"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin"
|
|
- "io/fs"
|
|
|
|
- "log"
|
|
|
|
"net/http"
|
|
"net/http"
|
|
- "path/filepath"
|
|
|
|
"strings"
|
|
"strings"
|
|
)
|
|
)
|
|
|
|
|
|
-func authRequired() gin.HandlerFunc {
|
|
|
|
- return func(c *gin.Context) {
|
|
|
|
- token := c.GetHeader("Authorization")
|
|
|
|
- if token == "" {
|
|
|
|
- tmp, _ := base64.StdEncoding.DecodeString(c.Query("token"))
|
|
|
|
- token = string(tmp)
|
|
|
|
- if token == "" {
|
|
|
|
- c.JSON(http.StatusForbidden, gin.H{
|
|
|
|
- "message": "auth fail",
|
|
|
|
- })
|
|
|
|
- c.Abort()
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- n := model.CheckToken(token)
|
|
|
|
-
|
|
|
|
- if n < 1 {
|
|
|
|
- c.JSON(http.StatusForbidden, gin.H{
|
|
|
|
- "message": "auth fail",
|
|
|
|
- })
|
|
|
|
- c.Abort()
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- c.Next()
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-type serverFileSystemType struct {
|
|
|
|
- http.FileSystem
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (f serverFileSystemType) Exists(prefix string, path string) bool {
|
|
|
|
- _, err := f.Open(filepath.Join(prefix, path))
|
|
|
|
- return err == nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func mustFS(dir string) (serverFileSystem static.ServeFileSystem) {
|
|
|
|
-
|
|
|
|
- sub, err := fs.Sub(frontend.DistFS, filepath.Join("dist", dir))
|
|
|
|
-
|
|
|
|
- if err != nil {
|
|
|
|
- log.Println(err)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- serverFileSystem = serverFileSystemType{
|
|
|
|
- http.FS(sub),
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
func InitRouter() *gin.Engine {
|
|
func InitRouter() *gin.Engine {
|
|
r := gin.New()
|
|
r := gin.New()
|
|
r.Use(gin.Logger())
|
|
r.Use(gin.Logger())
|
|
|
|
|
|
r.Use(gin.Recovery())
|
|
r.Use(gin.Recovery())
|
|
|
|
|
|
- r.Use(static.Serve("/", mustFS("")))
|
|
|
|
|
|
+ r.Use(tryStatic("/", mustFS("")))
|
|
|
|
|
|
r.NoRoute(func(c *gin.Context) {
|
|
r.NoRoute(func(c *gin.Context) {
|
|
accept := c.Request.Header.Get("Accept")
|
|
accept := c.Request.Header.Get("Accept")
|