From b47e1fa8816bb62ec24dbbe415b1c8fdd0e87ef5 Mon Sep 17 00:00:00 2001 From: YGXB_net Date: Tue, 2 Sep 2025 12:33:34 +0800 Subject: [PATCH] =?UTF-8?q?refactor(serve):=20=E9=87=8D=E6=9E=84=E9=9D=99?= =?UTF-8?q?=E6=80=81=E6=96=87=E4=BB=B6=E6=9C=8D=E5=8A=A1=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index f2f4036..04937ed 100644 --- a/main.go +++ b/main.go @@ -6,9 +6,10 @@ import ( "dypid/db" "embed" "fmt" + "io/fs" + "net/http" "github.com/gin-contrib/cors" - "github.com/gin-contrib/static" "github.com/gin-gonic/gin" ) @@ -25,12 +26,17 @@ func main() { //跨域设置 r.Use(cors.Default()) - //静态文件目录 - web, err := static.EmbedFolder(webDir, "web/dist") - if err != nil { - panic(err) - } - r.Use(static.Serve("/", web)) + //Vue网站服务 + assets, _ := fs.Sub(webDir, "web/dist/assets") + r.StaticFS("/assets", http.FS(assets)) + icon, _ := fs.ReadFile(webDir, "web/dist/favicon.ico") + r.GET("/favicon.ico", func(c *gin.Context) { + c.Data(200, "image/x-icon", icon) + }) + indexHtml, _ := fs.ReadFile(webDir, "web/dist/index.html") + r.NoRoute(func(c *gin.Context) { + c.Data(200, "text/html; charset=utf-8", indexHtml) + }) //API接口 g := r.Group("/api") //初始化路由组 /api/xxxx @@ -48,8 +54,8 @@ func main() { } // 监听并在 0.0.0.0:8080 上启动服务 - fmt.Printf("服务器正在%s运行\n", config.APPConfig.Host) - err = r.Run(config.APPConfig.Host) + fmt.Printf("服务器正在运行:http://%s\n", config.APPConfig.Host) + err := r.Run(config.APPConfig.Host) if err != nil { fmt.Println("服务启动失败:", err) return