-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
286 lines (238 loc) · 9.45 KB
/
Copy pathmain.go
File metadata and controls
286 lines (238 loc) · 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"context"
"embed"
"flag"
"io/fs"
"log"
"net/http"
"os/signal"
"strings"
"syscall"
"time"
"code-shield/cron_jobs"
"code-shield/handlers"
"code-shield/models"
"code-shield/services"
"github.com/gin-gonic/gin"
)
//go:embed all:frontend/dist
var frontendFS embed.FS
// 构建时通过 -ldflags 注入
var (
Version = "dev"
CommitID = "unknown"
BuildTime = "unknown"
)
func main() {
staleAction := flag.String("stale", "recover", "Action for stale (pending/running) tasks found at startup: 'recover' (re-enqueue), 'ignore' (leave as is), 'delete' (delete from database)")
backfill := flag.Bool("backfill", false, "Backfill historical findings from successful task reports of specialized campaigns into campaign tables")
flag.Parse()
if *staleAction != "recover" && *staleAction != "ignore" && *staleAction != "delete" {
log.Fatalf("Invalid -stale option: %q. Allowed values: recover, ignore, delete", *staleAction)
}
log.Printf("Code-Shield Server %s (commit: %s, built: %s)\n", Version, CommitID, BuildTime)
// Initialize database
models.InitDB()
// Load global configuration
if err := models.LoadConfig("config.yaml"); err != nil {
log.Fatalf("Failed to load config.yaml: %v", err)
}
// 初始化多 LLM 调度分配器
services.InitModelDispatcher()
if *backfill {
log.Println("[Server] Running in backfill mode.")
if err := services.BackfillHistoricalFindings(); err != nil {
log.Fatalf("[Server] Backfill failed: %v", err)
}
log.Println("[Server] Backfill completed successfully.")
return
}
// Sync opencode agent files with current prompt files
services.SyncAllAgents()
// Start worker pool with configured concurrency
services.StartWorkerPool(models.AppConfig.Server.WorkerCount)
// Recover tasks that were pending/running before the last shutdown
services.RecoverPendingTasks(*staleAction)
// Start cron jobs
cron_jobs.StartCronJobs()
// Initialize Gin engine
gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.Use(gin.Recovery())
if models.AppConfig.Server.GinLog {
r.Use(gin.Logger())
log.Println("[Server] GIN request logging enabled")
}
// Register Auth routes (unprotected)
auth := r.Group("/api")
{
auth.POST("/login", handlers.Login)
auth.GET("/auth/config", handlers.GetAuthConfig)
auth.GET("/public/tasks/:id", handlers.GetPublicTaskDetails)
auth.GET("/public/tasks/:id/findings", handlers.GetPublicAnalysisFindings)
// Sync endpoints
auth.POST("/sync/user", handlers.SyncUser)
auth.POST("/sync/department", handlers.SyncDepartment)
auth.POST("/sync/repo", handlers.SyncRepo)
// Webhook endpoint
auth.POST("/webhook", handlers.HandleWebhook)
}
// Register API routes (protected)
api := r.Group("/api")
api.Use(handlers.AuthMiddleware())
{
api.GET("/me", handlers.GetMe)
api.GET("/me/findings", handlers.GetMyFindings)
api.PATCH("/password", handlers.UpdatePassword)
api.POST("/me/department", handlers.UpdateMyDepartment)
// Merge Request Realtime Monitoring
api.GET("/mr", handlers.GetMrEvents)
api.GET("/mr/:id", handlers.GetMrEventDetail)
api.GET("/departments", handlers.GetDepartments)
api.GET("/departments/export", handlers.ExportDepartments)
api.GET("/users", handlers.GetUsers)
api.GET("/users/export", handlers.ExportUsers)
api.GET("/repos", handlers.GetRepos)
api.GET("/repos/export", handlers.ExportRepos)
api.GET("/config", handlers.GetConfig)
api.PATCH("/config", handlers.UpdateConfig)
// Task routes (generic, replaces /reviews/*)
api.GET("/tasks/overview", handlers.GetTaskOverview)
api.GET("/tasks", handlers.GetTasks)
api.POST("/tasks/trigger", handlers.TriggerTask)
api.POST("/tasks/trigger-missing", handlers.TriggerMissingTasks)
api.POST("/tasks/:id/notify", handlers.TriggerManualNotification)
api.POST("/tasks/:id/resume", handlers.ResumeTask)
api.GET("/tasks/:id", handlers.GetTaskDetails)
api.GET("/tasks/:id/report", handlers.GetTaskReportMarkdown)
api.GET("/tasks/:id/synthesis", handlers.GetTaskReportSynthesisJSON)
api.GET("/tasks/:id/synthesis/csv", handlers.ExportTaskReportSynthesisCSV)
api.GET("/tasks/:id/summary", handlers.GetTaskReportSummaryJSON)
api.GET("/tasks/:id/findings", handlers.GetAnalysisFindings)
// Task type management (read-only for normal users)
api.GET("/task-types", handlers.GetTaskTypes)
api.GET("/task-types/:id", handlers.GetTaskType)
api.GET("/task-types/:id/files", handlers.GetTaskTypeFiles)
api.GET("/issues", handlers.GetIssues)
api.POST("/issues", handlers.CreateIssue)
api.PATCH("/issues/:id", handlers.UpdateIssue)
// UT Effectiveness Dashboard
api.GET("/analysis/ut/repos", handlers.GetUTRepos)
api.GET("/analysis/ut/findings", handlers.GetUTFindings)
api.PATCH("/analysis/ut/findings/:id", handlers.UpdateUTFinding)
api.GET("/analysis/ut/departments", handlers.GetUTDepartments)
api.GET("/analysis/ut/trends", handlers.GetUTTrends)
// Campaign generic routes mapping
registerCampaignRoutes[models.CoredumpFinding](api, "coredump", "coredump_risk")
registerCampaignRoutes[models.FloatFinding](api, "float", "float_comparison")
registerCampaignRoutes[models.ThreadFinding](api, "thread", "thread_create")
registerCampaignRoutes[models.CjsonFinding](api, "cjson", "cjson_scan")
api.GET("/schedules", handlers.GetSchedules)
api.POST("/schedules", handlers.CreateSchedule)
api.PUT("/schedules/:id", handlers.UpdateSchedule)
api.DELETE("/schedules/:id", handlers.DeleteSchedule)
api.POST("/schedules/:id/trigger", handlers.TriggerSchedule)
api.GET("/executions", handlers.GetExecutionLogs)
api.DELETE("/executions/completed", handlers.ClearCompletedExecutionLogs)
api.DELETE("/executions/:id", handlers.DeletePendingExecution)
// Admin only routes
admin := api.Group("/")
admin.Use(handlers.AdminMiddleware())
{
admin.POST("/task-types", handlers.CreateTaskType)
admin.PATCH("/task-types/:id", handlers.UpdateTaskType)
admin.DELETE("/task-types/:id", handlers.DeleteTaskType)
admin.PUT("/task-types/:id/files/:file_type", handlers.UpdateTaskTypeFile)
admin.POST("/task-types/:id/trigger-all", handlers.TriggerAllReposForTaskType)
admin.DELETE("/tasks/invalid-reports", handlers.ClearInvalidReports)
admin.DELETE("/tasks/:id", handlers.DeleteTaskReport)
}
}
// Serve frontend static files
distFS, err := fs.Sub(frontendFS, "frontend/dist")
if err != nil {
log.Println("Warning: frontend dist folder not found, skipping frontend embedding.")
} else {
httpFS := http.FS(distFS)
r.NoRoute(func(c *gin.Context) {
path := c.Request.URL.Path
// DO NOT serve frontend index.html for API routes
if len(path) >= 4 && path[:4] == "/api" {
c.JSON(http.StatusNotFound, gin.H{"error": "API route not found"})
return
}
// If the user visits the root / or exact /shield (no slash), redirect to /shield/
if path == "/" || path == "/shield" {
c.Redirect(http.StatusFound, "/shield/")
return
}
// Strip /shield prefix if present to match internal dist directory structure
cleanPath := path
if strings.HasPrefix(path, "/shield") {
cleanPath = strings.TrimPrefix(path, "/shield")
}
if cleanPath != "" && cleanPath != "/" {
f, err := distFS.Open(cleanPath[1:])
if err == nil {
f.Close()
c.FileFromFS(cleanPath, httpFS)
return
}
}
indexBytes, err := fs.ReadFile(distFS, "index.html")
if err != nil {
c.String(http.StatusNotFound, "index.html not found")
return
}
c.Data(http.StatusOK, "text/html; charset=utf-8", indexBytes)
})
}
// Build HTTP server with timeouts
port := models.AppConfig.Server.Port
if port == "" {
port = ":8080"
}
// Wrap standard handler to strip /shield prefix for API requests before passing to Gin
var httpHandler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if strings.HasPrefix(req.URL.Path, "/shield/api") {
req.URL.Path = strings.TrimPrefix(req.URL.Path, "/shield")
}
r.ServeHTTP(w, req)
})
srv := &http.Server{
Addr: port,
Handler: httpHandler,
ReadTimeout: models.AppConfig.Server.ReadTimeout,
ReadHeaderTimeout: models.AppConfig.Server.ReadHeaderTimeout,
WriteTimeout: models.AppConfig.Server.WriteTimeout,
IdleTimeout: models.AppConfig.Server.IdleTimeout,
MaxHeaderBytes: models.AppConfig.Server.MaxHeaderBytes,
}
// Graceful shutdown
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
go func() {
log.Printf("Starting server on %s ...\n", port)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Server failed to start: %v", err)
}
}()
<-ctx.Done()
log.Println("Shutting down server ...")
// Cancel all running tasks to terminate AI processes
services.CancelAllRunningTasks()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(shutdownCtx); err != nil {
log.Fatalf("Server forced to shutdown: %v", err)
}
log.Println("Server exited gracefully")
}
func registerCampaignRoutes[T any](rg *gin.RouterGroup, campaignPath string, taskTypeName string) {
rg.GET("/analysis/"+campaignPath+"/repos", handlers.GetCampaignRepos[T](taskTypeName))
rg.GET("/analysis/"+campaignPath+"/findings", handlers.GetCampaignFindings[T]())
rg.PATCH("/analysis/"+campaignPath+"/findings/:id", handlers.UpdateCampaignFinding[T]())
rg.GET("/analysis/"+campaignPath+"/departments", handlers.GetCampaignDepartments[T]())
rg.GET("/analysis/"+campaignPath+"/trends", handlers.GetCampaignTrends[T]())
}