feat: build with no embedded front-end files

This commit is contained in:
Hintay 2025-02-04 01:10:29 +09:00
parent b090564a34
commit a560d98acf
No known key found for this signature in database
GPG key ID: 120FC7FF121F2F2D
10 changed files with 91 additions and 35 deletions

View file

@ -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.

10
.idea/nginx-ui.iml generated
View file

@ -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" />

View file

@ -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
app/app_unembed.go Normal file
View file

@ -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"

View file

@ -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
}

View file

@ -2,16 +2,14 @@ package middleware
import (
"encoding/base64"
"github.com/0xJacky/Nginx-UI/app"
"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"
"github.com/0xJacky/Nginx-UI/internal/user"
"github.com/0xJacky/Nginx-UI/settings"
"github.com/gin-gonic/gin"
"github.com/uozi-tech/cosy/logger"
)
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") {

View file

@ -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

View file

@ -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
router/routers_embed.go Normal file
View file

@ -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("")),
)
}

View file

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