refactor(serve): 重构静态文件服务实现
This commit is contained in:
24
main.go
24
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
|
||||
|
Reference in New Issue
Block a user