app

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package app provides the IoC service container and application bootstrap.

Index

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

Use appends global middleware to the application router.

type BindingFunc

type BindingFunc func(c *Container) (any, error)

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 NewContainer

func NewContainer() *Container

NewContainer creates a new empty Container.

func (*Container) Alias

func (c *Container) Alias(abstract, alias string)

Alias registers an alias for an abstract binding.

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) Bound

func (c *Container) Bound(abstract string) bool

Bound reports whether an abstract has a binding (or pre-set instance).

func (*Container) Flush

func (c *Container) Flush()

Flush removes all bindings and instances — useful in tests.

func (*Container) Instance

func (c *Container) Instance(abstract string, instance any)

Instance registers an already-constructed value as a shared instance.

func (*Container) Make

func (c *Container) Make(abstract string) (any, error)

Make resolves a service from the container by its abstract name.

func (*Container) MakeInto

func (c *Container) MakeInto(abstract string, dest any) error

MakeInto resolves a service and stores the result into dest (must be a non-nil pointer).

func (*Container) MustMake

func (c *Container) MustMake(abstract string) any

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.

func (*Container) Tag

func (c *Container) Tag(abstracts []string, tag string)

Tag assigns a set of abstracts to a named tag group.

func (*Container) Tagged

func (c *Container) Tagged(tag string) ([]any, error)

Tagged returns all services registered under the given tag.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL