From 8a7f7672d92e57574ed9d5044dee500b6f8ddba8 Mon Sep 17 00:00:00 2001 From: 0xJacky Date: Fri, 18 Feb 2022 23:19:53 +0800 Subject: [PATCH] Fix .js mime issues in windows --- frontend/src/layouts/FooterLayout.vue | 2 +- frontend/src/views/other/About.vue | 5 +- frontend/version.json | 2 +- go.mod | 27 ++++++++- server/router/middleware.go | 84 +++++++++++++++++++++++++++ server/router/routers.go | 61 +------------------ server/tool/nginx.go | 65 +++++++++++---------- 7 files changed, 149 insertions(+), 97 deletions(-) create mode 100644 server/router/middleware.go diff --git a/frontend/src/layouts/FooterLayout.vue b/frontend/src/layouts/FooterLayout.vue index 398b2ce5..5fcdfd04 100644 --- a/frontend/src/layouts/FooterLayout.vue +++ b/frontend/src/layouts/FooterLayout.vue @@ -1,6 +1,6 @@ diff --git a/frontend/src/views/other/About.vue b/frontend/src/views/other/About.vue index 4bac41d7..c2a49411 100644 --- a/frontend/src/views/other/About.vue +++ b/frontend/src/views/other/About.vue @@ -7,7 +7,8 @@

Yet another WebUI for Nginx

Version: {{ version }} ({{ build_id }})

项目组

-

Designer:@0xJacky

+

@0xJacky

+

@Hintay

技术栈

Go

Gin

@@ -15,7 +16,7 @@

Websocket

开源协议

GNU General Public License v2.0

-

Copyright © 2020 - {{ this_year }} 0xJacky

+

Copyright © 2020 - {{ this_year }} Nginx UI

