add model identities and restore httpx router
This commit is contained in:
70
backend/internal/httpx/router.go
Normal file
70
backend/internal/httpx/router.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"senlinai-agent/backend/internal/config"
|
||||
"senlinai-agent/backend/internal/logic/auth"
|
||||
)
|
||||
|
||||
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.Use(cors())
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func cors() gin.HandlerFunc {
|
||||
allowedOrigins := map[string]bool{
|
||||
"http://localhost:5173": true,
|
||||
"http://127.0.0.1:5173": true,
|
||||
"http://tauri.localhost": true,
|
||||
}
|
||||
|
||||
return func(c *gin.Context) {
|
||||
origin := c.GetHeader("Origin")
|
||||
if allowedOrigins[origin] {
|
||||
c.Header("Access-Control-Allow-Origin", origin)
|
||||
c.Header("Vary", "Origin")
|
||||
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
|
||||
c.Header("Access-Control-Allow-Headers", "Authorization, Content-Type")
|
||||
c.Header("Access-Control-Max-Age", "86400")
|
||||
}
|
||||
|
||||
if c.Request.Method == http.MethodOptions {
|
||||
c.AbortWithStatus(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
71
backend/internal/httpx/router_test.go
Normal file
71
backend/internal/httpx/router_test.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/require"
|
||||
"senlinai-agent/backend/internal/config"
|
||||
)
|
||||
|
||||
func TestHealthz(t *testing.T) {
|
||||
router := NewRouter(config.Config{Env: "test"})
|
||||
req := httptest.NewRequest(http.MethodGet, "/healthz", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
router.ServeHTTP(rec, req)
|
||||
|
||||
require.Equal(t, http.StatusOK, rec.Code)
|
||||
require.JSONEq(t, `{"status":"ok"}`, rec.Body.String())
|
||||
}
|
||||
|
||||
func TestNewRouterRegistersFeatureRoutesUnderAPI(t *testing.T) {
|
||||
router := NewRouter(config.Config{Env: "test"}, testRegistrar{})
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/ping", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
router.ServeHTTP(rec, req)
|
||||
|
||||
require.Equal(t, http.StatusOK, rec.Code)
|
||||
require.JSONEq(t, `{"pong":true}`, rec.Body.String())
|
||||
}
|
||||
|
||||
func TestRouterAddsCORSHeadersForLocalWebClient(t *testing.T) {
|
||||
router := NewRouter(config.Config{Env: "test"}, testRegistrar{})
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/ping", nil)
|
||||
req.Header.Set("Origin", "http://localhost:5173")
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
router.ServeHTTP(rec, req)
|
||||
|
||||
require.Equal(t, http.StatusOK, rec.Code)
|
||||
require.Equal(t, "http://localhost:5173", rec.Header().Get("Access-Control-Allow-Origin"))
|
||||
require.Contains(t, rec.Header().Get("Access-Control-Allow-Headers"), "Authorization")
|
||||
}
|
||||
|
||||
func TestRouterHandlesCORSPreflightBeforeAuth(t *testing.T) {
|
||||
router := NewProtectedRouter(config.Config{Env: "test"}, func(token string) (uint, error) {
|
||||
return 0, http.ErrNoCookie
|
||||
}, testRegistrar{})
|
||||
req := httptest.NewRequest(http.MethodOptions, "/api/ping", nil)
|
||||
req.Header.Set("Origin", "http://localhost:5173")
|
||||
req.Header.Set("Access-Control-Request-Method", "GET")
|
||||
req.Header.Set("Access-Control-Request-Headers", "Authorization")
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
router.ServeHTTP(rec, req)
|
||||
|
||||
require.Equal(t, http.StatusNoContent, rec.Code)
|
||||
require.Equal(t, "http://localhost:5173", rec.Header().Get("Access-Control-Allow-Origin"))
|
||||
require.Contains(t, rec.Header().Get("Access-Control-Allow-Headers"), "Authorization")
|
||||
}
|
||||
|
||||
type testRegistrar struct{}
|
||||
|
||||
func (testRegistrar) Register(router gin.IRouter) {
|
||||
router.GET("/ping", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"pong": true})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user