49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
// Package app is the composition root: it builds every module with its
|
|
// stores and mounts them on one HTTP engine. Both the server binary and the
|
|
// integration tests start from here.
|
|
package app
|
|
|
|
import (
|
|
"log/slog"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"messenger/internal/auth"
|
|
"messenger/internal/chat"
|
|
chatstore "messenger/internal/chat/store"
|
|
"messenger/internal/health"
|
|
"messenger/internal/platform/httpx"
|
|
"messenger/internal/user"
|
|
userstore "messenger/internal/user/store"
|
|
)
|
|
|
|
type App struct {
|
|
Engine *gin.Engine
|
|
Health *health.Handler
|
|
}
|
|
|
|
// Deps are the shared resources the modules are built on; their lifetime is
|
|
// owned by the caller.
|
|
type Deps struct {
|
|
Log *slog.Logger
|
|
DB *pgxpool.Pool
|
|
MaxBodyBytes int64
|
|
}
|
|
|
|
func New(d Deps) *App {
|
|
users := user.NewService(userstore.NewUsers(d.DB))
|
|
chats := chat.NewService(chatstore.NewChats(d.DB), chatstore.NewMessages(d.DB), users)
|
|
|
|
engine := httpx.NewEngine(d.Log, d.MaxBodyBytes)
|
|
probes := health.New()
|
|
probes.Register(engine)
|
|
|
|
public := engine.Group("/api/v1")
|
|
protected := public.Group("", auth.RequireUser(users))
|
|
user.NewHandler(users).Register(public, protected)
|
|
chat.NewHandler(chats).Register(protected)
|
|
|
|
return &App{Engine: engine, Health: probes}
|
|
}
|