build: make frontend embedding optional
This commit is contained in:
@@ -8,7 +8,7 @@ tmp_dir = "bin"
|
|||||||
cmd = ":"
|
cmd = ":"
|
||||||
full_bin = "go run main.go server"
|
full_bin = "go run main.go server"
|
||||||
delay = 1000
|
delay = 1000
|
||||||
exclude_dir = ["assets", "tmp", "vendor", "testdata", "web", "data", "envs", "configs", "bin"]
|
exclude_dir = ["internal/static/dist", "assets", "tmp", "vendor", "testdata", "web", "data", "envs", "configs", "bin"]
|
||||||
exclude_file = []
|
exclude_file = []
|
||||||
exclude_regex = ["_test.go"]
|
exclude_regex = ["_test.go"]
|
||||||
exclude_unchanged = false
|
exclude_unchanged = false
|
||||||
|
|||||||
+1
-3
@@ -22,8 +22,7 @@ configs/config.ini
|
|||||||
agent/config.ini
|
agent/config.ini
|
||||||
agent/agent.pid
|
agent/agent.pid
|
||||||
agent/baihu-agent
|
agent/baihu-agent
|
||||||
web/dist/*
|
web/dist/
|
||||||
!web/dist/.gitkeep
|
|
||||||
|
|
||||||
|
|
||||||
# IDE
|
# IDE
|
||||||
@@ -38,7 +37,6 @@ node_modules/
|
|||||||
|
|
||||||
# Embedded static files (built during CI/Docker)
|
# Embedded static files (built during CI/Docker)
|
||||||
internal/static/dist/
|
internal/static/dist/
|
||||||
!internal/static/dist/.gitkeep
|
|
||||||
|
|
||||||
# Config (sensitive)
|
# Config (sensitive)
|
||||||
configs/config.local.json
|
configs/config.local.json
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ VERSION=$(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
|
|||||||
BUILD_TIME=$(shell date '+%Y/%m/%d %H:%M:%S')
|
BUILD_TIME=$(shell date '+%Y/%m/%d %H:%M:%S')
|
||||||
LDFLAGS=-ldflags="-s -w -X 'github.com/engigu/baihu-panel/internal/constant.Version=$(VERSION)' -X 'github.com/engigu/baihu-panel/internal/constant.BuildTime=$(BUILD_TIME)'"
|
LDFLAGS=-ldflags="-s -w -X 'github.com/engigu/baihu-panel/internal/constant.Version=$(VERSION)' -X 'github.com/engigu/baihu-panel/internal/constant.BuildTime=$(BUILD_TIME)'"
|
||||||
|
|
||||||
|
TAGS_WEB=-tags web
|
||||||
|
|
||||||
DEV_UID ?= $(shell id -u 2>/dev/null || echo 1000)
|
DEV_UID ?= $(shell id -u 2>/dev/null || echo 1000)
|
||||||
DEV_GID ?= $(shell id -g 2>/dev/null || echo 1000)
|
DEV_GID ?= $(shell id -g 2>/dev/null || echo 1000)
|
||||||
export DEV_UID
|
export DEV_UID
|
||||||
@@ -19,16 +21,21 @@ all: build
|
|||||||
# Build frontend
|
# Build frontend
|
||||||
build-web:
|
build-web:
|
||||||
cd web && npm ci && npm run build
|
cd web && npm ci && npm run build
|
||||||
rm -rf internal/static/dist
|
|
||||||
cp -r web/dist internal/static/dist
|
|
||||||
|
|
||||||
# Build the application (requires frontend to be built first)
|
# Build the application (requires frontend to be built first)
|
||||||
build:
|
build:
|
||||||
@mkdir -p bin
|
@mkdir -p bin
|
||||||
CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) -o $(BINARY) main.go
|
CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) -o $(BINARY) main.go
|
||||||
|
|
||||||
# Build all (frontend + backend)
|
# Build release version (Frontend + Backend with embedded assets)
|
||||||
build-all: build-web build
|
release: build-web
|
||||||
|
@mkdir -p bin
|
||||||
|
rm -rf internal/static/dist
|
||||||
|
cp -r web/dist internal/static/dist
|
||||||
|
CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) $(TAGS_WEB) -o $(BINARY) main.go
|
||||||
|
|
||||||
|
# Alias for backward compatibility
|
||||||
|
build-all: release
|
||||||
|
|
||||||
# Build agent for all platforms
|
# Build agent for all platforms
|
||||||
build-agent: build-agent-linux-amd64 build-agent-linux-arm64 build-agent-windows-amd64 build-agent-darwin-amd64 build-agent-darwin-arm64
|
build-agent: build-agent-linux-amd64 build-agent-linux-arm64 build-agent-windows-amd64 build-agent-darwin-amd64 build-agent-darwin-arm64
|
||||||
@@ -67,14 +74,11 @@ clean:
|
|||||||
$(GOCLEAN)
|
$(GOCLEAN)
|
||||||
rm -rf bin/
|
rm -rf bin/
|
||||||
rm -rf internal/static/dist
|
rm -rf internal/static/dist
|
||||||
mkdir -p internal/static/dist
|
|
||||||
touch internal/static/dist/.gitkeep
|
|
||||||
rm -rf web/dist
|
rm -rf web/dist
|
||||||
mkdir -p web/dist
|
|
||||||
touch web/dist/.gitkeep
|
|
||||||
|
|
||||||
# Clean everything: local artifacts and Docker development environment (including volumes)
|
# Clean everything: local artifacts and Docker development environment (including volumes)
|
||||||
clean-all: clean docker-dev-clean
|
clean-all: clean docker-dev-clean
|
||||||
|
rm -rf web/node_modules
|
||||||
@echo "All local artifacts and Docker dev caches have been completely wiped."
|
@echo "All local artifacts and Docker dev caches have been completely wiped."
|
||||||
|
|
||||||
# Run the application
|
# Run the application
|
||||||
@@ -143,8 +147,10 @@ docker-dev-clean:
|
|||||||
# Help
|
# Help
|
||||||
help:
|
help:
|
||||||
@echo "Available targets:"
|
@echo "Available targets:"
|
||||||
@echo " all - Build the application (default)"
|
@echo " all - Build backend only (default)"
|
||||||
@echo " build - Build the application"
|
@echo " build - Build backend binary (no UI embedded)"
|
||||||
|
@echo " release - Build full release binary (with UI embedded)"
|
||||||
|
@echo " build-web - Build frontend assets only"
|
||||||
@echo " build-agent - Build agent packages (tar.gz) for all platforms"
|
@echo " build-agent - Build agent packages (tar.gz) for all platforms"
|
||||||
@echo " clean - Clean built files"
|
@echo " clean - Clean built files"
|
||||||
@echo " clean-all - Clean local files and Docker dev environment (including volumes)"
|
@echo " clean-all - Clean local files and Docker dev environment (including volumes)"
|
||||||
|
|||||||
@@ -64,10 +64,11 @@ func Setup(c *Controllers) *gin.Engine {
|
|||||||
root = router.Group("")
|
root = router.Group("")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 静态资源服务(Vue SPA),带缓存头部
|
|
||||||
staticFS := static.GetFS()
|
staticFS := static.GetFS()
|
||||||
|
if staticFS != nil {
|
||||||
|
// 静态资源服务(Vue SPA),带缓存头部
|
||||||
assetsGroup := root.Group("/assets")
|
assetsGroup := root.Group("/assets")
|
||||||
assetsGroup.Use(cacheControl("public, max-age=31536000, immutable")) // 带哈希的资源缓存1年
|
assetsGroup.Use(cacheControl("public, max-age=31536000, immutable")) // 带哈希的资源缓存
|
||||||
assetsGroup.StaticFS("/", http.FS(mustSubFS(staticFS, "assets")))
|
assetsGroup.StaticFS("/", http.FS(mustSubFS(staticFS, "assets")))
|
||||||
|
|
||||||
// logo.svg 短缓存实现
|
// logo.svg 短缓存实现
|
||||||
@@ -80,6 +81,7 @@ func Setup(c *Controllers) *gin.Engine {
|
|||||||
ctx.Header("Cache-Control", "public, max-age=86400") // 缓存1天
|
ctx.Header("Cache-Control", "public, max-age=86400") // 缓存1天
|
||||||
ctx.Data(200, "image/svg+xml", data)
|
ctx.Data(200, "image/svg+xml", data)
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// API 路由组
|
// API 路由组
|
||||||
api := root.Group("/api/v1")
|
api := root.Group("/api/v1")
|
||||||
|
|||||||
@@ -1,28 +1,20 @@
|
|||||||
|
//go:build web
|
||||||
|
|
||||||
package static
|
package static
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed dist/*
|
//go:embed dist/*
|
||||||
var distFS embed.FS
|
var distFS embed.FS
|
||||||
|
|
||||||
// GetFileSystem 返回嵌入的静态文件系统
|
|
||||||
func GetFileSystem() http.FileSystem {
|
|
||||||
subFS, err := fs.Sub(distFS, "dist")
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return http.FS(subFS)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetFS 返回嵌入的 fs.FS
|
// GetFS 返回嵌入的 fs.FS
|
||||||
func GetFS() fs.FS {
|
func GetFS() fs.FS {
|
||||||
subFS, err := fs.Sub(distFS, "dist")
|
subFS, err := fs.Sub(distFS, "dist")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return nil
|
||||||
}
|
}
|
||||||
return subFS
|
return subFS
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
//go:build !web
|
||||||
|
|
||||||
|
package static
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetFS() fs.FS {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadFile(name string) ([]byte, error) {
|
||||||
|
return nil, os.ErrNotExist
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user