diff --git a/gofi-backend/env/default.go b/gofi-backend/env/default.go index 1b55edb..1fa9e7b 100644 --- a/gofi-backend/env/default.go +++ b/gofi-backend/env/default.go @@ -3,5 +3,9 @@ package env +import "embed" + //current 当前模式 -const current = Development \ No newline at end of file +const current = Development + +var EmbedStaticAssets embed.FS diff --git a/gofi-backend/env/preview.go b/gofi-backend/env/preview.go index 4f0797b..34bacd0 100644 --- a/gofi-backend/env/preview.go +++ b/gofi-backend/env/preview.go @@ -3,3 +3,6 @@ package env const current = Preview + +//go:embed dist/* +var EmbedStaticAssets embed.FS diff --git a/gofi-backend/env/production.go b/gofi-backend/env/production.go index 6a94679..2decee5 100644 --- a/gofi-backend/env/production.go +++ b/gofi-backend/env/production.go @@ -3,3 +3,6 @@ package env const current = Production + +//go:embed dist/* +var EmbedStaticAssets embed.FS diff --git a/gofi-backend/main.go b/gofi-backend/main.go index 83637e0..b874c4b 100644 --- a/gofi-backend/main.go +++ b/gofi-backend/main.go @@ -1,7 +1,6 @@ package main import ( - "embed" "github.com/aviddiviner/gin-limit" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" @@ -14,9 +13,6 @@ import ( "net/http" ) -//go:embed dist/* -var embedStaticAssets embed.FS - func init() { extension.BindAdditionalType() boot.ParseArguments() @@ -37,16 +33,19 @@ func main() { } app.Use(middleware.CORS) - app.Use(middleware.StaticFS("/", "dist", embedStaticAssets)) - app.NoRoute(func(context *gin.Context) { - indexBytes, err := embedStaticAssets.ReadFile("dist/index.html") - if err != nil { - logrus.Fatal(err) - } - context.Writer.Header().Set("Content-Type", "text/html; charset=utf-8") - context.String(http.StatusOK, string(indexBytes)) - }) + if !env.IsDevelop() { + app.Use(middleware.StaticFS("/", "dist", env.EmbedStaticAssets)) + + app.NoRoute(func(context *gin.Context) { + indexBytes, err := env.EmbedStaticAssets.ReadFile("dist/index.html") + if err != nil { + logrus.Fatal(err) + } + context.Writer.Header().Set("Content-Type", "text/html; charset=utf-8") + context.String(http.StatusOK, string(indexBytes)) + }) + } api := app.Group("/api") app.Use(middleware.Language)