main.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package main
  2. import (
  3. "flag"
  4. "log"
  5. "os"
  6. "github.com/ansible-deploy/internal/handlers"
  7. "github.com/ansible-deploy/internal/services"
  8. "github.com/gin-gonic/gin"
  9. )
  10. var (
  11. configPath string
  12. port string
  13. )
  14. func init() {
  15. flag.StringVar(&configPath, "config", "config/config.yaml", "配置文件路径")
  16. flag.StringVar(&port, "port", "8080", "服务端口")
  17. }
  18. func main() {
  19. flag.Parse()
  20. // 加载配置
  21. cfg, err := services.LoadConfig(configPath)
  22. if err != nil {
  23. log.Printf("配置加载失败: %v,使用默认配置", err)
  24. cfg = services.DefaultConfig()
  25. }
  26. // 初始化Ansible服务
  27. ansibleService := services.NewAnsibleService(cfg)
  28. // 初始化处理器
  29. h := handlers.NewAnsibleHandler(ansibleService)
  30. // 初始化Web服务
  31. r := gin.Default()
  32. // CORS配置
  33. r.Use(func(c *gin.Context) {
  34. c.Header("Access-Control-Allow-Origin", "*")
  35. c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
  36. c.Header("Access-Control-Allow-Headers", "Content-Type, Authorization")
  37. if c.Request.Method == "OPTIONS" {
  38. c.AbortWithStatus(204)
  39. return
  40. }
  41. c.Next()
  42. })
  43. // 静态文件
  44. r.Static("/static", "./web/dist")
  45. r.Static("/assets", "./web/dist/assets")
  46. // API路由
  47. api := r.Group("/api")
  48. {
  49. // 主机管理
  50. api.GET("/hosts", h.ListHosts)
  51. api.POST("/hosts", h.AddHost)
  52. api.DELETE("/hosts/:id", h.DeleteHost)
  53. api.PUT("/hosts/:id", h.UpdateHost)
  54. api.POST("/hosts/test/:id", h.TestConnection)
  55. // 主机组管理
  56. api.GET("/groups", h.ListGroups)
  57. api.POST("/groups", h.CreateGroup)
  58. api.DELETE("/groups/:name", h.DeleteGroup)
  59. api.PUT("/groups/:name", h.UpdateGroup)
  60. // Playbook管理
  61. api.GET("/playbooks", h.ListPlaybooks)
  62. api.POST("/playbooks", h.CreatePlaybook)
  63. api.GET("/playbooks/:name/content", h.GetPlaybookContent)
  64. api.PUT("/playbooks/:name", h.UpdatePlaybook)
  65. api.DELETE("/playbooks/:name", h.DeletePlaybook)
  66. api.POST("/playbooks/execute", h.ExecutePlaybook)
  67. api.GET("/playbooks/:name", h.GetPlaybook)
  68. // 命令执行
  69. api.POST("/command/execute", h.ExecuteCommand)
  70. api.POST("/command/batch", h.BatchExecute)
  71. // 任务执行
  72. api.GET("/tasks", h.ListTasks)
  73. api.GET("/tasks/:id", h.GetTask)
  74. api.GET("/tasks/:id/stream", h.StreamTaskOutput)
  75. api.DELETE("/tasks/:id", h.CancelTask)
  76. }
  77. // 前端路由 - 禁止缓存确保始终返回最新版本
  78. r.GET("/", func(c *gin.Context) {
  79. c.Header("Cache-Control", "no-cache, no-store, must-revalidate")
  80. c.Header("Pragma", "no-cache")
  81. c.Header("Expires", "0")
  82. c.File("./web/dist/index.html")
  83. })
  84. // 创建必要目录
  85. os.MkdirAll(cfg.InventoryDir, 0755)
  86. os.MkdirAll(cfg.PlaybookDir, 0755)
  87. os.MkdirAll(cfg.LogDir, 0755)
  88. log.Printf("Ansible部署工具启动,监听端口: %s", port)
  89. if err := r.Run(":" + port); err != nil {
  90. log.Fatalf("服务启动失败: %v", err)
  91. }
  92. }