Documentation
¶
Overview ¶
Package handler provides HTTP request handlers following a consistent pattern for dependency injection and configuration. Each handler type follows these patterns:
1. Options Pattern:
- Each handler accepts functional options for configuration
- Options are type-safe and immutable
- Dependencies are explicitly declared and validated
2. Base Handler:
- Provides common functionality (logging, error handling)
- Embedded in all specific handlers
- Requires explicit logger configuration
3. Validation:
- All handlers validate their dependencies before use
- Required dependencies are checked at startup
- Clear error messages for missing dependencies
Example Usage:
handler := NewContactHandler(logger,
WithContactServiceOpt(contactService),
)
For testing:
handler := NewContactHandler(testLogger,
WithContactServiceOpt(mockService),
)
Index ¶
Constants ¶
const ( SignupFirstNameMinValue = 2 SignupFirstNameMaxValue = 50 SignupLastNameMinValue = 2 SignupLastNameMaxValue = 50 SignupEmailMinValue = 5 SignupEmailMaxValue = 100 SignupPasswordMinValue = 8 SignupPasswordMaxValue = 100 )
Signup validation constants (for linter compliance)
const (
// CookieExpiryMinutes is the number of minutes before a cookie expires
CookieExpiryMinutes = 15
)
Variables ¶
var ( // ErrNoCurrentUser is returned when no user is found in the current context ErrNoCurrentUser = errors.New("no current user found") )
Functions ¶
This section is empty.
Types ¶
type AuthHandler ¶
type AuthHandler struct {
Base
// contains filtered or unexported fields
}
AuthHandler handles authentication related requests
func NewAuthHandler ¶
func NewAuthHandler(logger logging.Logger, opts ...AuthHandlerOption) *AuthHandler
NewAuthHandler creates a new auth handler
func (*AuthHandler) Register ¶
func (h *AuthHandler) Register(e *echo.Echo)
Register registers the auth routes
func (*AuthHandler) Validate ¶
func (h *AuthHandler) Validate() error
Validate validates that required dependencies are set
type AuthHandlerOption ¶
type AuthHandlerOption func(*AuthHandler)
AuthHandlerOption defines an auth handler option
func WithUserService ¶
func WithUserService(svc user.Service) AuthHandlerOption
WithUserService sets the user service
type Base ¶
Base provides common handler functionality that is embedded in all specific handlers. It enforces consistent logging and error handling patterns across all handlers.
func NewBase ¶
NewBase creates a new base handler with the provided options. The logger must be explicitly provided using WithLogger option. There is no default logger to ensure proper configuration.
func (*Base) LogError ¶
LogError provides consistent error logging across all handlers. It ensures errors are logged with proper context and additional fields.
type FormHandler ¶
type FormHandler struct {
// contains filtered or unexported fields
}
FormHandler handles form-related requests
func NewFormHandler ¶
func NewFormHandler( logger logging.Logger, formService form.Service, formClient form.Client, userService user.Service, ) (*FormHandler, error)
NewFormHandler creates a new form handler
func (*FormHandler) Register ¶
func (h *FormHandler) Register(e *echo.Echo)
Register sets up the routes for the form handler
type Option ¶
type Option func(*Base)
Option configures a Base handler. It follows the functional options pattern for clean and type-safe dependency injection.
func WithLogger ¶
WithLogger sets the logger for the handler. This is a required dependency for all handlers.
type SignupValidation ¶
type SignupValidation struct {
FirstNameMin int
FirstNameMax int
LastNameMin int
LastNameMax int
EmailMin int
EmailMax int
PasswordMin int
PasswordMax int
}
SignupValidation holds validation constants for signup
type StaticHandler ¶
type StaticHandler struct {
// contains filtered or unexported fields
}
StaticHandler handles serving static files
func NewStaticHandler ¶
func NewStaticHandler(logger logging.Logger, cfg *config.Config) *StaticHandler
NewStaticHandler creates a new static file handler
func (*StaticHandler) HandleStatic ¶
func (h *StaticHandler) HandleStatic(c echo.Context) error
HandleStatic serves static files
func (*StaticHandler) Register ¶
func (h *StaticHandler) Register(e *echo.Echo)
Register sets up routes for static file serving
type VersionHandler ¶
type VersionHandler struct {
Base
// contains filtered or unexported fields
}
VersionHandler handles version-related endpoints
func NewVersionHandler ¶
func NewVersionHandler(info VersionInfo, base Base) *VersionHandler
NewVersionHandler creates a new version handler
func (*VersionHandler) GetVersion ¶
func (h *VersionHandler) GetVersion(c echo.Context) error
GetVersion returns the application version information
func (*VersionHandler) Register ¶
func (h *VersionHandler) Register(e *echo.Echo)
Register registers the version routes
type VersionInfo ¶
type VersionInfo struct {
Version string `json:"version"`
BuildTime string `json:"buildTime"`
GitCommit string `json:"gitCommit"`
GoVersion string `json:"goVersion"`
}
VersionInfo contains build and version information
type WebHandler ¶
type WebHandler struct {
Base
// contains filtered or unexported fields
}
WebHandler handles web page requests. It requires a renderer, and subscription service to function properly. Use the functional options pattern to configure these dependencies.
Dependencies:
- renderer: Required for rendering web pages
- middlewareManager: Required for security and request processing
- config: Required for configuration
func NewWebHandler ¶
func NewWebHandler(logger logging.Logger, renderer *view.Renderer, opts ...WebHandlerOption) (*WebHandler, error)
NewWebHandler creates a new web handler. It uses the functional options pattern to configure the handler. The logger is required as a direct parameter, while other dependencies are provided through options.
Example:
handler := NewWebHandler(logger,
WithRenderer(renderer),
WithConfig(config),
)
func (*WebHandler) Register ¶
func (h *WebHandler) Register(e *echo.Echo)
Register registers the web routes (SRP: now just calls validateDependencies and registerRoutes)
func (*WebHandler) Validate ¶
func (h *WebHandler) Validate() error
Validate validates that required dependencies are set. Returns an error if any required dependency is missing.
Required dependencies:
- renderer
- middlewareManager
- config
type WebHandlerOption ¶
type WebHandlerOption func(*WebHandler)
WebHandlerOption defines a web handler option. This type is used to implement the functional options pattern for configuring the WebHandler.
func WithConfig ¶
func WithConfig(cfg *config.Config) WebHandlerOption
WithConfig sets the config for the web handler.
func WithMiddlewareManager ¶
func WithMiddlewareManager(manager *amw.Manager) WebHandlerOption
WithMiddlewareManager sets the middleware manager for the web handler.
func WithRenderer ¶
func WithRenderer(renderer *view.Renderer) WebHandlerOption
WithRenderer sets the view renderer. This is a required option for the WebHandler as it needs the renderer to display web pages.
Example:
handler := NewWebHandler(logger, WithRenderer(renderer))