Use the gin framework

This commit is contained in:
2025-10-26 14:48:02 +08:00
parent 9e0eb1497b
commit d844403505
29 changed files with 1612 additions and 1858 deletions

View File

@@ -5,30 +5,32 @@ import (
"log"
"net/http"
"networkDev/web"
"github.com/gin-gonic/gin"
)
// RegisterRoutes 聚合注册所有路由
func RegisterRoutes(mux *http.ServeMux) {
registerStaticRoutes(mux)
registerFaviconRoute(mux)
RegisterHomeRoutes(mux)
RegisterAdminRoutes(mux)
func RegisterRoutes(router *gin.Engine) {
registerStaticRoutes(router)
registerFaviconRoute(router)
RegisterHomeRoutes(router)
RegisterAdminRoutes(router)
}
// registerStaticRoutes 注册静态资源路由
// 静态资源服务,将 /static/ 和 /assets/ 映射到嵌入的文件系统
func registerStaticRoutes(mux *http.ServeMux) {
func registerStaticRoutes(router *gin.Engine) {
if fsys, err := web.GetStaticFS(); err == nil {
// 为 /static/ 路径创建子文件系统
if staticSubFS, staticErr := fs.Sub(fsys, "static"); staticErr == nil {
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticSubFS))))
router.StaticFS("/static", http.FS(staticSubFS))
} else {
log.Printf("创建静态资源子文件系统失败: %v", staticErr)
}
// 为 /assets/ 路径创建子文件系统
if assetsSubFS, assetsErr := fs.Sub(fsys, "assets"); assetsErr == nil {
mux.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.FS(assetsSubFS))))
router.StaticFS("/assets", http.FS(assetsSubFS))
} else {
log.Printf("创建资产资源子文件系统失败: %v", assetsErr)
}
@@ -38,9 +40,9 @@ func registerStaticRoutes(mux *http.ServeMux) {
}
// registerFaviconRoute 注册favicon路由
func registerFaviconRoute(mux *http.ServeMux) {
func registerFaviconRoute(router *gin.Engine) {
// 将 /favicon.ico 重定向到 /assets/favicon.svg
mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/assets/favicon.svg", http.StatusMovedPermanently)
router.GET("/favicon.ico", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/assets/favicon.svg")
})
}