mirror of
https://github.com/skyle1995/NetworkAuth.git
synced 2026-05-25 02:24:05 +08:00
修改前端嵌入方案
This commit is contained in:
10
CHANGELOG.md
10
CHANGELOG.md
@@ -1,5 +1,15 @@
|
||||
# 更新日志
|
||||
|
||||
## 2026-05-24
|
||||
|
||||
### 登录
|
||||
|
||||
- [2026-05-24] [修正] 登录态判定改为仅依赖 localStorage 中 token + expires,不再依赖 multiple-tabs cookie,浏览器重启不再误清登录态。
|
||||
|
||||
### 前端资源
|
||||
|
||||
- [2026-05-24] [调整] 后端静态资源嵌入改为 go:embed frontend/dist,由 main.go 注入 fs.FS 至 server 挂载;移除 public/public.go 依赖,构建不再需要拷贝到 public/dist。
|
||||
|
||||
## 2026-05-17
|
||||
|
||||
### 前端布局
|
||||
|
||||
15
build.sh
15
build.sh
@@ -186,7 +186,7 @@ get_sha256_hex() {
|
||||
return 1
|
||||
}
|
||||
|
||||
# 编译并拷贝前端
|
||||
# 编译前端
|
||||
build_frontend() {
|
||||
echo -e "${BLUE}=====================================${NC}"
|
||||
echo -e "${YELLOW}🚀 开始编译前端项目...${NC}"
|
||||
@@ -197,17 +197,6 @@ build_frontend() {
|
||||
pnpm run build
|
||||
cd ..
|
||||
echo -e "${GREEN}✅ 前端编译完成!${NC}\n"
|
||||
|
||||
echo -e "${BLUE}=====================================${NC}"
|
||||
echo -e "${YELLOW}📂 正在将前端代码拷贝到后端 public 目录...${NC}"
|
||||
echo -e "${BLUE}=====================================${NC}"
|
||||
# 清理旧的静态文件
|
||||
rm -rf public/dist
|
||||
# 确保 public 目录存在
|
||||
mkdir -p public
|
||||
# 将前端新编译的 dist 目录整个复制到 public 下 (使用 -R 增强跨平台兼容性)
|
||||
cp -R frontend/dist public/
|
||||
echo -e "${GREEN}✅ 前端代码拷贝完成!${NC}\n"
|
||||
}
|
||||
|
||||
# 编译后端指定架构
|
||||
@@ -267,7 +256,7 @@ show_menu() {
|
||||
echo -e "1. 🔁 自动构建 + 生成更新包"
|
||||
echo -e "2. 🚀 全部构建 (前端 + 后端)"
|
||||
echo -e "3. 📦 编译所有后端架构"
|
||||
echo -e "4. 🌐 仅编译前端并拷贝"
|
||||
echo -e "4. 🌐 仅编译前端"
|
||||
echo -e "-------------------------------------"
|
||||
echo -e "0. ❌ 退出"
|
||||
echo -e "${BLUE}=====================================${NC}"
|
||||
|
||||
15
main.go
15
main.go
@@ -1,9 +1,22 @@
|
||||
package main
|
||||
|
||||
import "NetworkAuth/cmd"
|
||||
import (
|
||||
"NetworkAuth/cmd"
|
||||
"NetworkAuth/server"
|
||||
"embed"
|
||||
"io/fs"
|
||||
)
|
||||
|
||||
//go:embed all:frontend/dist
|
||||
var embeddedFrontendDist embed.FS
|
||||
|
||||
// main 是程序的入口点
|
||||
// 调用Cobra命令执行器来处理命令行参数和子命令
|
||||
func main() {
|
||||
distFS, err := fs.Sub(embeddedFrontendDist, "frontend/dist")
|
||||
if err != nil {
|
||||
panic("Failed to initialize embedded static files: " + err.Error())
|
||||
}
|
||||
server.SetFrontendFS(distFS)
|
||||
cmd.Execute()
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
package public
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed all:dist
|
||||
var Public embed.FS
|
||||
@@ -1,7 +1,6 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"NetworkAuth/public"
|
||||
"NetworkAuth/utils"
|
||||
"io"
|
||||
"io/fs"
|
||||
@@ -16,6 +15,12 @@ import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var frontendFS fs.FS
|
||||
|
||||
func SetFrontendFS(fsys fs.FS) {
|
||||
frontendFS = fsys
|
||||
}
|
||||
|
||||
// RegisterRoutes 聚合注册所有路由
|
||||
func RegisterRoutes(r *gin.Engine) {
|
||||
// 1. 所有接口路由基于 /api
|
||||
@@ -93,14 +98,12 @@ func registerFrontendRoutes(r *gin.Engine) {
|
||||
}
|
||||
}
|
||||
|
||||
// 提取嵌入的 dist 目录 (默认方式)
|
||||
distFS, err := fs.Sub(public.Public, "dist")
|
||||
if err != nil {
|
||||
panic("Failed to initialize embedded static files: " + err.Error())
|
||||
if frontendFS == nil {
|
||||
panic("Failed to initialize embedded static files: frontend fs is nil")
|
||||
}
|
||||
|
||||
// 提供静态文件服务器
|
||||
fileServer = http.FileServer(http.FS(distFS))
|
||||
fileServer = http.FileServer(http.FS(frontendFS))
|
||||
|
||||
// 拦截并处理静态资源请求
|
||||
r.Use(func(c *gin.Context) {
|
||||
@@ -118,8 +121,8 @@ func registerFrontendRoutes(r *gin.Engine) {
|
||||
cleanPath = "index.html"
|
||||
}
|
||||
|
||||
// 尝试在嵌入的文件系统中查找文件
|
||||
if _, err := fs.Stat(distFS, cleanPath); err == nil {
|
||||
// 尝试在嵌入的文件系统中查找文件
|
||||
if _, err := fs.Stat(frontendFS, cleanPath); err == nil {
|
||||
// 文件存在,交由 FileServer 处理
|
||||
// 设置一些常见的缓存头
|
||||
if strings.HasPrefix(path, "/static/") || strings.HasPrefix(path, "/assets/") {
|
||||
@@ -146,7 +149,7 @@ func registerFrontendRoutes(r *gin.Engine) {
|
||||
|
||||
// 其他所有非 API 请求,都返回 index.html 交给前端 Vue Router 处理
|
||||
c.Header("Content-Type", "text/html; charset=utf-8")
|
||||
indexFile, err := distFS.Open("index.html")
|
||||
indexFile, err := frontendFS.Open("index.html")
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Failed to load index.html")
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user