2022-08-16 23:30:23 +08:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2023-12-11 14:20:06 +08:00
|
|
|
"fmt"
|
2023-03-10 10:54:47 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/i18n"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/middleware"
|
|
|
|
rou "github.com/1Panel-dev/1Panel/backend/router"
|
2022-12-14 19:39:32 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/cmd/server/docs"
|
2022-10-17 17:13:47 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/cmd/server/web"
|
2023-12-20 22:44:09 +08:00
|
|
|
"github.com/gin-contrib/gzip"
|
2022-08-16 23:30:23 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
swaggerfiles "github.com/swaggo/files"
|
|
|
|
ginSwagger "github.com/swaggo/gin-swagger"
|
2023-12-20 22:44:09 +08:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
Router *gin.Engine
|
2022-08-16 23:30:23 +08:00
|
|
|
)
|
|
|
|
|
2023-05-30 00:00:56 +08:00
|
|
|
func setWebStatic(rootRouter *gin.RouterGroup) {
|
2023-06-07 16:31:25 +08:00
|
|
|
rootRouter.StaticFS("/public", http.FS(web.Favicon))
|
2023-11-09 10:44:15 +08:00
|
|
|
rootRouter.Use(func(c *gin.Context) {
|
|
|
|
c.Next()
|
|
|
|
})
|
2022-10-17 17:13:47 +08:00
|
|
|
rootRouter.GET("/assets/*filepath", func(c *gin.Context) {
|
2023-12-11 14:20:06 +08:00
|
|
|
c.Writer.Header().Set("Cache-Control", fmt.Sprintf("private, max-age=%d", 3600))
|
2022-10-17 17:13:47 +08:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-16 23:30:23 +08:00
|
|
|
func Routers() *gin.Engine {
|
2023-12-20 22:44:09 +08:00
|
|
|
Router = gin.Default()
|
2022-12-13 18:54:28 +08:00
|
|
|
Router.Use(middleware.OperationLog())
|
2022-12-18 23:00:24 +08:00
|
|
|
// Router.Use(middleware.CSRF())
|
|
|
|
// Router.Use(middleware.LoadCsrfToken())
|
2023-02-21 15:19:12 +08:00
|
|
|
if global.CONF.System.IsDemo {
|
|
|
|
Router.Use(middleware.DemoHandle())
|
|
|
|
}
|
2023-05-30 00:00:56 +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-02-19 13:55:27 +08:00
|
|
|
Router.Use(i18n.UseI18n())
|
2022-08-16 23:30:23 +08:00
|
|
|
|
2023-03-29 11:46:14 +08:00
|
|
|
swaggerRouter := Router.Group("1panel")
|
|
|
|
docs.SwaggerInfo.BasePath = "/api/v1"
|
|
|
|
swaggerRouter.Use(middleware.JwtAuth()).Use(middleware.SessionAuth()).GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
|
2022-08-16 23:30:23 +08:00
|
|
|
PublicGroup := Router.Group("")
|
|
|
|
{
|
|
|
|
PublicGroup.GET("/health", func(c *gin.Context) {
|
|
|
|
c.JSON(200, "ok")
|
|
|
|
})
|
2023-05-30 00:00:56 +08:00
|
|
|
PublicGroup.Use(gzip.Gzip(gzip.DefaultCompression))
|
|
|
|
setWebStatic(PublicGroup)
|
2022-08-16 23:30:23 +08:00
|
|
|
}
|
|
|
|
PrivateGroup := Router.Group("/api/v1")
|
2023-05-19 21:47:46 +08:00
|
|
|
PrivateGroup.Use(middleware.WhiteAllow())
|
|
|
|
PrivateGroup.Use(middleware.BindDomain())
|
2023-01-31 15:22:05 +08:00
|
|
|
PrivateGroup.Use(middleware.GlobalLoading())
|
2023-12-20 22:44:09 +08:00
|
|
|
for _, router := range rou.RouterGroupApp {
|
|
|
|
router.InitRouter(PrivateGroup)
|
2022-08-16 23:30:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Router
|
|
|
|
}
|