Files
2026-09-01 10:13:33 +08:00

55 lines
1.2 KiB
Go
Raw Permalink 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 main
import (
"flag"
"fmt"
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
func main() {
portFlag := flag.Int("port", 8000, "服务监听端口")
flag.Parse()
if err := loadConfig("config.json"); err != nil {
log.Fatalf("加载配置文件失败: %v", err)
}
// 初始化 dashboard 状态(必须在 startBuildWorker 之前,否则 worker 回调为 nil
dashboard = newDashboard()
//gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.Use(gin.Recovery())
r.Use(func(c *gin.Context) {
t := time.Now()
c.Next()
log.Printf("[%s] %s %d %v", c.Request.Method, c.Request.URL.Path, c.Writer.Status(), time.Since(t))
})
startBuildWorker()
// Webhook 入口
r.POST("/webhook", handleWebhook)
// Dashboard 静态资源 + 页面
r.GET("/", handleIndex)
r.StaticFS("/static", http.FS(staticSubFS()))
// Dashboard JSON API
r.GET("/api/queue", handleQueue)
r.GET("/api/history", handleHistory)
listenAddr := fmt.Sprintf(":%d", *portFlag)
log.Printf("GitLab Webhook 服务已启动 (队列模式 + 3 分钟冷却去重), 监听端口 %d ...\n", *portFlag)
log.Printf("Dashboard: http://localhost:%d/\n", *portFlag)
if err := r.Run(listenAddr); err != nil {
log.Fatalf("服务启动失败: %v", err)
}
}