chore: scaffold project workbench
This commit is contained in:
26
backend/internal/config/config.go
Normal file
26
backend/internal/config/config.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package config
|
||||
|
||||
import "os"
|
||||
|
||||
type Config struct {
|
||||
Env string
|
||||
DatabaseURL string
|
||||
StorageDir string
|
||||
SystemAIKey string
|
||||
}
|
||||
|
||||
func Load() Config {
|
||||
return Config{
|
||||
Env: getenv("APP_ENV", "development"),
|
||||
DatabaseURL: getenv("DATABASE_URL", "postgres://agent:agent@localhost:5432/agent?sslmode=disable"),
|
||||
StorageDir: getenv("STORAGE_DIR", "./data/files"),
|
||||
SystemAIKey: os.Getenv("SYSTEM_AI_KEY"),
|
||||
}
|
||||
}
|
||||
|
||||
func getenv(key string, fallback string) string {
|
||||
if value := os.Getenv(key); value != "" {
|
||||
return value
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
23
backend/internal/httpx/router.go
Normal file
23
backend/internal/httpx/router.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"senlinai-agent/backend/internal/config"
|
||||
)
|
||||
|
||||
func NewRouter(cfg config.Config) *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"})
|
||||
})
|
||||
|
||||
return router
|
||||
}
|
||||
21
backend/internal/httpx/router_test.go
Normal file
21
backend/internal/httpx/router_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"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())
|
||||
}
|
||||
Reference in New Issue
Block a user