1Panel/core/init/router/router.go

87 lines
2.5 KiB
Go
Raw Normal View History

2024-07-19 19:04:11 +08:00
package router
import (
"fmt"
2024-07-25 14:43:41 +08:00
"net/http"
2024-07-23 14:48:05 +08:00
"github.com/1Panel-dev/1Panel/cmd/server/docs"
"github.com/1Panel-dev/1Panel/cmd/server/web"
2024-07-19 19:04:11 +08:00
"github.com/1Panel-dev/1Panel/core/global"
"github.com/1Panel-dev/1Panel/core/i18n"
"github.com/1Panel-dev/1Panel/core/middleware"
"github.com/1Panel-dev/1Panel/core/router"
2024-07-19 19:04:11 +08:00
rou "github.com/1Panel-dev/1Panel/core/router"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
var (
Router *gin.Engine
)
func setWebStatic(rootRouter *gin.RouterGroup) {
rootRouter.StaticFS("/public", http.FS(web.Favicon))
rootRouter.Static("/api/v2/images", "./uploads")
2024-07-19 19:04:11 +08:00
rootRouter.Use(func(c *gin.Context) {
c.Next()
})
rootRouter.GET("/assets/*filepath", func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", fmt.Sprintf("private, max-age=%d", 3600))
staticServer := http.FileServer(http.FS(web.Assets))
staticServer.ServeHTTP(c.Writer, c.Request)
})
rootRouter.GET("/", func(c *gin.Context) {
staticServer := http.FileServer(http.FS(web.IndexHtml))
staticServer.ServeHTTP(c.Writer, c.Request)
})
}
func Routers() *gin.Engine {
Router = gin.Default()
Router.Use(i18n.UseI18n())
swaggerRouter := Router.Group("1panel")
2024-08-06 16:20:31 +08:00
docs.SwaggerInfo.BasePath = "/api/v2"
swaggerRouter.Use(middleware.JwtAuth()).Use(middleware.SessionAuth()).GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
2024-07-19 19:04:11 +08:00
PublicGroup := Router.Group("")
{
PublicGroup.GET("/health", func(c *gin.Context) {
c.JSON(200, "ok")
})
PublicGroup.Use(gzip.Gzip(gzip.DefaultCompression))
setWebStatic(PublicGroup)
}
2024-07-25 18:13:57 +08:00
agentRouter := Router.Group("agent")
agentRouter.Use(middleware.JwtAuth())
var agent router.AgentRouter
agent.InitRouter(agentRouter)
2024-07-25 18:13:57 +08:00
Router.Use(middleware.OperationLog())
if global.CONF.System.IsDemo {
Router.Use(middleware.DemoHandle())
}
Router.Use(middleware.JwtAuth())
Router.Use(middleware.SessionAuth())
Router.Use(middleware.PasswordExpired())
Router.Use(middleware.Proxy())
2024-07-25 14:43:41 +08:00
PrivateGroup := Router.Group("/api/v2/core")
2024-07-19 19:04:11 +08:00
PrivateGroup.Use(middleware.WhiteAllow())
PrivateGroup.Use(middleware.BindDomain())
PrivateGroup.Use(middleware.GlobalLoading())
for _, router := range rou.RouterGroupApp {
router.InitRouter(PrivateGroup)
}
2024-07-23 18:14:20 +08:00
Router.NoRoute(func(c *gin.Context) {
c.Writer.WriteHeader(http.StatusOK)
_, _ = c.Writer.Write(web.IndexByte)
c.Writer.Header().Add("Accept", "text/html")
c.Writer.Flush()
})
2024-07-19 19:04:11 +08:00
return Router
}