2026-07-13 14:39:56 +08:00
|
|
|
|
package router
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-07-23 12:04:51 +08:00
|
|
|
|
"context"
|
2026-07-15 14:05:22 +08:00
|
|
|
|
"github.com/1024XEngineer/xinfra/server/internal/config"
|
|
|
|
|
|
"github.com/1024XEngineer/xinfra/server/internal/handler"
|
|
|
|
|
|
"github.com/1024XEngineer/xinfra/server/internal/service"
|
2026-07-13 14:39:56 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2026-07-15 13:43:21 +08:00
|
|
|
|
swaggerFiles "github.com/swaggo/files"
|
|
|
|
|
|
ginSwagger "github.com/swaggo/gin-swagger"
|
2026-07-13 14:39:56 +08:00
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Dependencies struct {
|
|
|
|
|
|
Config config.Config
|
|
|
|
|
|
DB *gorm.DB
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-15 15:12:54 +08:00
|
|
|
|
// New 初始化路由。
|
2026-07-15 13:43:21 +08:00
|
|
|
|
// @title xinfra API
|
|
|
|
|
|
// @version 1.0
|
|
|
|
|
|
// @description xinfra 平台后端 API 文档
|
|
|
|
|
|
// @termsOfService http://swagger.io/terms/
|
2026-07-15 15:12:54 +08:00
|
|
|
|
// @contact.name API Support
|
|
|
|
|
|
// @contact.url http://www.swagger.io/support
|
|
|
|
|
|
// @contact.email support@swagger.io
|
|
|
|
|
|
// @license.name Apache 2.0
|
|
|
|
|
|
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
|
|
|
|
|
|
// @BasePath /api/v1
|
2026-07-15 13:43:21 +08:00
|
|
|
|
// @securityDefinitions.apikey BearerAuth
|
|
|
|
|
|
// @in header
|
|
|
|
|
|
// @name Authorization
|
|
|
|
|
|
// @description 请输入 Bearer Token(例如:Bearer xxx)
|
2026-07-13 14:39:56 +08:00
|
|
|
|
func New(deps Dependencies) *gin.Engine {
|
|
|
|
|
|
if deps.Config.AppEnv == "prod" {
|
|
|
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
r := gin.New()
|
|
|
|
|
|
r.Use(gin.Logger(), gin.Recovery())
|
|
|
|
|
|
|
2026-07-15 15:12:54 +08:00
|
|
|
|
registerSwaggerRoutes(r)
|
|
|
|
|
|
registerTestRoutes(r)
|
|
|
|
|
|
registerAuthServerRoutes(r, deps)
|
2026-07-15 13:43:21 +08:00
|
|
|
|
|
|
|
|
|
|
return r
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-15 15:12:54 +08:00
|
|
|
|
func registerSwaggerRoutes(r *gin.Engine) {
|
|
|
|
|
|
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.URL("/swagger/doc.json")))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func registerTestRoutes(r *gin.Engine) {
|
|
|
|
|
|
r.GET("/api/v1/ping", handler.Ping)
|
|
|
|
|
|
|
2026-07-15 13:43:21 +08:00
|
|
|
|
testGroup := r.Group("/api/v1/test")
|
|
|
|
|
|
{
|
|
|
|
|
|
testGroup.GET("/success", handler.TestSuccess)
|
|
|
|
|
|
testGroup.GET("/error/:code", handler.TestError)
|
|
|
|
|
|
testGroup.GET("/500", handler.Test500)
|
|
|
|
|
|
testGroup.GET("/401", handler.Test401)
|
|
|
|
|
|
testGroup.GET("/403", handler.Test403)
|
|
|
|
|
|
testGroup.GET("/timeout", handler.TestTimeout)
|
|
|
|
|
|
testGroup.GET("/paginated", handler.TestPaginated)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-07-15 15:12:54 +08:00
|
|
|
|
|
|
|
|
|
|
func registerAuthServerRoutes(r *gin.Engine, deps Dependencies) {
|
2026-07-13 14:39:56 +08:00
|
|
|
|
auditService := service.NewAuditService(deps.DB)
|
|
|
|
|
|
authService := service.NewAuthService(deps.Config, deps.DB, auditService)
|
|
|
|
|
|
wayenService := service.NewWayenService(deps.Config, deps.DB)
|
2026-07-21 17:05:48 +08:00
|
|
|
|
wayneRoleBindingService := service.NewWayneRoleBindingService(deps.Config, deps.DB)
|
2026-07-21 18:26:50 +08:00
|
|
|
|
deploymentService := service.NewDeploymentService(deps.Config, deps.DB)
|
2026-07-23 12:04:51 +08:00
|
|
|
|
deliveryService := service.NewDeliveryService(deps.Config, deps.DB, auditService)
|
|
|
|
|
|
if deps.Config.DeliverySchedulerEnabled {
|
|
|
|
|
|
go deliveryService.Run(context.Background())
|
|
|
|
|
|
}
|
2026-07-13 14:39:56 +08:00
|
|
|
|
|
|
|
|
|
|
healthHandler := handler.NewHealthHandler(deps.DB)
|
2026-07-16 16:19:25 +08:00
|
|
|
|
authHandler := handler.NewAuthHandler(deps.Config, authService)
|
2026-07-17 15:02:18 +08:00
|
|
|
|
userHandler := handler.NewUserHandler(deps.DB)
|
2026-07-17 19:26:40 +08:00
|
|
|
|
businessLineHandler := handler.NewBusinessLineHandler(deps.DB, wayneRoleBindingService)
|
2026-07-13 14:39:56 +08:00
|
|
|
|
wayenHandler := handler.NewWayenHandler(deps.DB, wayenService, auditService)
|
2026-07-16 15:52:56 +08:00
|
|
|
|
wayneRoleBindingHandler := handler.NewWayneRoleBindingHandler(wayneRoleBindingService, auditService)
|
2026-07-17 19:26:40 +08:00
|
|
|
|
subsystemAuthHandler := handler.NewSubsystemAuthHandler(deps.DB, wayneRoleBindingService, auditService)
|
2026-07-14 17:37:08 +08:00
|
|
|
|
clouddmHandler := handler.NewCloudDMHandler(deps.Config, auditService)
|
2026-07-13 14:39:56 +08:00
|
|
|
|
samlHandler := handler.NewSAMLHandler(deps.Config, authService)
|
|
|
|
|
|
oauthHandler := handler.NewOAuthHandler(deps.Config, deps.DB, auditService)
|
2026-07-21 18:26:50 +08:00
|
|
|
|
deploymentHandler := handler.NewDeploymentHandler(deps.Config, deps.DB, deploymentService)
|
2026-07-23 12:04:51 +08:00
|
|
|
|
deliveryHandler := handler.NewDeliveryHandler(deliveryService)
|
|
|
|
|
|
executionHandler := handler.NewExecutionHandler(deliveryService, deps.Config.DeliveryServiceToken)
|
2026-07-22 14:57:11 +08:00
|
|
|
|
containerServiceHandler := handler.NewContainerServiceHandler(deps.DB, wayneRoleBindingService)
|
2026-07-13 14:39:56 +08:00
|
|
|
|
|
|
|
|
|
|
r.GET("/healthz", healthHandler.Healthz)
|
|
|
|
|
|
r.GET("/readyz", healthHandler.Readyz)
|
2026-07-23 12:04:51 +08:00
|
|
|
|
r.POST("/api/v1/executions", executionHandler.Create)
|
2026-07-14 17:37:08 +08:00
|
|
|
|
r.GET("/auth/.well-known/openid-configuration", oauthHandler.Discovery)
|
2026-07-13 14:39:56 +08:00
|
|
|
|
r.GET("/auth/oauth/authorize", oauthHandler.Authorize)
|
|
|
|
|
|
r.POST("/auth/oauth/token", oauthHandler.Token)
|
2026-07-14 17:37:08 +08:00
|
|
|
|
r.GET("/auth/oauth/jwks", oauthHandler.JWKS)
|
2026-07-13 14:39:56 +08:00
|
|
|
|
r.GET("/auth/oauth/userinfo", oauthHandler.UserInfo)
|
2026-07-21 18:26:50 +08:00
|
|
|
|
r.POST("/auth/internal/deployments/:id/events", deploymentHandler.InternalEvent)
|
|
|
|
|
|
r.POST("/auth/internal/deployments/:id/finish", deploymentHandler.InternalFinish)
|
2026-07-13 14:39:56 +08:00
|
|
|
|
|
|
|
|
|
|
v1 := r.Group("/auth/api/v1")
|
|
|
|
|
|
{
|
2026-07-16 16:19:25 +08:00
|
|
|
|
v1.GET("/config", authHandler.Config)
|
|
|
|
|
|
v1.POST("/login", authHandler.LocalLogin)
|
2026-07-13 14:39:56 +08:00
|
|
|
|
v1.GET("/login/internal-sso", samlHandler.Login)
|
2026-07-16 15:52:56 +08:00
|
|
|
|
v1.POST("/logout", samlHandler.Logout)
|
2026-07-13 14:39:56 +08:00
|
|
|
|
v1.GET("/saml/metadata", samlHandler.Metadata)
|
|
|
|
|
|
v1.POST("/saml/acs", samlHandler.ACS)
|
|
|
|
|
|
|
|
|
|
|
|
protected := v1.Group("")
|
|
|
|
|
|
protected.Use(handler.AuthMiddleware(deps.Config))
|
|
|
|
|
|
protected.GET("/users/me", userHandler.Me)
|
2026-07-17 15:02:18 +08:00
|
|
|
|
protected.GET("/users", userHandler.List)
|
|
|
|
|
|
protected.GET("/business-lines", businessLineHandler.ListCurrentUserBusinessLines)
|
|
|
|
|
|
protected.GET("/business-lines/all", businessLineHandler.ListAll)
|
|
|
|
|
|
protected.POST("/business-lines", businessLineHandler.Create)
|
|
|
|
|
|
protected.PUT("/business-lines/:id", businessLineHandler.Update)
|
|
|
|
|
|
protected.DELETE("/business-lines/:id", businessLineHandler.Delete)
|
|
|
|
|
|
protected.POST("/business-lines/authorizations", businessLineHandler.GrantPermission)
|
|
|
|
|
|
protected.GET("/business-lines/:id/wayne-namespaces", businessLineHandler.ListWayneNamespaces)
|
|
|
|
|
|
protected.PUT("/business-lines/:id/wayne-namespaces", businessLineHandler.ReplaceWayneNamespaces)
|
2026-07-13 14:39:56 +08:00
|
|
|
|
protected.GET("/wayen/login", wayenHandler.Login)
|
|
|
|
|
|
protected.GET("/wayen/credential", wayenHandler.GetCredential)
|
|
|
|
|
|
protected.PUT("/wayen/credential", wayenHandler.SaveCredential)
|
2026-07-16 15:52:56 +08:00
|
|
|
|
protected.GET("/wayne/namespaces", wayneRoleBindingHandler.ListNamespaces)
|
|
|
|
|
|
protected.GET("/wayne/groups", wayneRoleBindingHandler.ListGroups)
|
2026-07-17 19:26:40 +08:00
|
|
|
|
protected.GET("/wayne/users/:username/roles", wayneRoleBindingHandler.GetCurrentUserRoles)
|
2026-07-16 15:52:56 +08:00
|
|
|
|
protected.GET("/wayne/namespaces/:namespaceid/operator-permissions", wayneRoleBindingHandler.NamespaceOperatorPermissions)
|
|
|
|
|
|
protected.GET("/wayne/apps/:appid/operator-permissions", wayneRoleBindingHandler.AppOperatorPermissions)
|
|
|
|
|
|
protected.PUT("/wayne/namespaces/:namespaceid/roles", wayneRoleBindingHandler.BindNamespace)
|
|
|
|
|
|
protected.DELETE("/wayne/namespaces/:namespaceid/roles", wayneRoleBindingHandler.UnbindNamespace)
|
|
|
|
|
|
protected.PUT("/wayne/apps/:appid/roles", wayneRoleBindingHandler.BindApp)
|
|
|
|
|
|
protected.DELETE("/wayne/apps/:appid/roles", wayneRoleBindingHandler.UnbindApp)
|
2026-07-17 19:26:40 +08:00
|
|
|
|
protected.GET("/subsystem-auth/systems", subsystemAuthHandler.ListSystems)
|
|
|
|
|
|
protected.GET("/subsystem-auth/wayne/roles", subsystemAuthHandler.ListWayneNamespaceRoles)
|
|
|
|
|
|
protected.GET("/subsystem-auth/wayne/business-lines/:id/namespaces", subsystemAuthHandler.ListWayneBusinessLineNamespaces)
|
|
|
|
|
|
protected.GET("/subsystem-auth/wayne/users/:username/roles", subsystemAuthHandler.GetWayneUserRoles)
|
|
|
|
|
|
protected.PUT("/subsystem-auth/wayne/business-lines/:id/namespaces/:namespaceid/users/:username/roles", subsystemAuthHandler.BindWayneNamespaceRoles)
|
|
|
|
|
|
protected.DELETE("/subsystem-auth/wayne/business-lines/:id/namespaces/:namespaceid/users/:username/roles", subsystemAuthHandler.UnbindWayneNamespaceRoles)
|
|
|
|
|
|
protected.POST("/subsystem-auth/wayne/business-lines/:id/users/:userid/init", subsystemAuthHandler.InitWayneBusinessLineUser)
|
2026-07-22 14:57:11 +08:00
|
|
|
|
protected.GET("/container-services/business-lines/:id/summary", containerServiceHandler.Summary)
|
|
|
|
|
|
protected.GET("/container-services/business-lines/:id/workloads", containerServiceHandler.Workloads)
|
2026-07-21 18:26:50 +08:00
|
|
|
|
protected.POST("/deployments", deploymentHandler.Create)
|
|
|
|
|
|
protected.GET("/deployments/:id", deploymentHandler.Get)
|
|
|
|
|
|
protected.GET("/deployments/:id/events", deploymentHandler.Events)
|
|
|
|
|
|
protected.POST("/deployments/:id/cancel", deploymentHandler.Cancel)
|
2026-07-14 17:37:08 +08:00
|
|
|
|
protected.GET("/clouddm/login", clouddmHandler.Login)
|
2026-07-23 12:04:51 +08:00
|
|
|
|
protected.GET("/delivery/targets", deliveryHandler.Targets)
|
|
|
|
|
|
protected.POST("/delivery/targets", deliveryHandler.CreateTarget)
|
|
|
|
|
|
protected.PUT("/delivery/quotas", deliveryHandler.UpsertQuota)
|
|
|
|
|
|
protected.POST("/delivery/mysql", deliveryHandler.CreateMySQL)
|
|
|
|
|
|
protected.GET("/delivery/tasks", deliveryHandler.List)
|
|
|
|
|
|
protected.GET("/delivery/tasks/:id", deliveryHandler.Get)
|
|
|
|
|
|
protected.POST("/delivery/tasks/:id/cancel", deliveryHandler.Cancel)
|
2026-07-13 14:39:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|