refactor(serve): 重构静态文件服务实现
This commit is contained in:
24
main.go
24
main.go
@@ -6,9 +6,10 @@ import (
|
|||||||
"dypid/db"
|
"dypid/db"
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-contrib/cors"
|
"github.com/gin-contrib/cors"
|
||||||
"github.com/gin-contrib/static"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,12 +26,17 @@ func main() {
|
|||||||
//跨域设置
|
//跨域设置
|
||||||
r.Use(cors.Default())
|
r.Use(cors.Default())
|
||||||
|
|
||||||
//静态文件目录
|
//Vue网站服务
|
||||||
web, err := static.EmbedFolder(webDir, "web/dist")
|
assets, _ := fs.Sub(webDir, "web/dist/assets")
|
||||||
if err != nil {
|
r.StaticFS("/assets", http.FS(assets))
|
||||||
panic(err)
|
icon, _ := fs.ReadFile(webDir, "web/dist/favicon.ico")
|
||||||
}
|
r.GET("/favicon.ico", func(c *gin.Context) {
|
||||||
r.Use(static.Serve("/", web))
|
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接口
|
//API接口
|
||||||
g := r.Group("/api") //初始化路由组 /api/xxxx
|
g := r.Group("/api") //初始化路由组 /api/xxxx
|
||||||
@@ -48,8 +54,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 监听并在 0.0.0.0:8080 上启动服务
|
// 监听并在 0.0.0.0:8080 上启动服务
|
||||||
fmt.Printf("服务器正在%s运行\n", config.APPConfig.Host)
|
fmt.Printf("服务器正在运行:http://%s\n", config.APPConfig.Host)
|
||||||
err = r.Run(config.APPConfig.Host)
|
err := r.Run(config.APPConfig.Host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("服务启动失败:", err)
|
fmt.Println("服务启动失败:", err)
|
||||||
return
|
return
|
||||||
|
Reference in New Issue
Block a user