feat:new ui load static

This commit is contained in:
samwaf
2024-09-04 15:27:19 +08:00
parent b8fc39ce74
commit 498487caa8
5 changed files with 91 additions and 57 deletions

4
.gitignore vendored
View File

@@ -11,4 +11,6 @@
/data/local_stats.db-wal
registration_data.bin
dist/
.idea/
.idea/
/public/dist/*
!*.gitkeep

View File

@@ -50,6 +50,7 @@ var (
GWAF_LOCAL_STATS_DB *gorm.DB //通用本地数据库存放统计数据,尊重用户隐私
GWAF_REMOTE_DB *gorm.DB //仅当用户使用云数据库
GWAF_LOCAL_SERVER_PORT int = 26666 // 本地local端口
GWAF_LOCAL_INDEX_HTML string //本地首页HTML信息
GWAF_USER_CODE string // 当前识别号
GWAF_CUSTOM_SERVER_NAME string // 当前服务器自定义名称
GWAF_TENANT_ID string = "SamWafCom" // 当前租户ID

11
public/public.go Normal file
View File

@@ -0,0 +1,11 @@
package public
import (
"embed"
"io/fs"
)
//go:embed all:dist
var dist embed.FS
var Public, _ = fs.Sub(dist, "dist")

View File

@@ -4,10 +4,9 @@ import (
"SamWaf/global"
"SamWaf/middleware"
"SamWaf/router"
"SamWaf/vue"
"SamWaf/wafmangeweb/static"
"context"
"errors"
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/gin-gonic/gin"
"log"
"net/http"
@@ -53,9 +52,12 @@ func (web *WafWebManager) initRouter(r *gin.Engine) {
router.ApiGroupApp.InitCenterRouter(RouterGroup)
router.ApiGroupApp.InitLicenseRouter(RouterGroup)
}
//TODO 中心管控 特定
//RouterGroup.Use(middleware.CenterApi())
r.Use(middleware.GinGlobalExceptionMiddleWare())
if global.GWAF_RELEASE == "true" {
static.Static(r, func(handlers ...gin.HandlerFunc) {
r.NoRoute(handlers...)
})
}
}
func (web *WafWebManager) cors() gin.HandlerFunc {
@@ -85,9 +87,6 @@ func (web *WafWebManager) StartLocalServer() {
r := gin.Default()
r.Use(web.cors()) //解决跨域
if global.GWAF_RELEASE == "true" {
web.index(r)
}
web.initRouter(r)
web.R = r
@@ -119,52 +118,3 @@ func (web *WafWebManager) CloseLocalServer() {
log.Println("local Server exiting")
}
// vue静态路由
func (web *WafWebManager) index(r *gin.Engine) *gin.Engine {
//静态文件路径
const staticPath = `vue/dist/`
var (
js = assetfs.AssetFS{
Asset: vue.Asset,
AssetDir: vue.AssetDir,
AssetInfo: nil,
Prefix: staticPath + "assets",
Fallback: "index.html",
}
fs = assetfs.AssetFS{
Asset: vue.Asset,
AssetDir: vue.AssetDir,
AssetInfo: nil,
Prefix: staticPath,
Fallback: "index.html",
}
)
// 加载静态文件
r.StaticFS("/assets", &js)
r.StaticFS("/favicon.ico", &fs)
r.GET("/", func(c *gin.Context) {
//设置响应状态
c.Writer.WriteHeader(http.StatusOK)
//载入首页
indexHTML, _ := vue.Asset(staticPath + "index.html")
c.Writer.Write(indexHTML)
//响应HTML类型
c.Writer.Header().Add("Accept", "text/html")
//显示刷新
c.Writer.Flush()
})
// 关键点【解决页面刷新404的问题】
r.NoRoute(func(c *gin.Context) {
//设置响应状态
c.Writer.WriteHeader(http.StatusOK)
//载入首页
indexHTML, _ := vue.Asset(staticPath + "index.html")
c.Writer.Write(indexHTML)
//响应HTML类型
c.Writer.Header().Add("Accept", "text/html")
//显示刷新
c.Writer.Flush()
})
return r
}

View File

@@ -0,0 +1,70 @@
package static
import (
"SamWaf/global"
"SamWaf/public"
"SamWaf/utils/zlog"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"io"
"io/fs"
"net/http"
)
var static fs.FS
func initStatic() {
static = public.Public
return
}
func initIndex() {
indexFile, err := static.Open("index.html")
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
zlog.Error("index.html not exist, you may forget to put dist of frontend to public/dist")
}
zlog.Error("failed to read index.html: %v", err)
}
defer func() {
_ = indexFile.Close()
}()
index, err := io.ReadAll(indexFile)
if err != nil {
zlog.Error("failed to read dist/index.html")
}
global.GWAF_LOCAL_INDEX_HTML = string(index)
}
func Static(r *gin.Engine, noRoute func(handlers ...gin.HandlerFunc)) {
initStatic()
initIndex()
folders := []string{"assets"}
for i, folder := range folders {
sub, err := fs.Sub(static, folder)
if err != nil {
zlog.Error("can't find folder: %s", folder)
}
r.StaticFS(fmt.Sprintf("/%s", folders[i]), http.FS(sub))
}
r.GET("/favicon.ico", func(c *gin.Context) {
faviconFile, err := static.Open("favicon.ico")
if err != nil {
zlog.Error("can't find favicon.ico")
c.Status(http.StatusNotFound)
return
}
defer faviconFile.Close()
c.Header("Content-Type", "image/x-icon")
c.Status(http.StatusOK)
_, _ = io.Copy(c.Writer, faviconFile)
})
noRoute(func(c *gin.Context) {
c.Header("Content-Type", "text/html")
c.Status(200)
_, _ = c.Writer.WriteString(global.GWAF_LOCAL_INDEX_HTML)
c.Writer.Flush()
c.Writer.WriteHeaderNow()
})
}