name: handlers-and-http description: Implement HTTP handlers that parse requests, invoke use cases, and format responses following REST conventions.
Handlers & HTTP Integration
When to use this skill
- Create HTTP endpoint handlers for authentication flows
- Parse HTTP requests into domain types
- Handle HTTP-specific concerns (status codes, headers, serialization)
- Return properly formatted JSON responses
- Map domain errors returned from services to HTTP status codes
- Keep handlers thin by delegating to use cases
Key principles
- Thin handlers: Business logic lives in services, not handlers.
- Service coordination: Handlers invoke services which are interfaces
- HTTP boundaries: Handle only HTTP serialization and status codes, as well as request parsing and validation.
- Error mapping: Map domain errors returned from services to HTTP status codes via
constants/errors.go. - Context propagation: Pass request context to services
Pattern
Handlers are the HTTP boundary:
- Define handler struct with service dependency. If a handler needs multiple unrelated services, create a use case struct instead. For example, a
RegisterHandlerneeding bothUserServiceandEmailServiceshould delegate to aRegisterUseCasethat imports both interfaces, keeping the handler thin and focused on HTTP concerns. - Implement
Handlemethod withhttp.HandlerFunctype. - Parse and validate request. Always include a
Validate()method on request structs to clean up input but do not trim sensitive fields like passwords, tokens, etc. Before trimming fields on the request struct, check that pointer fields are not nil to avoid dereferencing nil pointers. For example, if you have a request struct with a pointer field likeEmail *string, you should check ifEmailis not nil before callingstrings.TrimSpace()on it within theValidate()method. This ensures that you don't encounter a runtime panic due to dereferencing a nil pointer. - Invoke use case/service
- Map response to HTTP (status code, headers, JSON) using
reqCtx.SetJSONResponse. - Handle errors consistently
- Always make use of the code within the
internal/folder as there are utilities for request parsing, response formatting, and error handling. Avoid reinventing the wheel by utilising these utilities to ensure consistency across handlers.
Example
See examples/todo_handlers.go for:
- CreateTodoHandler parsing POST requests
- MarkTodoCompleteHandler patterns
- Error handling and response formatting
Common mistakes
- Putting business logic in handlers
- Handlers calling multiple services directly (use use cases instead)
- Not propagating request context to use cases
- Forgetting error handling
- Incorrect HTTP status codes and not using
reqCtx.SetJSONResponsefor consistent response formatting or not using it to return the response at all, which can lead to inconsistent responses and missing status codes. Always usereqCtx.SetJSONResponseto ensure that responses are consistently formatted and include the appropriate status codes. - Logging sensitive data (passwords, tokens)
References
- plugins/email-password/handlers/ - Handler examples
- internal/handlers/ - Core handler examples
- internal/router/ - HTTP utilities