Explorar el Código

feat: build with no embedded front-end files

Hintay hace 2 meses
padre
commit
a560d98acf

+ 1 - 1
.air.toml

@@ -7,7 +7,7 @@ tmp_dir = "tmp"
 
 [build]
 # Just plain old shell command. You could use `make` as well.
-cmd = "CGO_ENABLED=1 go build -tags=jsoniter -ldflags=\"-X 'github.com/0xJacky/Nginx-UI/settings.buildTime=$(date +%s)'\" -v -o ./tmp/main ."
+cmd = "CGO_ENABLED=1 go build -tags=jsoniter,unembed -ldflags=\"-X 'github.com/0xJacky/Nginx-UI/settings.buildTime=$(date +%s)'\" -v -o ./tmp/main ."
 # Binary file yields from `cmd`.
 bin = "tmp/main"
 # Customize binary.

+ 9 - 1
.idea/nginx-ui.iml

@@ -1,6 +1,14 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <module type="WEB_MODULE" version="4">
-  <component name="Go" enabled="true" />
+  <component name="Go" enabled="true">
+    <buildTags>
+      <option name="customFlags">
+        <array>
+          <option value="unembed" />
+        </array>
+      </option>
+    </buildTags>
+  </component>
   <component name="NewModuleRootManager">
     <content url="file://$MODULE_DIR$" />
     <orderEntry type="inheritedJdk" />

+ 4 - 0
app/app.go

@@ -1,3 +1,5 @@
+//go:build !unembed
+
 package app
 
 import (
@@ -6,3 +8,5 @@ import (
 
 //go:embed i18n.json dist/* dist/*/* src/language/* src/language/*/*
 var DistFS embed.FS
+
+var VersionPath = "dist/version.json"

+ 10 - 0
app/app_unembed.go

@@ -0,0 +1,10 @@
+//go:build unembed
+
+package app
+
+import "embed"
+
+//go:embed i18n.json src/language/* src/language/*/* src/version.json
+var DistFS embed.FS
+
+var VersionPath = "src/version.json"

+ 29 - 0
internal/middleware/embed.go

@@ -0,0 +1,29 @@
+//go:build !unembed
+
+package middleware
+
+import (
+	"io/fs"
+	"net/http"
+	"path"
+
+	"github.com/0xJacky/Nginx-UI/app"
+	"github.com/gin-contrib/static"
+	"github.com/uozi-tech/cosy/logger"
+)
+
+func MustFs(dir string) (serverFileSystem static.ServeFileSystem) {
+
+	sub, err := fs.Sub(app.DistFS, path.Join("dist", dir))
+
+	if err != nil {
+		logger.Error(err)
+		return
+	}
+
+	serverFileSystem = ServerFileSystemType{
+		http.FS(sub),
+	}
+
+	return
+}

+ 4 - 22
internal/middleware/middleware.go

@@ -2,16 +2,14 @@ package middleware
 
 import (
 	"encoding/base64"
-	"github.com/0xJacky/Nginx-UI/app"
+	"net/http"
+	"path"
+	"strings"
+
 	"github.com/0xJacky/Nginx-UI/internal/user"
 	"github.com/0xJacky/Nginx-UI/settings"
-	"github.com/gin-contrib/static"
 	"github.com/gin-gonic/gin"
 	"github.com/uozi-tech/cosy/logger"
-	"io/fs"
-	"net/http"
-	"path"
-	"strings"
 )
 
 func AuthRequired() gin.HandlerFunc {
@@ -72,22 +70,6 @@ func (f ServerFileSystemType) Exists(prefix string, _path string) bool {
 	return err == nil
 }
 
-func MustFs(dir string) (serverFileSystem static.ServeFileSystem) {
-
-	sub, err := fs.Sub(app.DistFS, path.Join("dist", dir))
-
-	if err != nil {
-		logger.Error(err)
-		return
-	}
-
-	serverFileSystem = ServerFileSystemType{
-		http.FS(sub),
-	}
-
-	return
-}
-
 func CacheJs() gin.HandlerFunc {
 	return func(c *gin.Context) {
 		if strings.Contains(c.Request.URL.String(), "js") {

+ 4 - 3
internal/upgrader/info.go

@@ -2,11 +2,12 @@ package upgrader
 
 import (
 	"encoding/json"
-	"github.com/0xJacky/Nginx-UI/app"
-	"github.com/pkg/errors"
 	"os"
 	"path/filepath"
 	"runtime"
+
+	"github.com/0xJacky/Nginx-UI/app"
+	"github.com/pkg/errors"
 )
 
 type RuntimeInfo struct {
@@ -43,7 +44,7 @@ func GetRuntimeInfo() (r RuntimeInfo, err error) {
 }
 
 func GetCurrentVersion() (c CurVersion, err error) {
-	verJson, err := app.DistFS.ReadFile("dist/version.json")
+	verJson, err := app.DistFS.ReadFile(app.VersionPath)
 	if err != nil {
 		err = errors.Wrap(err, "service.GetCurrentVersion ReadFile err")
 		return

+ 4 - 7
router/routers.go

@@ -1,6 +1,8 @@
 package router
 
 import (
+	"net/http"
+
 	"github.com/0xJacky/Nginx-UI/api/analytic"
 	"github.com/0xJacky/Nginx-UI/api/certificate"
 	"github.com/0xJacky/Nginx-UI/api/cluster"
@@ -19,19 +21,14 @@ import (
 	"github.com/0xJacky/Nginx-UI/api/upstream"
 	"github.com/0xJacky/Nginx-UI/api/user"
 	"github.com/0xJacky/Nginx-UI/internal/middleware"
-	"github.com/gin-contrib/static"
 	"github.com/gin-gonic/gin"
 	"github.com/uozi-tech/cosy"
-	"net/http"
 )
 
 func InitRouter() {
 	r := cosy.GetEngine()
-	r.Use(
-		middleware.CacheJs(),
-		middleware.IPWhiteList(),
-		static.Serve("/", middleware.MustFs("")),
-	)
+
+	initEmbedRoute(r)
 
 	r.NoRoute(func(c *gin.Context) {
 		c.JSON(http.StatusNotFound, gin.H{

+ 17 - 0
router/routers_embed.go

@@ -0,0 +1,17 @@
+//go:build !unembed
+
+package router
+
+import (
+	"github.com/0xJacky/Nginx-UI/internal/middleware"
+	"github.com/gin-contrib/static"
+	"github.com/gin-gonic/gin"
+)
+
+func initEmbedRoute(r *gin.Engine) {
+	r.Use(
+		middleware.CacheJs(),
+		middleware.IPWhiteList(),
+		static.Serve("/", middleware.MustFs("")),
+	)
+}

+ 8 - 0
router/routers_unembed.go

@@ -0,0 +1,8 @@
+//go:build unembed
+
+package router
+
+import "github.com/gin-gonic/gin"
+
+func initEmbedRoute(r *gin.Engine) {
+}