Files
ansible-deploy/cmd/main.go
T
cnbugs bdb7d54505 feat: v2.0 全面升级 - SSH密钥管理/模板中心/文件分发/全新UI
- 新增SSH密钥库管理(集中托管私钥、自动指纹识别)
- 新增模板中心(24个预置模板,7大分类,一键创建Playbook)
- 新增文件分发功能(内容/文件双模式,权限设置)
- 新增系统信息接口(版本/主机数/运行时间)
- 主机支持密码/SSH密钥双认证方式
- 全新暗色玻璃拟态UI(侧边栏导航、SSE实时日志流)
- 修复UpdateHost ID丢失bug
- 添加.gitignore排除敏感数据
2026-07-30 22:49:13 +08:00

120 lines
3.1 KiB
Go

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)
}
}