- 删除Dockerfile中不必要的mailcap包以精简镜像 - 将前端配置从process.env改为import.meta.env以适配现代构建工具 - 移除Go代码中手动设置mime类型的冗余逻辑 - 修复静态资源路由路径格式(/assets改为assets/) - 优化导入语句格式和代码结构
This commit is contained in:
+1
-1
@@ -44,7 +44,7 @@ FROM debian:stable-slim
|
|||||||
ENV TZ=Asia/Shanghai
|
ENV TZ=Asia/Shanghai
|
||||||
|
|
||||||
RUN apt-get update \
|
RUN apt-get update \
|
||||||
&& apt-get install -y ca-certificates tzdata mailcap \
|
&& apt-get install -y ca-certificates tzdata \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
"message-nest/routers"
|
"message-nest/routers"
|
||||||
"message-nest/service/cron_msg_service"
|
"message-nest/service/cron_msg_service"
|
||||||
"message-nest/service/cron_service"
|
"message-nest/service/cron_service"
|
||||||
"mime"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
@@ -20,7 +19,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
//go:embed all:web/dist
|
//go:embed web/dist/*
|
||||||
f embed.FS
|
f embed.FS
|
||||||
|
|
||||||
//go:embed .release*
|
//go:embed .release*
|
||||||
@@ -28,10 +27,6 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// 设置 mime 类型,防止 docker 镜像中缺少 mime 类型导致 js 文件加载失败
|
|
||||||
mime.AddExtensionType(".js", "application/javascript")
|
|
||||||
mime.AddExtensionType(".css", "text/css")
|
|
||||||
|
|
||||||
constant.InitReleaseInfo(rf)
|
constant.InitReleaseInfo(rf)
|
||||||
setting.Setup()
|
setting.Setup()
|
||||||
logging.Setup()
|
logging.Setup()
|
||||||
|
|||||||
+11
-10
@@ -3,16 +3,17 @@ package routers
|
|||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"message-nest/middleware"
|
"message-nest/middleware"
|
||||||
"message-nest/pkg/setting"
|
"message-nest/pkg/setting"
|
||||||
"message-nest/routers/api"
|
"message-nest/routers/api"
|
||||||
"message-nest/routers/api/v1"
|
v1 "message-nest/routers/api/v1"
|
||||||
"message-nest/routers/api/v2"
|
v2 "message-nest/routers/api/v2"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AppendCors 添加是否跨域(debug模式开启)
|
// AppendCors 添加是否跨域(debug模式开启)
|
||||||
@@ -54,12 +55,12 @@ func AppendServerStaticHtmlWithPrefix(router gin.IRouter, f embed.FS, pathPrefix
|
|||||||
}
|
}
|
||||||
|
|
||||||
htmlContent := string(content)
|
htmlContent := string(content)
|
||||||
|
|
||||||
// 注入 base 标签和配置脚本
|
// 注入 base 标签和配置脚本
|
||||||
// base 标签必须在 head 的最前面,确保所有相对路径都基于这个 base
|
// base 标签必须在 head 的最前面,确保所有相对路径都基于这个 base
|
||||||
baseTag := fmt.Sprintf(`<base href="%s/">`, pathPrefix)
|
baseTag := fmt.Sprintf(`<base href="%s/">`, pathPrefix)
|
||||||
configScript := fmt.Sprintf(`<script>window.__URL_PATH_PREFIX__ = '%s';</script>`, pathPrefix)
|
configScript := fmt.Sprintf(`<script>window.__URL_PATH_PREFIX__ = '%s';</script>`, pathPrefix)
|
||||||
|
|
||||||
// 在 <head> 标签后立即插入 base 标签
|
// 在 <head> 标签后立即插入 base 标签
|
||||||
htmlContent = strings.Replace(htmlContent, "<head>", "<head>"+baseTag, 1)
|
htmlContent = strings.Replace(htmlContent, "<head>", "<head>"+baseTag, 1)
|
||||||
// 在 </head> 标签前注入配置
|
// 在 </head> 标签前注入配置
|
||||||
@@ -73,7 +74,7 @@ func AppendServerStaticHtmlWithPrefix(router gin.IRouter, f embed.FS, pathPrefix
|
|||||||
// 无路径前缀时,使用原有逻辑
|
// 无路径前缀时,使用原有逻辑
|
||||||
if r, ok := router.(*gin.Engine); ok {
|
if r, ok := router.(*gin.Engine); ok {
|
||||||
r.Use(middleware.StaticCacheMiddleware())
|
r.Use(middleware.StaticCacheMiddleware())
|
||||||
r.StaticFS("/assets", http.FS(assets))
|
r.StaticFS("assets/", http.FS(assets))
|
||||||
r.GET("/", func(ctx *gin.Context) {
|
r.GET("/", func(ctx *gin.Context) {
|
||||||
ctx.FileFromFS("/", http.FS(dist))
|
ctx.FileFromFS("/", http.FS(dist))
|
||||||
})
|
})
|
||||||
@@ -93,13 +94,13 @@ func InitRouter(f embed.FS) *gin.Engine {
|
|||||||
app.Use(gin.Recovery())
|
app.Use(gin.Recovery())
|
||||||
|
|
||||||
AppendCors(app)
|
AppendCors(app)
|
||||||
|
|
||||||
// 获取 URL 前缀
|
// 获取 URL 前缀
|
||||||
pathPrefix := setting.ServerSetting.UrlPrefix
|
pathPrefix := setting.ServerSetting.UrlPrefix
|
||||||
if pathPrefix != "" && pathPrefix[0] != '/' {
|
if pathPrefix != "" && pathPrefix[0] != '/' {
|
||||||
pathPrefix = "/" + pathPrefix
|
pathPrefix = "/" + pathPrefix
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果有路径前缀,创建路由组
|
// 如果有路径前缀,创建路由组
|
||||||
var router gin.IRouter
|
var router gin.IRouter
|
||||||
if pathPrefix != "" {
|
if pathPrefix != "" {
|
||||||
@@ -107,7 +108,7 @@ func InitRouter(f embed.FS) *gin.Engine {
|
|||||||
} else {
|
} else {
|
||||||
router = app
|
router = app
|
||||||
}
|
}
|
||||||
|
|
||||||
AppendServerStaticHtmlWithPrefix(router, f, pathPrefix)
|
AppendServerStaticHtmlWithPrefix(router, f, pathPrefix)
|
||||||
|
|
||||||
router.POST("/auth", api.GetAuth)
|
router.POST("/auth", api.GetAuth)
|
||||||
@@ -171,7 +172,7 @@ func InitRouter(f embed.FS) *gin.Engine {
|
|||||||
apiV1.POST("/templates/edit", v1.EditMessageTemplate)
|
apiV1.POST("/templates/edit", v1.EditMessageTemplate)
|
||||||
apiV1.POST("/templates/delete", v1.DeleteMessageTemplate)
|
apiV1.POST("/templates/delete", v1.DeleteMessageTemplate)
|
||||||
apiV1.POST("/templates/preview", v1.PreviewMessageTemplate)
|
apiV1.POST("/templates/preview", v1.PreviewMessageTemplate)
|
||||||
|
|
||||||
// messageTemplate instances
|
// messageTemplate instances
|
||||||
apiV1.GET("/templates/ins/get", v1.GetTemplateWithIns)
|
apiV1.GET("/templates/ins/get", v1.GetTemplateWithIns)
|
||||||
apiV1.POST("/templates/ins/addone", v1.AddTemplateIns)
|
apiV1.POST("/templates/ins/addone", v1.AddTemplateIns)
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
// config.js
|
// config.js
|
||||||
|
|
||||||
const isProduction = process.env.NODE_ENV === 'prod' || process.env.NODE_ENV === 'production';
|
const isProduction = import.meta.env.PROD;
|
||||||
|
|
||||||
// 从 window 对象获取路径前缀(由后端注入或通过 API 获取)
|
// 从 window 对象获取路径前缀(由后端注入或通过 API 获取)
|
||||||
const getPathPrefix = () => {
|
const getPathPrefix = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user