Documentation
¶
Overview ¶
Package app provides the IoC service container and application bootstrap.
Index ¶
- type Application
- func (a *Application) Boot() *Application
- func (a *Application) Env(key, def string) string
- func (a *Application) EnvName() string
- func (a *Application) Err() error
- func (a *Application) IsDebug() bool
- func (a *Application) IsProduction() bool
- func (a *Application) Load(files ...string) *Application
- func (a *Application) LoadDir(dir string) *Application
- func (a *Application) Register(providers ...ServiceProvider) *Application
- func (a *Application) Route(fn func(*routing.Router)) *Application
- func (a *Application) Serve() error
- func (a *Application) ServeAddr(addr string) error
- func (a *Application) SetLogger(l *slog.Logger) *Application
- func (a *Application) Use(mw ...onihttp.MiddlewareFunc) *Application
- type BindingFunc
- type Container
- func (c *Container) Alias(abstract, alias string)
- func (c *Container) Bind(abstract string, factory BindingFunc)
- func (c *Container) Bound(abstract string) bool
- func (c *Container) Flush()
- func (c *Container) Instance(abstract string, instance any)
- func (c *Container) Make(abstract string) (any, error)
- func (c *Container) MakeInto(abstract string, dest any) error
- func (c *Container) MustMake(abstract string) any
- func (c *Container) Singleton(abstract string, factory BindingFunc)
- func (c *Container) Tag(abstracts []string, tag string)
- func (c *Container) Tagged(tag string) ([]any, error)
- type ServiceProvider
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Application ¶
type Application struct {
*Container
Config *config.Config
Router *routing.Router
Logger *slog.Logger
// contains filtered or unexported fields
}
Application is the central OniWorks application object. It wires together the IoC container, config, router, and HTTP kernel.
func New ¶
func New() *Application
New creates and returns a new Application with sensible defaults.
func (*Application) Boot ¶
func (a *Application) Boot() *Application
Boot calls Boot() on all registered providers in registration order. Boot is called automatically by Serve; call it manually if you need services before starting the server (e.g. in CLI commands). Boot is safe for concurrent use and runs each provider's Boot exactly once.
func (*Application) Env ¶
func (a *Application) Env(key, def string) string
Env is a shortcut to read an environment variable with a fallback.
func (*Application) EnvName ¶
func (a *Application) EnvName() string
EnvName returns the application environment name (app.env config key, falling back to the APP_ENV environment variable).
func (*Application) Err ¶
func (a *Application) Err() error
Err returns the first configuration load error, if any. Serve returns it automatically; call Err directly when not using Serve (e.g. CLI commands).
func (*Application) IsDebug ¶
func (a *Application) IsDebug() bool
IsDebug reports whether the application is running in debug mode.
func (*Application) IsProduction ¶
func (a *Application) IsProduction() bool
IsProduction reports whether APP_ENV is "production".
func (*Application) Load ¶
func (a *Application) Load(files ...string) *Application
Load reads one or more config files (YAML, TOML) and/or .env files. Files are loaded in order; later values override earlier ones.
A load failure is recorded and fails the application fast: Serve/ServeAddr refuse to start and return the error (also available via Err). Running with silently-missing config is worse than not starting.
oni.Load("config/app.yaml", ".env")
func (*Application) LoadDir ¶
func (a *Application) LoadDir(dir string) *Application
LoadDir loads all YAML/TOML config files from a directory, keyed by filename. Like Load, a failure is recorded and returned by Serve/Err (fail fast).
oni.LoadDir("config/") // loads config/app.yaml as "app.*", config/database.yaml as "database.*"
func (*Application) Register ¶
func (a *Application) Register(providers ...ServiceProvider) *Application
Register adds one or more service providers. Register is called before Boot. If the application has already booted, late-registered providers are booted immediately so they are never left half-initialized.
func (*Application) Route ¶
func (a *Application) Route(fn func(*routing.Router)) *Application
Route registers routes on the application router using a callback.
oni.Route(func(r *routing.Router) {
r.Get("/", HomeHandler)
r.Group("/api/v1", func(r *routing.Group) { ... })
})
func (*Application) Serve ¶
func (a *Application) Serve() error
Serve starts the HTTP server. It blocks until shutdown.
func (*Application) ServeAddr ¶
func (a *Application) ServeAddr(addr string) error
ServeAddr starts the server on the given address (overrides config). Useful for testing.
func (*Application) SetLogger ¶
func (a *Application) SetLogger(l *slog.Logger) *Application
SetLogger replaces the application logger.
func (*Application) Use ¶
func (a *Application) Use(mw ...onihttp.MiddlewareFunc) *Application
Use appends global middleware to the application router.
type BindingFunc ¶
BindingFunc is a factory function that receives the container and returns a service.
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container is the IoC service container. It manages bindings, singletons, and instances.
func (*Container) Bind ¶
func (c *Container) Bind(abstract string, factory BindingFunc)
Bind registers a transient binding. A new instance is created each time Make is called. Re-binding an abstract evicts any previously cached singleton instance.
func (*Container) Flush ¶
func (c *Container) Flush()
Flush removes all bindings and instances — useful in tests.
func (*Container) MakeInto ¶
MakeInto resolves a service and stores the result into dest (must be a non-nil pointer).
func (*Container) MustMake ¶
MustMake resolves a service and panics on error. Useful for required dependencies.
func (*Container) Singleton ¶
func (c *Container) Singleton(abstract string, factory BindingFunc)
Singleton registers a singleton binding. The same instance is returned on every Make call. Re-binding an abstract evicts any previously cached singleton instance so the new factory takes effect.
type ServiceProvider ¶
type ServiceProvider interface {
// Register binds services into the container. No services should be resolved here
// because other providers may not have registered their services yet.
Register(app *Application)
// Boot is called after all providers have been registered. Safe to resolve services.
Boot(app *Application)
}
ServiceProvider is the interface all service providers must implement. Providers bootstrap framework features into the application container.