Documentation
¶
Overview ¶
Package togo is the microkernel of the togo framework. The kernel is deliberately thin: configuration, a hook/event bus, a plugin loader+registry, a database pool, and server bootstrap. Every capability — REST, GraphQL, auth, dashboard, resources — ships as a Plugin installed by the CLI and discovered here.
Index ¶
- Constants
- func Providers() []string
- func Register(p Plugin)
- func RegisterDialect(driver string, d Dialect)
- func RegisterProvider(p Provider)
- func RegisterProviderFunc(name string, priority int, fn func(*Kernel) error)
- type Broker
- type Cache
- type Config
- type Dialect
- type HookFunc
- type Hooks
- type Kernel
- func (k *Kernel) Boot(ctx context.Context) error
- func (k *Kernel) Close()
- func (k *Kernel) Dialect() Dialect
- func (k *Kernel) Get(key string) (any, bool)
- func (k *Kernel) Handler() http.Handler
- func (k *Kernel) Plugins() []Plugin
- func (k *Kernel) ReportError(ctx context.Context, err error)
- func (k *Kernel) SQL(ctx context.Context) (*sql.DB, error)
- func (k *Kernel) Serve(ctx context.Context) error
- func (k *Kernel) Set(key string, v any)
- func (k *Kernel) T(key string) string
- func (k *Kernel) Use(p Plugin) *Kernel
- func (k *Kernel) UseMiddleware(m ...func(http.Handler) http.Handler)
- type Plugin
- type Provider
- type Queue
- type QueueHandler
- type Storage
- type Translator
Constants ¶
const ( PriorityCore = 0 // log PriorityService = 50 // cache, storage, realtime, i18n PriorityLate = 90 // queue (depends on log) )
Priority bands (providers pick their own; built-in feature packages use these).
Variables ¶
This section is empty.
Functions ¶
func Providers ¶ added in v0.7.1
func Providers() []string
Providers returns the names of registered providers in run order.
func Register ¶
func Register(p Plugin)
Register adds a plugin to the global auto-discovery registry.
func RegisterDialect ¶ added in v0.21.0
RegisterDialect registers the SQL dialect for a database/sql driver name. DB driver plugins call this from init() for drivers the kernel doesn't ship.
func RegisterProvider ¶ added in v0.7.0
func RegisterProvider(p Provider)
RegisterProvider registers a service provider globally (call from init()). Higher Priority runs later and wins.
Types ¶
type Broker ¶ added in v0.11.0
type Broker interface {
Publish(event, data string)
Handler() http.HandlerFunc
}
Broker fans out server-push (realtime) messages to connected clients.
type Cache ¶ added in v0.11.0
type Cache interface {
Get(key string) (any, bool)
Set(key string, value any, ttl time.Duration)
Delete(key string)
}
Cache is the key/value cache contract.
type Config ¶
type Config struct {
Addr string
DBDriver string // database/sql driver name (default "sqlite"); providers register others
DatabaseURL string
GraphQLPath string
RESTPath string
DocsPath string
Locale string
LocaleDir string
StorageDir string
}
Config holds runtime configuration resolved from the environment, so connections/URLs/endpoints stay dynamic (togo convention: .env + hooks).
func LoadConfig ¶
func LoadConfig() *Config
LoadConfig reads configuration from environment variables with sane defaults. SQLite is the default driver — no external database needed to get started.
type Dialect ¶ added in v0.16.0
type Dialect struct {
Placeholder func(n int) string // 1-based positional placeholder
ILike string // case-insensitive LIKE operator
}
Dialect captures the per-driver SQL differences the ORM needs. It lives in the kernel (not the orm plugin) so the kernel can expose Kernel.Dialect() without depending on the orm package — the orm plugin consumes togo.Dialect.
func DialectFor ¶ added in v0.16.0
DialectFor returns the dialect registered for a database/sql driver name, falling back to the SQLite dialect for unknown drivers.
type Hooks ¶
type Hooks struct {
// contains filtered or unexported fields
}
Hooks is a priority-ordered event bus for lifecycle events (resource CRUD, auth, plugin load). Listeners run in ascending priority (0 first).
type Kernel ¶
type Kernel struct {
Config *Config
Router chi.Router
Hooks *Hooks
Log *slog.Logger
Cache Cache
Queue Queue
Storage Storage
Realtime Broker
I18n Translator
// contains filtered or unexported fields
}
Kernel is the shared runtime handed to every plugin and used by the app's entrypoint to mount REST/GraphQL and serve.
func New ¶
func New() *Kernel
New constructs a kernel: loads config, logger, router (with recovery + request-logging middleware) and hook bus, and seeds the plugin list from auto-discovery (blank-imported plugin packages).
func (*Kernel) Boot ¶
Boot runs Register then Boot for every plugin in priority order. Safe to call once; subsequent calls are no-ops.
func (*Kernel) Dialect ¶ added in v0.4.0
Dialect returns the ORM dialect for the configured driver.
func (*Kernel) Handler ¶ added in v0.21.0
Handler returns the kernel's HTTP handler: the router wrapped by every middleware registered via UseMiddleware (first-registered is outermost).
func (*Kernel) ReportError ¶ added in v0.2.0
ReportError logs an error and fires the "error" hook so trackers (Sentry, GlitchTip, …) shipped as plugins can capture it. This is togo's central error reporting path — call it from anywhere with an error worth surfacing.
func (*Kernel) SQL ¶ added in v0.3.0
SQL returns a lazily-opened database/sql handle using Config.DBDriver (default "sqlite"). Driver registration is the app's responsibility (blank-import the driver, or a DB provider plugin) — SQLite is core; Postgres/MySQL/etc. are provider plugins that register their driver and set DB_DRIVER.
func (*Kernel) Set ¶ added in v0.10.0
Set stores an arbitrary service in the kernel container, so any plugin can inject capabilities the core doesn't know about (e.g. auth).
func (*Kernel) T ¶ added in v0.7.0
T translates a key in the configured locale (trans() equivalent).
func (*Kernel) UseMiddleware ¶ added in v0.21.0
UseMiddleware registers global HTTP middleware that wraps the whole router at serve time (outermost first-registered). Unlike chi's Router.Use — which a plugin can only call before ANY route is mounted — this can be called by any number of plugins at any provider priority: the middleware is applied as an outer wrapper in Handler(), after all providers have run. Prefer this over k.Router.Use for cross-cutting middleware (CORS, auth context, tracing).
type Plugin ¶
type Plugin interface {
// Name uniquely identifies the plugin (e.g. "rest-huma", "auth-supabase").
Name() string
// Priority controls boot order; lower boots first. Infrastructure plugins
// (config, db) use low values; feature plugins use higher ones.
Priority() int
// Register binds services, config, and hooks. No I/O or route mounting here.
Register(k *Kernel) error
// Boot starts the plugin: mount routes, register schema, run migrations.
Boot(ctx context.Context, k *Kernel) error
}
Plugin is the contract every togo capability implements. The runtime boots plugins in ascending Priority order (0–100), mirroring laravilt's ordered service-provider lifecycle.
func Discovered ¶
func Discovered() []Plugin
Discovered returns a copy of the auto-registered plugins.
type Provider ¶ added in v0.7.0
Provider contributes a service to the kernel. The kernel core is tiny (config, router, hooks, log, plugin lifecycle). Every OTHER capability — cache, queue, storage, realtime, i18n, db drivers — lives in its own provider package under togo/providers/* and self-registers in init(). A project opts into features by blank-importing those packages (chosen at `togo new`), so the core hard-codes nothing and any plugin can inject/override a provider globally.
type Queue ¶ added in v0.11.0
type Queue interface {
Handle(name string, h QueueHandler)
Dispatch(ctx context.Context, name string, payload any) error
}
Queue dispatches named jobs to registered handlers.
type QueueHandler ¶ added in v0.11.0
QueueHandler processes a dispatched job payload.
type Storage ¶ added in v0.11.0
type Storage interface {
Put(path string, data []byte) error
Get(path string) ([]byte, error)
Delete(path string) error
Path(path string) string
}
Storage is the blob storage contract.
type Translator ¶ added in v0.11.0
Translator resolves a key for a locale (trans()).