The api package implements the API layer of the application, providing a structured approach to manage HTTP servers, routes, handlers, and request/response mappings.
The Server struct manages the lifecycle of the HTTP server. It is responsible for:
- Listening for incoming requests.
- Serving responses via the http.Server.
- Gracefully shutting down the server when required.
This component is highly configurable, allowing you to set:
- Server address and port.
- Read and write timeouts.
The default router is based on the lightweight Chi router, which can be replaced with any router implementing the http.Handler interface.
Features:
- Route Initialization: Configures application routes and middleware.
- Centralized Error Handling: Provides a unified approach for managing errors across routes.
The Handler
struct serves as a generic interface for handling incoming HTTP requests. It decouples business logic from the HTTP layer by:
- Validating and passing the request's context and inputs to the service functions.
- Returning a response or error from the service functions.
How to Use:
The Mappers struct simplifies the process of translating HTTP requests into service-layer inputs and mapping service outputs to HTTP responses.
Key Features:
- No need to create new handlers for each endpoint.
- Implement the following interfaces for custom mappings:
Examples:
The mappers package includes examples for handling various types of requests and responses.