Core API Reference
Complete API reference for the Core framework (pkg/core).
Core Struct
The central application container.
Creation
Creates a new Core instance with the specified options.
Methods
Service Access
Retrieves a service by name with type safety.
Retrieves a service by name, panics if not found or wrong type.
Actions
Broadcasts a message to all registered action handlers.
Registers an action handler.
Service Registration
Manually adds a service to the registry.
Config Access
Returns the config service if registered.
Options
WithService
Registers a service using its factory function.
WithName
Registers a service with an explicit name.
WithAssets
Sets embedded assets for the application.
WithServiceLock
Prevents late service registration after initialization.
ServiceFactory
Factory function signature for service creation.
Message
Messages can be any type. Common patterns:
// Map-based message
c.ACTION(map[string]any{
"action": "user.created",
"id": "123",
})
// Typed message
type UserCreated struct {
ID string
Email string
}
c.ACTION(UserCreated{ID: "123", Email: "user@example.com"})
ServiceRuntime
Generic helper for services that need Core access.
Creation
Methods
func (r *ServiceRuntime[T]) Core() *Core
func (r *ServiceRuntime[T]) Options() T
func (r *ServiceRuntime[T]) Config() *config.Service
Usage
type MyOptions struct {
Timeout time.Duration
}
type MyService struct {
*core.ServiceRuntime[MyOptions]
}
func NewMyService(c *core.Core) (any, error) {
opts := MyOptions{Timeout: 30 * time.Second}
return &MyService{
ServiceRuntime: core.NewServiceRuntime(c, opts),
}, nil
}
func (s *MyService) DoSomething() {
timeout := s.Options().Timeout
cfg := s.Config()
// ...
}
Lifecycle Interfaces
Startable
Implement for initialization on app start.
Stoppable
Implement for cleanup on app shutdown.
IPC Handler
Automatically registered when using WithService.
Built-in Actions
ActionServiceStartup
Sent to all services when application starts.
ActionServiceShutdown
Sent to all services when application shuts down.
Error Helpers
Creates a contextual error with service and operation info.
if err != nil {
return core.E("myservice", "Connect", err)
}
// Error: myservice.Connect: connection refused
Complete Example
package main
import (
"context"
"github.com/Snider/Core/pkg/core"
"github.com/Snider/Core/pkg/config"
)
type MyService struct {
*core.ServiceRuntime[struct{}]
data string
}
func NewMyService(c *core.Core) (any, error) {
return &MyService{
ServiceRuntime: core.NewServiceRuntime(c, struct{}{}),
data: "initialized",
}, nil
}
func (s *MyService) OnStartup(ctx context.Context) error {
// Startup logic
return nil
}
func (s *MyService) OnShutdown(ctx context.Context) error {
// Cleanup logic
return nil
}
func (s *MyService) HandleIPCEvents(c *core.Core, msg core.Message) error {
switch m := msg.(type) {
case map[string]any:
if m["action"] == "myservice.update" {
s.data = m["data"].(string)
}
}
return nil
}
func main() {
c, err := core.New(
core.WithService(config.Register),
core.WithService(NewMyService),
core.WithServiceLock(),
)
if err != nil {
panic(err)
}
svc := core.MustServiceFor[*MyService](c, "main")
_ = svc
}