package v1 import ( "context" "lincodex/backend/internal/adapter/primary/http/api" "lincodex/backend/internal/adapter/primary/http/problem" ) // Handler is the composite implementation of the generated api.Handler. // // The embedded api.UnimplementedHandler keeps the backend compiling after the // contract gains an operation that has no business implementation yet. Such an // operation returns ht.ErrNotImplemented without side effects. The generated // server passes that error to the ErrorHandler configured in the router, which // answers 501 with code not_implemented, before it would call NewError. // // The contract's common default response makes ogen add NewError to // api.Handler. The embedded api.UnimplementedHandler.NewError returns a zero // status code that the generated encoder sends as HTTP 200, so Handler defines // NewError explicitly: *ogenerrors.SecurityError, which the generated server // passes to it directly, becomes 401, and every other error becomes 500 with a // fixed body and is logged here, never exposing err.Error(). Errors of the // generated *api.ProblemStatusCode type are encoded before NewError is called, // so operations return them for expected failures such as 403 or 404. // // Each implemented operation gets an explicit method here that delegates to // the handler owning it. Each implemented operation also gets a router test // through v1.NewHandler that asserts a non-501 response, because renaming an // operationId would otherwise silently fall back to the embedded 501 method. type Handler struct { api.UnimplementedHandler health *HealthHandler identity *IdentityHandler problems *problem.Responder } var _ api.Handler = (*Handler)(nil) // HandlerOption adds the handler of a business area to Handler. type HandlerOption func(*Handler) // WithIdentity serves the registration operations. A nil handler leaves them // to the embedded api.UnimplementedHandler, as when no database is // configured. func WithIdentity(identity *IdentityHandler) HandlerOption { return func(handler *Handler) { handler.identity = identity } } func NewHandler(health *HealthHandler, problems *problem.Responder, options ...HandlerOption) *Handler { handler := &Handler{health: health, problems: problems} for _, option := range options { option(handler) } return handler } func (handler *Handler) GetHealthz(ctx context.Context) (*api.HealthResponse, error) { return handler.health.GetHealthz(ctx) } func (handler *Handler) RegisterAccount( ctx context.Context, request *api.RegistrationRequest, params api.RegisterAccountParams, ) (api.RegisterAccountRes, error) { if handler.identity == nil { return handler.UnimplementedHandler.RegisterAccount(ctx, request, params) } return handler.identity.RegisterAccount(ctx, request, params) } func (handler *Handler) ResendEmailVerification( ctx context.Context, request *api.EmailVerificationResendRequest, ) (api.ResendEmailVerificationRes, error) { if handler.identity == nil { return handler.UnimplementedHandler.ResendEmailVerification(ctx, request) } return handler.identity.ResendEmailVerification(ctx, request) } func (handler *Handler) ConfirmEmail( ctx context.Context, request *api.EmailVerificationRequest, params api.ConfirmEmailParams, ) (api.ConfirmEmailRes, error) { if handler.identity == nil || handler.identity.confirmation == nil { return handler.UnimplementedHandler.ConfirmEmail(ctx, request, params) } return handler.identity.ConfirmEmail(ctx, request, params) } // NewError implements api.Handler for errors returned by operations. func (handler *Handler) NewError(ctx context.Context, err error) *api.ProblemStatusCode { return handler.problems.HandlerError(ctx, err) }