de30497f59
- Implemented tenant and user management features including: - Tenant creation and management with associated migrations. - User creation and management with associated migrations. - Tenant membership management with associated migrations. - Platform user roles management with associated migrations. - Quota management with associated migrations. - Article and template management with associated migrations. - Added HTTP handlers for templates and workspaces. - Created tests for protected and public routes. - Introduced a script to check tenant scope in SQL queries. - Documented task plan for backend completion and frontend foundation.
109 lines
3.0 KiB
Go
109 lines
3.0 KiB
Go
package bootstrap
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
goredis "github.com/redis/go-redis/v9"
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/geo-platform/tenant-api/internal/shared/auth"
|
|
"github.com/geo-platform/tenant-api/internal/shared/config"
|
|
"github.com/geo-platform/tenant-api/internal/shared/llm"
|
|
"github.com/geo-platform/tenant-api/internal/shared/middleware"
|
|
"github.com/geo-platform/tenant-api/internal/shared/observability"
|
|
"github.com/geo-platform/tenant-api/internal/shared/repository/postgres"
|
|
"github.com/geo-platform/tenant-api/internal/shared/repository/redis"
|
|
"github.com/geo-platform/tenant-api/internal/shared/response"
|
|
"github.com/geo-platform/tenant-api/internal/shared/stream"
|
|
)
|
|
|
|
type App struct {
|
|
Config *config.Config
|
|
Logger *zap.Logger
|
|
DB *pgxpool.Pool
|
|
Redis *goredis.Client
|
|
Engine *gin.Engine
|
|
JWT *auth.Manager
|
|
Sessions *auth.SessionStore
|
|
LLM llm.Client
|
|
GenerationStreams *stream.GenerationHub
|
|
}
|
|
|
|
func New(configPath string) (*App, error) {
|
|
cfg, err := config.Load(configPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("load config: %w", err)
|
|
}
|
|
|
|
logger, err := observability.NewLogger(cfg.Log.Level, cfg.Log.Format)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("init logger: %w", err)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
pool, err := postgres.NewPool(ctx, cfg.Database)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("init postgres: %w", err)
|
|
}
|
|
|
|
rdb, err := redis.NewClient(ctx, cfg.Redis)
|
|
if err != nil {
|
|
pool.Close()
|
|
return nil, fmt.Errorf("init redis: %w", err)
|
|
}
|
|
|
|
jwtMgr := auth.NewManager(cfg.JWT.Secret, cfg.JWT.AccessTTL, cfg.JWT.RefreshTTL)
|
|
sessions := auth.NewSessionStore(rdb)
|
|
llmClient := llm.New(cfg.LLM)
|
|
generationStreams := stream.NewGenerationHub()
|
|
|
|
if cfg.Server.Mode == "release" {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
}
|
|
engine := gin.New()
|
|
|
|
engine.Use(
|
|
middleware.Recovery(logger),
|
|
middleware.RequestID(),
|
|
middleware.Logger(logger),
|
|
middleware.CORS(),
|
|
)
|
|
|
|
engine.GET("/api/health/live", func(c *gin.Context) {
|
|
response.Success(c, gin.H{"status": "alive"})
|
|
})
|
|
engine.GET("/api/health/ready", func(c *gin.Context) {
|
|
if err := pool.Ping(c.Request.Context()); err != nil {
|
|
response.Error(c, response.ErrServiceUnavailable(50301, "postgres_unavailable", "database is not reachable"))
|
|
return
|
|
}
|
|
if err := rdb.Ping(c.Request.Context()).Err(); err != nil {
|
|
response.Error(c, response.ErrServiceUnavailable(50302, "redis_unavailable", "redis is not reachable"))
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"status": "ready"})
|
|
})
|
|
|
|
return &App{
|
|
Config: cfg,
|
|
Logger: logger,
|
|
DB: pool,
|
|
Redis: rdb,
|
|
Engine: engine,
|
|
JWT: jwtMgr,
|
|
Sessions: sessions,
|
|
LLM: llmClient,
|
|
GenerationStreams: generationStreams,
|
|
}, nil
|
|
}
|
|
|
|
func (a *App) Close() {
|
|
a.DB.Close()
|
|
_ = a.Redis.Close()
|
|
_ = a.Logger.Sync()
|
|
}
|