package main import ( "flag" "log" "os" "github.com/ansible-deploy/internal/handlers" "github.com/ansible-deploy/internal/services" "github.com/gin-gonic/gin" ) var ( configPath string port string ) func init() { flag.StringVar(&configPath, "config", "config/config.yaml", "配置文件路径") flag.StringVar(&port, "port", "8080", "服务端口") } func main() { flag.Parse() cfg, err := services.LoadConfig(configPath) if err != nil { log.Printf("配置加载失败: %v,使用默认配置", err) cfg = services.DefaultConfig() } ansibleService := services.NewAnsibleService(cfg) h := handlers.NewAnsibleHandler(ansibleService) r := gin.Default() r.Use(func(c *gin.Context) { c.Header("Access-Control-Allow-Origin", "*") c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") c.Header("Access-Control-Allow-Headers", "Content-Type, Authorization") if c.Request.Method == "OPTIONS" { c.AbortWithStatus(204) return } c.Next() }) r.Static("/static", "./web/dist") r.Static("/assets", "./web/dist/assets") api := r.Group("/api") { // 主机管理 api.GET("/hosts", h.ListHosts) api.POST("/hosts", h.AddHost) api.DELETE("/hosts/:id", h.DeleteHost) api.PUT("/hosts/:id", h.UpdateHost) api.POST("/hosts/test/:id", h.TestConnection) api.GET("/hosts/test/:id", h.TestConnection) // 主机组管理 api.GET("/groups", h.ListGroups) api.POST("/groups", h.CreateGroup) api.DELETE("/groups/:name", h.DeleteGroup) api.PUT("/groups/:name", h.UpdateGroup) // Playbook管理 api.GET("/playbooks", h.ListPlaybooks) api.POST("/playbooks", h.CreatePlaybook) api.GET("/playbooks/:name/content", h.GetPlaybookContent) api.PUT("/playbooks/:name", h.UpdatePlaybook) api.DELETE("/playbooks/:name", h.DeletePlaybook) api.POST("/playbooks/execute", h.ExecutePlaybook) api.GET("/playbooks/:name", h.GetPlaybook) // Playbook模板 api.GET("/templates", h.ListTemplates) api.GET("/templates/categories", h.ListTemplateCategories) api.GET("/templates/:id", h.GetTemplate) api.POST("/templates/deploy", h.CreateFromTemplate) // 命令执行 api.POST("/command/execute", h.ExecuteCommand) api.POST("/command/batch", h.BatchExecute) // 文件分发 api.POST("/files/distribute", h.DistributeFile) // SSH密钥管理 api.GET("/sshkeys", h.ListSSHKeys) api.POST("/sshkeys", h.AddSSHKey) api.DELETE("/sshkeys/:name", h.DeleteSSHKey) // 任务执行 api.GET("/tasks", h.ListTasks) api.GET("/tasks/:id", h.GetTask) api.GET("/tasks/:id/stream", h.StreamTaskOutput) api.DELETE("/tasks/:id", h.CancelTask) // 系统信息 api.GET("/system/info", h.GetSystemInfo) } r.GET("/", func(c *gin.Context) { c.Header("Cache-Control", "no-cache, no-store, must-revalidate") c.Header("Pragma", "no-cache") c.Header("Expires", "0") c.File("./web/dist/index.html") }) os.MkdirAll(cfg.InventoryDir, 0755) os.MkdirAll(cfg.PlaybookDir, 0755) os.MkdirAll(cfg.LogDir, 0755) log.Printf("🚀 Ansible Deploy v2.0 启动,监听端口: %s", port) if err := r.Run(":" + port); err != nil { log.Fatalf("服务启动失败: %v", err) } }