功能开发完成

This commit is contained in:
2025-12-31 16:23:40 +08:00
parent 2b51050ca8
commit 6f0598a859
28 changed files with 5463 additions and 118 deletions

View File

@@ -3,23 +3,32 @@ package api
import (
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
_ "github.com/gitcodestatic/gitcodestatic/docs"
"github.com/gitcodestatic/gitcodestatic/internal/api/handlers"
"github.com/gitcodestatic/gitcodestatic/internal/service"
"github.com/gitcodestatic/gitcodestatic/internal/storage"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
httpSwagger "github.com/swaggo/http-swagger"
)
// Router 路由配置
type Router struct {
repoHandler *handlers.RepoHandler
statsHandler *handlers.StatsHandler
taskHandler *handlers.TaskHandler
webDir string
webEnabled bool
}
// NewRouter 创建路由
func NewRouter(repoService *service.RepoService, statsService *service.StatsService) *Router {
func NewRouter(repoService *service.RepoService, statsService *service.StatsService, store storage.Store, webDir string, webEnabled bool) *Router {
return &Router{
repoHandler: handlers.NewRepoHandler(repoService),
statsHandler: handlers.NewStatsHandler(statsService),
statsHandler: handlers.NewStatsHandler(statsService, store),
taskHandler: handlers.NewTaskHandler(store),
webDir: webDir,
webEnabled: webEnabled,
}
}
@@ -40,6 +49,19 @@ func (rt *Router) Setup() http.Handler {
w.Write([]byte(`{"status":"healthy"}`))
})
// Swagger documentation
r.Get("/swagger/*", httpSwagger.Handler(
httpSwagger.URL("/swagger/doc.json"),
))
// Web UI static files
if rt.webEnabled {
fileServer := http.FileServer(http.Dir(rt.webDir))
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
fileServer.ServeHTTP(w, r)
})
}
// API routes
r.Route("/api/v1", func(r chi.Router) {
// 仓库管理
@@ -47,6 +69,7 @@ func (rt *Router) Setup() http.Handler {
r.Post("/batch", rt.repoHandler.AddBatch)
r.Get("/", rt.repoHandler.List)
r.Get("/{id}", rt.repoHandler.Get)
r.Get("/{id}/branches", rt.repoHandler.GetBranches)
r.Post("/{id}/switch-branch", rt.repoHandler.SwitchBranch)
r.Post("/{id}/update", rt.repoHandler.Update)
r.Post("/{id}/reset", rt.repoHandler.Reset)
@@ -58,6 +81,15 @@ func (rt *Router) Setup() http.Handler {
r.Post("/calculate", rt.statsHandler.Calculate)
r.Get("/result", rt.statsHandler.QueryResult)
r.Get("/commit-count", rt.statsHandler.CountCommits)
r.Get("/caches", rt.statsHandler.ListCaches)
r.Delete("/caches/clear", rt.statsHandler.ClearAllCaches)
})
// 任务
r.Route("/tasks", func(r chi.Router) {
r.Get("/", rt.taskHandler.List)
r.Delete("/clear", rt.taskHandler.ClearAllTasks)
r.Delete("/clear-completed", rt.taskHandler.ClearCompletedTasks)
})
})