Dependency injection and service lifecycle framework for Go. Zero external dependencies beyond testify for tests.
import "dappco.re/go/core"c := core.New(core.Options{
{Key: "name", Value: "myapp"},
})
// Register a service
c.Service("auth", core.Service{
OnStart: func() core.Result { return core.Result{OK: true} },
OnStop: func() core.Result { return core.Result{OK: true} },
})
// Retrieve it
r := c.Service("auth")
if r.OK { /* use r.Value */ }
// Register and run commands
c.Command("deploy", handler)
c.Cli().Run()Key-value pairs that flow through all subsystems:
opts := core.Options{
{Key: "name", Value: "brain"},
{Key: "port", Value: 8080},
}
name := opts.String("name")
port := opts.Int("port")
ok := opts.Has("debug")Universal return type replacing (value, error):
r := c.Data().New(core.Options{{Key: "name", Value: "store"}})
if r.OK { use(r.Value) }
// Map from Go conventions
r.Result(file, err) // OK = err == nil, Value = fileManaged component with optional lifecycle hooks:
core.Service{
Name: "cache",
Options: opts,
OnStart: func() core.Result { /* ... */ },
OnStop: func() core.Result { /* ... */ },
OnReload: func() core.Result { /* ... */ },
}| Accessor | Purpose |
|---|---|
c.Options() |
Input configuration |
c.App() |
Application identity |
c.Data() |
Embedded/stored content |
c.Drive() |
Resource handle registry |
c.Fs() |
Local filesystem I/O |
c.Config() |
Configuration + feature flags |
c.Cli() |
CLI surface layer |
c.Command("path") |
Command tree |
c.Service("name") |
Service registry |
c.Lock("name") |
Named mutexes |
c.IPC() |
Message bus |
Fire-and-forget actions, request/response queries, and task dispatch:
// Register a handler
c.IPC().On(func(c *core.Core, msg core.Message) core.Result {
// handle message
return core.Result{OK: true}
})
// Dispatch
c.IPC().Action(core.Message{Action: "cache.flush"})go get dappco.re/go/core@latest