Files
iqudo-top1/codes/internal/httpserver/server.go
2026-08-06 21:48:42 +08:00

115 lines
3.9 KiB
Go

package httpserver
import (
"io/fs"
"mime"
"net/http"
"path"
"strings"
"git.iwork-ai.com/xdc/iqudo-top1/internal/auth"
"git.iwork-ai.com/xdc/iqudo-top1/internal/dashboard"
"git.iwork-ai.com/xdc/iqudo-top1/internal/knowledge"
"git.iwork-ai.com/xdc/iqudo-top1/internal/middleware"
runhandler "git.iwork-ai.com/xdc/iqudo-top1/internal/run"
"git.iwork-ai.com/xdc/iqudo-top1/internal/scenario"
"git.iwork-ai.com/xdc/iqudo-top1/internal/sop"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"gorm.io/gorm"
)
func New(db *gorm.DB, authService *auth.Service, frontend fs.FS, log *zap.Logger, environment string) http.Handler {
if environment == "prod" {
gin.SetMode(gin.ReleaseMode)
}
router := gin.New()
router.Use(middleware.RequestLogger(log.Named("http")), middleware.Recovery(log.Named("recovery")))
authHandler := auth.NewHandler(authService)
scenarioHandler := scenario.NewHandler(db)
sopHandler := sop.NewHandler(db)
runHandler := runhandler.NewHandler(db)
knowledgeHandler := knowledge.NewHandler(db)
dashboardHandler := dashboard.NewHandler(db)
api := router.Group("/api/v1")
api.GET("/health", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok"})
})
api.POST("/auth/login", authHandler.Login)
api.POST("/auth/refresh", authHandler.Refresh)
protected := api.Group("")
protected.Use(middleware.Authenticate(authService))
protected.GET("/auth/me", authHandler.Me)
protected.GET("/dashboard/summary", dashboardHandler.Summary)
protected.GET("/scenarios", scenarioHandler.List)
protected.POST("/scenarios", scenarioHandler.Create)
protected.GET("/scenarios/:id", scenarioHandler.Get)
protected.PUT("/scenarios/:id", scenarioHandler.Update)
protected.DELETE("/scenarios/:id", scenarioHandler.Archive)
protected.POST("/scenarios/:id/fields", scenarioHandler.CreateField)
protected.PUT("/scenario-fields/:fieldId", scenarioHandler.UpdateField)
protected.DELETE("/scenario-fields/:fieldId", scenarioHandler.DeleteField)
protected.GET("/sops", sopHandler.List)
protected.GET("/scenarios/:id/sops", sopHandler.List)
protected.POST("/scenarios/:id/sops", sopHandler.Create)
protected.GET("/sops/:id", sopHandler.Get)
protected.PUT("/sops/:id/draft", sopHandler.SaveGraph)
protected.POST("/sops/:id/validate", sopHandler.Validate)
protected.POST("/sops/:id/submit-review", sopHandler.SubmitReview)
protected.POST("/sops/:id/publish", sopHandler.Publish)
protected.POST("/sops/:id/offline", sopHandler.Offline)
protected.POST("/sops/:id/versions", sopHandler.CreateVersion)
protected.GET("/published-sops", runHandler.PublishedSOPs)
protected.GET("/runs", runHandler.List)
protected.POST("/runs", runHandler.Start)
protected.GET("/runs/:id", runHandler.Get)
protected.POST("/runs/:id/answer", runHandler.Answer)
protected.POST("/runs/:id/finish", runHandler.Finish)
protected.POST("/runs/:id/feedback", runHandler.Feedback)
protected.GET("/knowledge-cards", knowledgeHandler.List)
protected.POST("/knowledge-cards", knowledgeHandler.Create)
protected.DELETE("/knowledge-cards/:id", knowledgeHandler.Delete)
router.NoRoute(spaHandler(frontend))
return router
}
func spaHandler(frontend fs.FS) gin.HandlerFunc {
return func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/api/") {
c.JSON(http.StatusNotFound, gin.H{"code": "NOT_FOUND", "message": "接口不存在"})
return
}
name := strings.TrimPrefix(path.Clean(c.Request.URL.Path), "/")
if name == "" || name == "." {
name = "index.html"
}
data, err := fs.ReadFile(frontend, name)
if err != nil {
name = "index.html"
data, err = fs.ReadFile(frontend, name)
}
if err != nil {
c.Status(http.StatusNotFound)
return
}
contentType := mime.TypeByExtension(path.Ext(name))
if contentType == "" {
contentType = "application/octet-stream"
}
if name == "index.html" {
c.Header("Cache-Control", "no-cache")
} else {
c.Header("Cache-Control", "public, max-age=31536000, immutable")
}
c.Data(http.StatusOK, contentType, data)
}
}