Files
pipu-music/services/api/internal/server/router.go
T
mrleo1nidandClaude Opus 5.5 f075863f15
ci / mobile / mobile (Metro-бандл Android) (push) Successful in 47s
ci / ts / ts (packages + web) (push) Successful in 38s
ci / api / api (push) Successful in 18s
ci / contract / contract (сгенерированный код совпадает с openapi.yaml) (push) Successful in 35s
feat(api): user management and audit log for admins (M1)
- /admin/users: list with search, role filter and cursor pages; create
  with a one-time temporary password; rename, change role, block;
  reset password; delete
- every change is checked against the role policy, a forbidden field
  rejects the whole patch; the last active admin is locked in the
  transaction and cannot be demoted, blocked or deleted
- role change and blocking revoke access tokens at once; blocking and
  password reset also end all sessions
- /admin/audit: newest-first journal with actor names
- path and query parameter errors are problem+json now

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 06:18:02 +03:00

66 lines
2.1 KiB
Go

// Package server собирает HTTP-роутер сервиса api из модулей.
package server
import (
"log/slog"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"pipu/api/internal/api/gen"
"pipu/api/internal/auth"
"pipu/api/internal/platform/httpx"
"pipu/api/internal/system"
)
// Deps — модули, реализующие части контракта API.
type Deps struct {
Log *slog.Logger
System *system.Handler
Auth *auth.Handler
// AuthService проверяет access-токены в middleware. nil — только для тестов служебных эндпоинтов.
AuthService *auth.Service
}
// handlers объединяет модули в единую реализацию gen.StrictServerInterface.
// По мере появления модулей (catalog, ...) они встраиваются сюда.
// Алиасы дают встроенным полям разные имена: у модулей одинаковое имя типа Handler.
type (
systemHandler = system.Handler
authHandler = auth.Handler
)
type handlers struct {
*systemHandler
*authHandler
}
var _ gen.StrictServerInterface = handlers{}
// NewRouter возвращает корневой http.Handler.
func NewRouter(d Deps) http.Handler {
log := d.Log
if log == nil {
log = slog.New(slog.DiscardHandler)
}
r := chi.NewRouter()
r.Use(middleware.RequestID, httpx.RealIP, httpx.AccessLog(log), middleware.Recoverer)
if d.AuthService != nil {
r.Use(auth.Middleware(d.AuthService, log))
}
r.NotFound(httpx.NotFound)
r.MethodNotAllowed(httpx.MethodNotAllowed)
strict := gen.NewStrictHandlerWithOptions(handlers{systemHandler: d.System, authHandler: d.Auth}, nil, gen.StrictHTTPServerOptions{
RequestErrorHandlerFunc: httpx.RequestError,
ResponseErrorHandlerFunc: httpx.ResponseError(log),
})
return gen.HandlerWithOptions(strict, gen.ChiServerOptions{
BaseRouter: r,
// Ошибки разбора path- и query-параметров — тоже problem+json, а не текст по умолчанию.
ErrorHandlerFunc: httpx.RequestError,
})
}