Documentation
¶
Index ¶
- Variables
- func DependencyGet[T any](deps *Dependencies, key DependencyKey) (T, bool)
- func DependencyMustGet[T any](deps *Dependencies, key DependencyKey) T
- func ParseJsonBodyFromRequest[T any](request *http.Request) (T, error)
- func WriteErrorToResponse(response http.ResponseWriter, statusCode int, message string)
- func WriteJsonBodyToResponse[T any](response http.ResponseWriter, body T) error
- type App
- func (app *App) Run() error
- func (app *App) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (app *App) WithController(controller IController) *App
- func (app *App) WithControllerFactory(factory ControllerFactory) *App
- func (app *App) WithDependencies(deps *Dependencies) *App
- func (app *App) WithLogger(logger ILogger) *App
- func (app *App) WithMiddleware(mw Middleware) *App
- func (app *App) WithMux(mux *http.ServeMux) *App
- func (app *App) WithPort(port string) *App
- func (app *App) WithRoute(path string, handler http.Handler) *App
- func (app *App) WithScope(path string, scope *Scope) *App
- type Authenticator
- type AuthenticatorConfig
- type ControllerFactory
- type DefaultLogger
- func (l *DefaultLogger) Debug(v ...interface{})
- func (l *DefaultLogger) Debugf(format string, v ...interface{})
- func (l *DefaultLogger) Error(v ...interface{})
- func (l *DefaultLogger) Errorf(format string, v ...interface{})
- func (l *DefaultLogger) Fatal(v ...interface{})
- func (l *DefaultLogger) Fatalf(format string, v ...interface{})
- func (l *DefaultLogger) Info(v ...interface{})
- func (l *DefaultLogger) Infof(format string, v ...interface{})
- func (l *DefaultLogger) Log(v ...interface{})
- func (l *DefaultLogger) Logf(format string, v ...interface{})
- func (l *DefaultLogger) Trace(v ...interface{})
- func (l *DefaultLogger) Tracef(format string, v ...interface{})
- func (l *DefaultLogger) Warning(v ...interface{})
- func (l *DefaultLogger) Warningf(format string, v ...interface{})
- type Dependencies
- type DependencyKey
- type IController
- type ILogger
- type Middleware
- type Scope
Constants ¶
This section is empty.
Variables ¶
var AuthTokenKey = authTokenKeyType{}
var RequestIDKey = requestIDKeyType{}
Functions ¶
func DependencyGet ¶
func DependencyGet[T any](deps *Dependencies, key DependencyKey) (T, bool)
func DependencyMustGet ¶
func DependencyMustGet[T any](deps *Dependencies, key DependencyKey) T
func ParseJsonBodyFromRequest ¶ added in v0.0.3
func WriteErrorToResponse ¶ added in v0.0.3
func WriteErrorToResponse(response http.ResponseWriter, statusCode int, message string)
func WriteJsonBodyToResponse ¶ added in v0.0.3
func WriteJsonBodyToResponse[T any](response http.ResponseWriter, body T) error
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
func (*App) Run ¶
Run starts to listen the HTTP server on the specified port. It applies all registered middleware to the handler. It returns an error if the server fails to start.
func (*App) ServeHTTP ¶
func (app *App) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the http.Handler interface. It allows the App to be used as a handler in an HTTP server. It delegates the request handling to the ServeMux.
func (*App) WithController ¶
func (app *App) WithController(controller IController) *App
WithController registers a controller with the application. It calls the RegisterRoutes method of the controller to set up its routes. If the controller is nil, it logs an error and does not register it. Controller routes that are registered directly to the App will use all the middleware registered to the app.
func (*App) WithControllerFactory ¶
func (app *App) WithControllerFactory(factory ControllerFactory) *App
WithControllerFactory registers a controller using a factory function. The factory is called with the application's dependencies to create the controller. If the factory returns nil, it logs an error and does not register the controller. This method allows for dynamic controller creation based on the application's dependencies. This method should be used if you are not manually bootstrapping the controller.
func (*App) WithDependencies ¶
func (app *App) WithDependencies(deps *Dependencies) *App
func (*App) WithLogger ¶
WithLogger sets the logger for the application. If the logger is nil, it logs a warning and uses the default logger. This method allows the application to use a custom logger for logging messages.
func (*App) WithMiddleware ¶
func (app *App) WithMiddleware(mw Middleware) *App
WithMiddleware registers a middleware function to the application. If the middleware is nil, it logs a warning and does not register it. This method is used to add middleware that can modify the request/response cycle. The middleware will be applied to all routes handled by the application.
func (*App) WithMux ¶ added in v0.0.3
WithMux sets the ServeMux for the application. If the provided mux is nil, it logs a warning and uses the existing mux. This method allows the application to use a custom ServeMux for routing. A default mux which is a simple http.ServeMux is created when the app is initialized.
func (*App) WithPort ¶
WithPort sets the port for the application. If the port is empty, it logs a warning and defaults to "8080". This method allows the application to listen on a specific port for incoming HTTP requests.
func (*App) WithRoute ¶
WithRoute registers a handler for a specific path. It ensures the path starts and ends with a slash. If the handler is nil, it logs a warning and does not register the route. This method is used to add routes outside of controllers. The it will use all the middleware registered to the app.
type Authenticator ¶
type Authenticator[T jwt.Claims] struct { *AuthenticatorConfig }
func NewAuthenticator ¶
func NewAuthenticator[T jwt.Claims](config *AuthenticatorConfig) *Authenticator[T]
func (*Authenticator[T]) GenerateToken ¶
func (a *Authenticator[T]) GenerateToken(claims T) (string, error)
GenerateToken creates a new JWT token with the provided claims, signs it, and encrypts it. The token is signed using the configured key and encrypted using JWE with RSA-OAEP and A128GCM. The generated token is suitable for use in authentication and authorization processes.
func (*Authenticator[T]) ParseToken ¶
func (a *Authenticator[T]) ParseToken(token string, claims T) (*jwt.Token, error)
ParseToken decrypts the token and parses it into the provided claims. It does not validate the claims, allowing for custom validation logic to be applied later.
func (*Authenticator[T]) VerifyToken ¶
func (a *Authenticator[T]) VerifyToken(token string, claims T) (T, error)
VerifyToken decrypts the token, parses it, and validates the claims. It checks the audience and issuer against the configured values. If the token is valid, it returns the claims; otherwise, it returns an error. This method is used to ensure that the token is valid and can be trusted for authentication.
type AuthenticatorConfig ¶
type AuthenticatorConfig struct {
JWEPrivateKey *rsa.PrivateKey
Lifetime time.Duration
Issuer string
Audience []string
Key string
}
func LoadAuthenticatorConfigFromEnv ¶
func LoadAuthenticatorConfigFromEnv() (*AuthenticatorConfig, error)
func NewAuthenticatorConfig ¶
func NewAuthenticatorConfig(jwePrivateKey *rsa.PrivateKey, lifetime time.Duration, issuer string, audience []string, key string) *AuthenticatorConfig
func (*AuthenticatorConfig) Validate ¶
func (config *AuthenticatorConfig) Validate() error
type ControllerFactory ¶
type ControllerFactory func(*Dependencies) IController
type DefaultLogger ¶
type DefaultLogger struct {
// contains filtered or unexported fields
}
func (*DefaultLogger) Debug ¶
func (l *DefaultLogger) Debug(v ...interface{})
func (*DefaultLogger) Debugf ¶
func (l *DefaultLogger) Debugf(format string, v ...interface{})
func (*DefaultLogger) Error ¶
func (l *DefaultLogger) Error(v ...interface{})
func (*DefaultLogger) Errorf ¶
func (l *DefaultLogger) Errorf(format string, v ...interface{})
func (*DefaultLogger) Fatal ¶
func (l *DefaultLogger) Fatal(v ...interface{})
func (*DefaultLogger) Fatalf ¶
func (l *DefaultLogger) Fatalf(format string, v ...interface{})
func (*DefaultLogger) Info ¶
func (l *DefaultLogger) Info(v ...interface{})
func (*DefaultLogger) Infof ¶
func (l *DefaultLogger) Infof(format string, v ...interface{})
func (*DefaultLogger) Log ¶
func (l *DefaultLogger) Log(v ...interface{})
func (*DefaultLogger) Logf ¶
func (l *DefaultLogger) Logf(format string, v ...interface{})
func (*DefaultLogger) Trace ¶
func (l *DefaultLogger) Trace(v ...interface{})
func (*DefaultLogger) Tracef ¶
func (l *DefaultLogger) Tracef(format string, v ...interface{})
func (*DefaultLogger) Warning ¶
func (l *DefaultLogger) Warning(v ...interface{})
func (*DefaultLogger) Warningf ¶
func (l *DefaultLogger) Warningf(format string, v ...interface{})
type Dependencies ¶
type Dependencies struct {
// contains filtered or unexported fields
}
func NewDependencies ¶
func NewDependencies() *Dependencies
func (*Dependencies) Set ¶
func (d *Dependencies) Set(key DependencyKey, value interface{})
type DependencyKey ¶
type DependencyKey string
type IController ¶
type IController interface {
// RegisterRoutes registers the controller's routes with the provided ServeMux.
// It is called by the Grove application to set up the HTTP routes for the controller.
RegisterRoutes(mux *http.ServeMux)
}
IController defines the interface for controllers in the Grove application. Controllers implement this interface to register their routes with the application's HTTP multiplexer. The RegisterRoutes method is called with the application's ServeMux and Dependencies to set up the routes and any necessary dependencies for the controller.
type ILogger ¶
type ILogger interface {
Log(v ...interface{})
Logf(format string, v ...interface{})
Info(v ...interface{})
Infof(format string, v ...interface{})
Error(v ...interface{})
Errorf(format string, v ...interface{})
Debug(v ...interface{})
Debugf(format string, v ...interface{})
Warning(v ...interface{})
Warningf(format string, v ...interface{})
Trace(v ...interface{})
Tracef(format string, v ...interface{})
Fatal(v ...interface{})
Fatalf(format string, v ...interface{})
}
func NewDefaultLogger ¶
type Middleware ¶
Middleware defines a function type that takes an http.Handler and returns an http.Handler. This allows for chaining middleware functions in the Grove application.
func DefaultAuthMiddleware ¶
func DefaultAuthMiddleware[T jwt.Claims](authenticator *Authenticator[T], logger ILogger, claimsFactory func() T) Middleware
DefaultAuthMiddleware is a middleware that provides default authentication logic. It checks for a valid token in the request header and denies access if the token is missing
func DefaultRequestLoggerMiddleware ¶
func DefaultRequestLoggerMiddleware(logger ILogger) Middleware
DefaultRequestLoggerMiddleware logs the request details including a unique request ID. It uses the Logger instance to log the start and completion of each request. The request ID is generated using the uuid package and is stored in the request context. This middleware can be used to trace requests through the application. It logs the request method, URL, and duration of the request. This is useful for debugging and monitoring purposes.
type Scope ¶
type Scope struct {
// contains filtered or unexported fields
}
func (*Scope) WithController ¶ added in v0.0.2
func (s *Scope) WithController(controller IController) *Scope
func (*Scope) WithMiddleware ¶ added in v0.0.2
func (s *Scope) WithMiddleware(mw Middleware) *Scope
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
grove
command
|
|
|
examples
|
|
|
authenticator
command
|
|
|
dependency_injection
command
|
|
|
parsing_body
command
|
|
|
scoped_routes
command
|
|
|
skeleton
command
|