1Panel/backend/init/router/router.go

91 lines
2.9 KiB
Go
Raw Normal View History

2022-08-16 23:30:23 +08:00
package router
import (
2022-10-20 18:45:47 +08:00
"html/template"
"net/http"
2023-04-17 18:30:40 +08:00
"github.com/gin-contrib/gzip"
"github.com/1Panel-dev/1Panel/backend/global"
"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"
2022-08-16 23:30:23 +08:00
ginI18n "github.com/gin-contrib/i18n"
"github.com/gin-gonic/gin"
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
2022-10-17 17:13:47 +08:00
func setWebStatic(rootRouter *gin.Engine) {
rootRouter.StaticFS("/fav", http.FS(web.Favicon))
2022-10-17 17:13:47 +08:00
rootRouter.GET("/assets/*filepath", func(c *gin.Context) {
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)
})
rootRouter.NoRoute(func(c *gin.Context) {
c.Writer.WriteHeader(http.StatusOK)
2022-11-21 15:20:04 +08:00
_, _ = c.Writer.Write(web.IndexByte)
2022-10-17 17:13:47 +08:00
c.Writer.Header().Add("Accept", "text/html")
c.Writer.Flush()
})
}
2022-08-16 23:30:23 +08:00
func Routers() *gin.Engine {
Router := gin.Default()
Router.Use(middleware.OperationLog())
// 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())
}
Router.Use(gzip.Gzip(gzip.DefaultCompression))
2022-10-17 17:13:47 +08:00
setWebStatic(Router)
2022-08-16 23:30:23 +08:00
Router.Use(i18n.GinI18nLocalize())
Router.SetFuncMap(template.FuncMap{
"Localize": ginI18n.GetMessage,
})
systemRouter := rou.RouterGroupApp
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")
})
}
PrivateGroup := Router.Group("/api/v1")
2023-01-31 15:22:05 +08:00
PrivateGroup.Use(middleware.GlobalLoading())
2022-08-16 23:30:23 +08:00
{
systemRouter.InitBaseRouter(PrivateGroup)
2022-11-24 23:56:48 +08:00
systemRouter.InitDashboardRouter(PrivateGroup)
systemRouter.InitHostRouter(PrivateGroup)
systemRouter.InitContainerRouter(PrivateGroup)
systemRouter.InitTerminalRouter(PrivateGroup)
2022-09-08 11:39:14 +08:00
systemRouter.InitMonitorRouter(PrivateGroup)
2022-11-15 17:20:57 +08:00
systemRouter.InitLogRouter(PrivateGroup)
2022-08-24 11:10:50 +08:00
systemRouter.InitFileRouter(PrivateGroup)
systemRouter.InitCronjobRouter(PrivateGroup)
2022-09-15 10:44:43 +08:00
systemRouter.InitSettingRouter(PrivateGroup)
systemRouter.InitAppRouter(PrivateGroup)
2022-10-28 17:04:57 +08:00
systemRouter.InitWebsiteRouter(PrivateGroup)
systemRouter.InitWebsiteGroupRouter(PrivateGroup)
systemRouter.InitWebsiteDnsAccountRouter(PrivateGroup)
2022-10-20 18:45:47 +08:00
systemRouter.InitDatabaseRouter(PrivateGroup)
2022-11-16 10:31:35 +08:00
systemRouter.InitWebsiteSSLRouter(PrivateGroup)
2022-11-11 17:41:39 +08:00
systemRouter.InitWebsiteAcmeAccountRouter(PrivateGroup)
2022-11-23 16:19:05 +08:00
systemRouter.InitNginxRouter(PrivateGroup)
2023-03-29 14:58:28 +08:00
systemRouter.InitRuntimeRouter(PrivateGroup)
2022-08-16 23:30:23 +08:00
}
return Router
}