diff --git a/frontend/version.json b/frontend/version.json index 67c23b80..07c4a3a8 100644 --- a/frontend/version.json +++ b/frontend/version.json @@ -1 +1 @@ -{"version":"1.1.0","build_id":4,"total_build":21} \ No newline at end of file +{"version":"1.1.0","build_id":5,"total_build":22} \ No newline at end of file diff --git a/go.mod b/go.mod index 34af1ece..414682e1 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/0xJacky/Nginx-UI -go 1.16 +go 1.17 require ( github.com/dgrijalva/jwt-go v3.2.0+incompatible @@ -21,3 +21,28 @@ require ( gorm.io/driver/sqlite v1.1.4 gorm.io/gorm v1.21.14 ) + +require ( + github.com/StackExchange/wmi v1.2.1 // indirect + github.com/cenkalti/backoff/v4 v4.1.0 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-ole/go-ole v1.2.5 // indirect + github.com/golang/protobuf v1.3.4 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.2 // indirect + github.com/json-iterator/go v1.1.9 // indirect + github.com/leodido/go-urn v1.2.0 // indirect + github.com/mattn/go-isatty v0.0.12 // indirect + github.com/mattn/go-sqlite3 v1.14.5 // indirect + github.com/miekg/dns v1.1.40 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.1 // indirect + github.com/tklauser/go-sysconf v0.3.7 // indirect + github.com/tklauser/numcpus v0.2.3 // indirect + github.com/ugorji/go/codec v1.1.7 // indirect + golang.org/x/net v0.0.0-20210220033124-5f55cee0dc0d // indirect + golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect + golang.org/x/text v0.3.4 // indirect + gopkg.in/square/go-jose.v2 v2.5.1 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect +) diff --git a/server/router/middleware.go b/server/router/middleware.go new file mode 100644 index 00000000..b58df665 --- /dev/null +++ b/server/router/middleware.go @@ -0,0 +1,84 @@ +package router + +import ( + "encoding/base64" + "github.com/0xJacky/Nginx-UI/frontend" + "github.com/0xJacky/Nginx-UI/server/model" + "github.com/gin-contrib/static" + "github.com/gin-gonic/gin" + "io/fs" + "log" + "net/http" + "path" + "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(path.Join(prefix, _path)) + return err == nil +} + +func mustFS(dir string) (serverFileSystem static.ServeFileSystem) { + + sub, err := fs.Sub(frontend.DistFS, path.Join("dist", dir)) + + if err != nil { + log.Println(err) + return + } + + serverFileSystem = serverFileSystemType{ + http.FS(sub), + } + + return +} + +// tryStatic Static returns a middleware handler that serves static files in the given directory. +func tryStatic(urlPrefix string, fs static.ServeFileSystem) gin.HandlerFunc { + fileserver := http.FileServer(fs) + if urlPrefix != "" { + fileserver = http.StripPrefix(urlPrefix, fileserver) + } + return func(c *gin.Context) { + if fs.Exists(urlPrefix, c.Request.URL.Path) { + fileserver.ServeHTTP(c.Writer, c.Request) + if strings.Contains(c.Request.URL.Path, ".js") { + c.Writer.Header().Set("content-type", "application/javascript") + } + c.Abort() + } + } +} diff --git a/server/router/routers.go b/server/router/routers.go index 9d4bb5d9..65c12967 100644 --- a/server/router/routers.go +++ b/server/router/routers.go @@ -2,78 +2,19 @@ package router import ( "bufio" - "encoding/base64" - "github.com/0xJacky/Nginx-UI/frontend" 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" - "io/fs" - "log" "net/http" - "path/filepath" "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 { r := gin.New() r.Use(gin.Logger()) r.Use(gin.Recovery()) - r.Use(static.Serve("/", mustFS(""))) + r.Use(tryStatic("/", mustFS(""))) r.NoRoute(func(c *gin.Context) { accept := c.Request.Header.Get("Accept") diff --git a/server/tool/nginx.go b/server/tool/nginx.go index 66e6c51c..80f6dddb 100644 --- a/server/tool/nginx.go +++ b/server/tool/nginx.go @@ -1,52 +1,53 @@ package tool import ( - "errors" - "log" - "os/exec" - "path/filepath" - "regexp" - "strings" + "errors" + "log" + "os/exec" + "path/filepath" + "regexp" + "strings" ) func TestNginxConf(filePath string) error { - out, err := exec.Command("nginx", "-t").CombinedOutput() - if err != nil { - log.Println(err) - } - output := string(out) - log.Println(output) - if strings.Contains(output, "failed") { - return errors.New(output) - } - return nil + out, err := exec.Command("nginx", "-t").CombinedOutput() + if err != nil { + log.Println(err) + } + output := string(out) + log.Println(output) + if strings.Contains(output, "failed") { + return errors.New(output) + } + return nil } func ReloadNginx() string { - out, err := exec.Command("nginx", "-s", "reload").CombinedOutput() + out, err := exec.Command("nginx", "-s", "reload").CombinedOutput() - if err != nil { - log.Println(err) - } + if err != nil { + log.Println(err) + } - output := string(out) - log.Println(output) + output := string(out) + log.Println(output) - return output + return output } func GetNginxConfPath(dir string) string { - out, err := exec.Command("nginx", "-V").CombinedOutput() - if err != nil { - log.Fatal(err) - } - // fmt.Printf("%s\n", out) + out, err := exec.Command("nginx", "-V").CombinedOutput() + if err != nil { + log.Println(err) + return "" + } + // fmt.Printf("%s\n", out) - r, _ := regexp.Compile("--conf-path=(.*)/(.*.conf)") + r, _ := regexp.Compile("--conf-path=(.*)/(.*.conf)") - confPath := r.FindStringSubmatch(string(out))[1] + confPath := r.FindStringSubmatch(string(out))[1] - // fmt.Println(confPath) + // fmt.Println(confPath) - return filepath.Join(confPath, dir) + return filepath.Join(confPath, dir) }