refactor backend packages and global db service
This commit is contained in:
@@ -1,43 +0,0 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"senlinai-agent/backend/internal/auth"
|
||||
"senlinai-agent/backend/internal/config"
|
||||
)
|
||||
|
||||
type RouteRegistrar interface {
|
||||
Register(router gin.IRouter)
|
||||
}
|
||||
|
||||
func NewRouter(cfg config.Config, registrars ...RouteRegistrar) *gin.Engine {
|
||||
return newRouter(cfg, nil, registrars...)
|
||||
}
|
||||
|
||||
func NewProtectedRouter(cfg config.Config, tokenVerifier func(string) (uint, error), registrars ...RouteRegistrar) *gin.Engine {
|
||||
return newRouter(cfg, tokenVerifier, registrars...)
|
||||
}
|
||||
|
||||
func newRouter(cfg config.Config, tokenVerifier func(string) (uint, error), registrars ...RouteRegistrar) *gin.Engine {
|
||||
if cfg.Env == "test" {
|
||||
gin.SetMode(gin.TestMode)
|
||||
}
|
||||
|
||||
router := gin.New()
|
||||
router.Use(gin.Recovery())
|
||||
|
||||
router.GET("/healthz", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||
})
|
||||
api := router.Group("/api")
|
||||
if tokenVerifier != nil {
|
||||
api.Use(auth.RequireUser(tokenVerifier))
|
||||
}
|
||||
for _, registrar := range registrars {
|
||||
registrar.Register(api)
|
||||
}
|
||||
|
||||
return router
|
||||
}
|
||||
Reference in New Issue
Block a user