Files
xinfra/server/internal/router/router.go
T
Hungerdream be39f2a03c feat(delivery): expose rollback retry, release ack and CloudDM retry APIs
- POST /delivery/tasks/:id/rollback/retry  (platform admin) re-launches
  the rollback job after a previous cleanup failure
- POST /delivery/tasks/:id/rollback/release (platform admin) releases
  bookkeeping after the operator verified the target host manually
- POST /delivery/tasks/:id/clouddm/retry retries only the CloudDM
  registration step for an already healthy instance
- task logs now include rollback AWX job stdout and localized
  text/classes for the new rollback states
2026-07-28 14:29:26 +08:00

159 lines
7.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package router
import (
"context"
"github.com/1024XEngineer/xinfra/server/internal/config"
"github.com/1024XEngineer/xinfra/server/internal/handler"
"github.com/1024XEngineer/xinfra/server/internal/service"
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"gorm.io/gorm"
)
type Dependencies struct {
Config config.Config
DB *gorm.DB
}
// New 初始化路由。
// @title xinfra API
// @version 1.0
// @description xinfra 平台后端 API 文档
// @termsOfService http://swagger.io/terms/
// @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
// @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization
// @description 请输入 Bearer Token(例如:Bearer xxx)
func New(deps Dependencies) *gin.Engine {
if deps.Config.AppEnv == "prod" {
gin.SetMode(gin.ReleaseMode)
}
r := gin.New()
r.Use(gin.Logger(), gin.Recovery())
registerSwaggerRoutes(r)
registerTestRoutes(r)
registerAuthServerRoutes(r, deps)
return r
}
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)
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)
}
}
func registerAuthServerRoutes(r *gin.Engine, deps Dependencies) {
auditService := service.NewAuditService(deps.DB)
authService := service.NewAuthService(deps.Config, deps.DB, auditService)
wayenService := service.NewWayenService(deps.Config, deps.DB)
wayneRoleBindingService := service.NewWayneRoleBindingService(deps.Config, deps.DB)
deliveryService := service.NewDeliveryService(deps.Config, deps.DB, auditService)
if deps.Config.DeliverySchedulerEnabled {
go deliveryService.Run(context.Background())
}
healthHandler := handler.NewHealthHandler(deps.DB)
authHandler := handler.NewAuthHandler(deps.Config, authService)
userHandler := handler.NewUserHandler(deps.DB)
businessLineHandler := handler.NewBusinessLineHandler(deps.DB, wayneRoleBindingService)
wayenHandler := handler.NewWayenHandler(deps.DB, wayenService, auditService)
wayneRoleBindingHandler := handler.NewWayneRoleBindingHandler(wayneRoleBindingService, auditService)
subsystemAuthHandler := handler.NewSubsystemAuthHandler(deps.DB, wayneRoleBindingService, auditService)
clouddmHandler := handler.NewCloudDMHandler(deps.Config, auditService)
samlHandler := handler.NewSAMLHandler(deps.Config, authService)
oauthHandler := handler.NewOAuthHandler(deps.Config, deps.DB, auditService)
deliveryHandler := handler.NewDeliveryHandler(deliveryService)
executionHandler := handler.NewExecutionHandler(deliveryService, deps.Config.DeliveryServiceToken)
containerServiceHandler := handler.NewContainerServiceHandler(deps.DB, wayneRoleBindingService)
taskLogHandler := handler.NewTaskLogHandler(deps.DB, deliveryService, wayneRoleBindingService)
r.GET("/healthz", healthHandler.Healthz)
r.GET("/readyz", healthHandler.Readyz)
r.POST("/api/v1/executions", executionHandler.Create)
r.GET("/auth/.well-known/openid-configuration", oauthHandler.Discovery)
r.GET("/auth/oauth/authorize", oauthHandler.Authorize)
r.POST("/auth/oauth/token", oauthHandler.Token)
r.GET("/auth/oauth/jwks", oauthHandler.JWKS)
r.GET("/auth/oauth/userinfo", oauthHandler.UserInfo)
v1 := r.Group("/auth/api/v1")
{
v1.GET("/config", authHandler.Config)
v1.POST("/login", authHandler.LocalLogin)
v1.GET("/login/internal-sso", samlHandler.Login)
v1.POST("/logout", samlHandler.Logout)
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)
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)
protected.GET("/wayen/login", wayenHandler.Login)
protected.GET("/wayen/credential", wayenHandler.GetCredential)
protected.PUT("/wayen/credential", wayenHandler.SaveCredential)
protected.GET("/wayne/namespaces", wayneRoleBindingHandler.ListNamespaces)
protected.GET("/wayne/groups", wayneRoleBindingHandler.ListGroups)
protected.GET("/wayne/users/:username/roles", wayneRoleBindingHandler.GetCurrentUserRoles)
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)
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)
protected.GET("/container-services/business-lines/:id/summary", containerServiceHandler.Summary)
protected.GET("/container-services/business-lines/:id/workloads", containerServiceHandler.Workloads)
protected.GET("/clouddm/login", clouddmHandler.Login)
protected.GET("/delivery/targets", deliveryHandler.Targets)
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)
protected.POST("/delivery/tasks/:id/rollback/retry", deliveryHandler.RetryRollback)
protected.POST("/delivery/tasks/:id/rollback/release", deliveryHandler.AcknowledgeRollbackRelease)
protected.POST("/delivery/tasks/:id/clouddm/retry", deliveryHandler.RetryCloudDMRegistration)
protected.GET("/task-logs", taskLogHandler.List)
protected.GET("/task-logs/:id", taskLogHandler.Get)
}
}