Documentation
¶
Index ¶
- Constants
- Variables
- type Controller
- type Module
- type ModuleOption
- type Option
- func WithGinMode(mode string) Option
- func WithIdleTimeout(d time.Duration) Option
- func WithLogger(logger *slog.Logger) Option
- func WithMiddlewares(middlewares ...gin.HandlerFunc) Option
- func WithPort(port int) Option
- func WithReadHeaderTimeout(d time.Duration) Option
- func WithReadTimeout(d time.Duration) Option
- func WithRecovery(recovery gin.HandlerFunc) Option
- func WithShutdownTimeout(d time.Duration) Option
- func WithWriteTimeout(d time.Duration) Option
- type Runner
- type Server
- func (server *Server) AddModules(m ...Module)
- func (server *Server) Cors(opts ...func(*cors2.Option))
- func (server *Server) GinEngine() *gin.Engine
- func (server *Server) Handler() http.Handler
- func (server *Server) InitModules()
- func (server *Server) Modules() []Module
- func (server *Server) Port() int
- func (server *Server) Run() error
- func (server *Server) RunContext(ctx context.Context) error
- func (server *Server) SetModules(m ...Module)
- type SolaController
- type SolaModule
- func (m *SolaModule) AddControllers(c ...Controller)
- func (m *SolaModule) AddPostMiddleware(middleware gin.HandlerFunc)
- func (m *SolaModule) AddPreMiddleware(middleware gin.HandlerFunc)
- func (m *SolaModule) Controllers() []Controller
- func (m *SolaModule) PostMiddlewares() []gin.HandlerFunc
- func (m *SolaModule) PreMiddlewares() []gin.HandlerFunc
- func (m *SolaModule) SetControllers(c ...Controller)
- func (m *SolaModule) SetPostMiddlewares(middlewares ...gin.HandlerFunc)
- func (m *SolaModule) SetPreMiddlewares(middlewares ...gin.HandlerFunc)
- func (m *SolaModule) SetRoutes(router *gin.RouterGroup)
- func (m *SolaModule) Uri() string
- type SolaService
Constants ¶
const ( DefaultReadHeaderTimeout = 10 * time.Second DefaultReadTimeout = 30 * time.Second DefaultWriteTimeout = 0 DefaultIdleTimeout = 120 * time.Second DefaultShutdownTimeout = 10 * time.Second )
Default http.Server timeouts applied by NewSolanum. WriteTimeout is unlimited by default so streaming responses (SSE, downloads) keep working.
Variables ¶
var ErrPortNotConfigured = errors.New("solanum: server port is not configured; use WithPort")
ErrPortNotConfigured is returned by Run when no valid port is set.
Functions ¶
This section is empty.
Types ¶
type Controller ¶
type Controller interface {
// Handlers returns the slice of service definitions this controller manages.
Handlers() []*SolaService
}
Controller declares a set of service handlers for a logical grouping of routes. SolaController is the default implementation.
type Module ¶
type Module interface {
// Uri returns the base URI path for this module (e.g., "/users").
Uri() string
// SetRoutes binds the module's controllers, middleware, and routes onto a RouterGroup.
SetRoutes(router *gin.RouterGroup)
}
Module represents a self-contained HTTP module mounted under its own URI prefix. SolaModule is the default implementation with controllers and pre/post middlewares.
type ModuleOption ¶
type ModuleOption func(*SolaModule) error
ModuleOption configures a SolaModule. NewModule panics when an option returns an error.
func WithUri ¶ added in v0.4.4
func WithUri(uri string) ModuleOption
WithUri sets the module's base URI prefix. It must be empty or start with '/'.
type Option ¶
type Option func(*Server)
Option configures a Server.
func WithGinMode ¶
WithGinMode sets the global gin mode (gin.DebugMode, gin.ReleaseMode, gin.TestMode) before the engine is created. Note that gin mode is process-wide.
func WithIdleTimeout ¶
WithIdleTimeout sets http.Server.IdleTimeout (zero falls back to the read timeout).
func WithLogger ¶
WithLogger sets the logger used by the framework itself (defaults to slog.Default()). Passing nil silences framework logs.
func WithMiddlewares ¶
func WithMiddlewares(middlewares ...gin.HandlerFunc) Option
WithMiddlewares appends application-wide middlewares (logger, authentication, ...) that run before every module's middlewares.
func WithReadHeaderTimeout ¶
WithReadHeaderTimeout sets http.Server.ReadHeaderTimeout (zero means no timeout).
func WithReadTimeout ¶
WithReadTimeout sets http.Server.ReadTimeout (zero means no timeout).
func WithRecovery ¶
func WithRecovery(recovery gin.HandlerFunc) Option
WithRecovery replaces the default gin.Recovery() middleware (e.g. with gin.CustomRecovery). Passing nil disables panic recovery.
func WithShutdownTimeout ¶
WithShutdownTimeout sets how long graceful shutdown waits for in-flight requests.
func WithWriteTimeout ¶
WithWriteTimeout sets http.Server.WriteTimeout (zero means no timeout).
type Runner ¶
type Runner interface {
// InitModules sets up all registered modules and their routes (idempotent).
InitModules()
// Handler initializes module routes (once) and returns an http.Handler for tests or custom servers.
Handler() http.Handler
// Modules returns a slice of pointers to registered Modules.
Modules() []Module
// SetModules replaces all registered Modules with the provided list.
SetModules(m ...Module)
// AddModules appends one or more Modules to the Runner.
AddModules(m ...Module)
// GinEngine exposes the underlying *gin.Engine for custom setup.
GinEngine() *gin.Engine
// Port exposes the configured port for the HTTP server.
Port() int
// Cors applies CORS configuration to the Gin engine using functional options.
// It panics when the options are insecure (e.g. credentials with all origins)
// or when called after routes are initialized.
Cors(opts ...func(*cors.Option))
// Run boots the HTTP server, initializing modules and listening on the configured port.
// It shuts down gracefully on SIGINT/SIGTERM and returns an error when serving fails.
Run() error
// RunContext is like Run but shuts down gracefully when ctx is done.
RunContext(ctx context.Context) error
}
Runner is the application entrypoint interface for Solanum. It manages module initialization, CORS, and server start. Global middlewares are configured with NewSolanum options (WithMiddlewares, WithRecovery).
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server implements the Runner interface and drives the application startup. It holds the Gin engine, listening port, and registered modules.
func NewSolanum ¶
NewSolanum creates and returns a new Server configured with the given options. gin.Recovery() is registered by default; see WithRecovery.
func (*Server) AddModules ¶
AddModules appends one or more Module implementations to the Runner.
func (*Server) Cors ¶
Cors applies configured CORS settings to the Gin engine using the cors middleware. Accepts functional options for customizing allowed origins, methods, headers, etc. It panics when the options are insecure (e.g. credentials with all origins) or when called after routes are initialized.
func (*Server) GinEngine ¶
GinEngine returns the underlying *gin.Engine for direct access and customization.
func (*Server) Handler ¶
Handler initializes module routes (once) and returns the server as an http.Handler. Useful for tests with httptest or for embedding into a custom http.Server.
func (*Server) InitModules ¶
func (server *Server) InitModules()
InitModules sets up routing groups for each Module and applies their routes. It is idempotent: routes are registered only on the first call.
func (*Server) Run ¶
Run initializes all modules and serves HTTP on the configured port until SIGINT or SIGTERM is received, then shuts down gracefully.
func (*Server) RunContext ¶
RunContext initializes all modules and serves HTTP on the configured port until ctx is done, then shuts down gracefully within the shutdown timeout. It returns nil after a graceful shutdown and an error when the server fails to start or stop.
func (*Server) SetModules ¶
SetModules replaces all registered Module implementations with the provided list.
type SolaController ¶
type SolaController struct {
// contains filtered or unexported fields
}
SolaController groups one or more SolaService handlers under a logical controller. It implements the Controller interface.
func NewController ¶
func NewController() *SolaController
NewController constructs an empty SolaController ready to receive handlers.
func (*SolaController) Handlers ¶
func (ctr *SolaController) Handlers() []*SolaService
Handlers returns the slice of SolaService entries managed by this controller.
func (*SolaController) SetHandlers ¶
func (ctr *SolaController) SetHandlers(handlers ...*SolaService)
SetHandlers replaces the controller's handler list with the provided entries.
type SolaModule ¶
type SolaModule struct {
// contains filtered or unexported fields
}
SolaModule encapsulates a self-contained HTTP module with its own URI prefix, controllers, and middleware stacks.
func NewModule ¶
func NewModule(opts ...ModuleOption) *SolaModule
NewModule creates a new SolaModule with the given URI prefix. The module starts with empty controller and middleware lists.
func (*SolaModule) AddControllers ¶
func (m *SolaModule) AddControllers(c ...Controller)
AddControllers appends one or more Controller implementations to this module.
func (*SolaModule) AddPostMiddleware ¶ added in v0.3.2
func (m *SolaModule) AddPostMiddleware(middleware gin.HandlerFunc)
AddPostMiddleware appends a single middleware to the post-handler chain.
func (*SolaModule) AddPreMiddleware ¶ added in v0.3.2
func (m *SolaModule) AddPreMiddleware(middleware gin.HandlerFunc)
AddPreMiddleware appends a single middleware to the pre-handler chain.
func (*SolaModule) Controllers ¶
func (m *SolaModule) Controllers() []Controller
Controllers returns all controllers registered in this module.
func (*SolaModule) PostMiddlewares ¶ added in v0.3.0
func (m *SolaModule) PostMiddlewares() []gin.HandlerFunc
PostMiddlewares returns the list of middleware to execute after handlers.
func (*SolaModule) PreMiddlewares ¶ added in v0.3.0
func (m *SolaModule) PreMiddlewares() []gin.HandlerFunc
PreMiddlewares returns the list of middleware to execute before handlers.
func (*SolaModule) SetControllers ¶
func (m *SolaModule) SetControllers(c ...Controller)
SetControllers registers one or more Controller implementations for this module.
func (*SolaModule) SetPostMiddlewares ¶ added in v0.4.0
func (m *SolaModule) SetPostMiddlewares(middlewares ...gin.HandlerFunc)
SetPostMiddlewares replaces the post-handler middleware chain with the provided list.
func (*SolaModule) SetPreMiddlewares ¶ added in v0.4.0
func (m *SolaModule) SetPreMiddlewares(middlewares ...gin.HandlerFunc)
SetPreMiddlewares replaces the pre-handler middleware chain with the provided list.
func (*SolaModule) SetRoutes ¶
func (m *SolaModule) SetRoutes(router *gin.RouterGroup)
SetRoutes registers the module's routes and middleware on the given RouterGroup. The final handler chain per route is: module pre-middlewares → service PreHandlers → handler → service PostHandlers → module post-middlewares. Post handlers run after the rest of the chain completes, even when it was aborted (check c.IsAborted() to tell); they are skipped only when the chain panics. It panics when a service is nil, has no Handler, or has no Method.
func (*SolaModule) Uri ¶
func (m *SolaModule) Uri() string
Uri returns the base URI prefix for this module.
type SolaService ¶
type SolaService struct {
// Uri is the route path relative to the module prefix (e.g., "/:id").
Uri string
// Method is the HTTP method to bind (GET, POST, etc.).
Method string
// PreHandlers are middleware functions to run before Handler for this route only.
PreHandlers []gin.HandlerFunc
// Handler is the Gin handler function to execute.
Handler gin.HandlerFunc
// PostHandlers are middleware functions to run after Handler for this route only.
// They run even when a pre-handler or Handler calls ctx.Abort*() (check ctx.IsAborted()),
// so they suit logging and metrics. The response may already be written by then,
// so changing the status code or headers here has no effect.
PostHandlers []gin.HandlerFunc
}
SolaService represents a single HTTP route handler configuration.