mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-12-28 18:36:34 +08:00
refactor: 引入tailwind css & 后端部分非公共包位置调整
This commit is contained in:
@@ -2,46 +2,33 @@ package initialize
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"mayfly-go/pkg/config"
|
||||
"mayfly-go/pkg/ioc"
|
||||
"mayfly-go/pkg/middleware"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/static"
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// RouterApi
|
||||
// 该接口的实现类注册到ioc中,则会自动将请求配置注册到路由中
|
||||
type RouterApi interface {
|
||||
// ReqConfs 获取请求配置信息
|
||||
ReqConfs() *req.Confs
|
||||
}
|
||||
|
||||
func InitRouter() *gin.Engine {
|
||||
// server配置
|
||||
serverConfig := config.Conf.Server
|
||||
gin.SetMode(serverConfig.Model)
|
||||
|
||||
var router = gin.New()
|
||||
router.MaxMultipartMemory = 8 << 20
|
||||
type RouterConfig struct {
|
||||
ContextPath string // 请求路径上下文
|
||||
}
|
||||
|
||||
func InitRouter(router *gin.Engine, conf RouterConfig) *gin.Engine {
|
||||
// 没有路由即 404返回
|
||||
router.NoRoute(func(g *gin.Context) {
|
||||
g.JSON(http.StatusNotFound, gin.H{"code": 404, "msg": fmt.Sprintf("not found '%s:%s'", g.Request.Method, g.Request.URL.Path)})
|
||||
})
|
||||
|
||||
// 设置静态资源
|
||||
setStatic(serverConfig.ContextPath, router)
|
||||
|
||||
// 是否允许跨域
|
||||
if serverConfig.Cors {
|
||||
router.Use(middleware.Cors())
|
||||
}
|
||||
|
||||
// 设置路由组
|
||||
api := router.Group(serverConfig.ContextPath + "/api")
|
||||
api := router.Group(conf.ContextPath + "/api")
|
||||
|
||||
// 获取所有实现了RouterApi接口的实例,并注册对应路由
|
||||
ras := ioc.GetBeansByType[RouterApi](reflect.TypeOf((*RouterApi)(nil)).Elem())
|
||||
@@ -56,36 +43,3 @@ func InitRouter() *gin.Engine {
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
func setStatic(contextPath string, router *gin.Engine) {
|
||||
// 使用embed打包静态资源至二进制文件中
|
||||
fsys, _ := fs.Sub(static.Static, "static")
|
||||
fileServer := http.FileServer(http.FS(fsys))
|
||||
handler := WrapStaticHandler(http.StripPrefix(contextPath, fileServer))
|
||||
|
||||
router.GET(contextPath+"/", handler)
|
||||
router.GET(contextPath+"/favicon.ico", handler)
|
||||
router.GET(contextPath+"/config.js", handler)
|
||||
// 所有/assets/**开头的都是静态资源文件
|
||||
router.GET(contextPath+"/assets/*file", handler)
|
||||
|
||||
// 设置静态资源
|
||||
if staticConfs := config.Conf.Server.Static; staticConfs != nil {
|
||||
for _, scs := range *staticConfs {
|
||||
router.StaticFS(scs.RelativePath, http.Dir(scs.Root))
|
||||
}
|
||||
}
|
||||
// 设置静态文件
|
||||
if staticFileConfs := config.Conf.Server.StaticFile; staticFileConfs != nil {
|
||||
for _, sfs := range *staticFileConfs {
|
||||
router.StaticFile(sfs.RelativePath, sfs.Filepath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func WrapStaticHandler(h http.Handler) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.Writer.Header().Set("Cache-Control", `public, max-age=31536000`)
|
||||
h.ServeHTTP(c.Writer, c.Request)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user