app
packageAPI reference for the app
package.
Imports
(9)context
STD
fmt
STD
log/slog
INT
github.com/mirkobrombin/go-foundation/pkg/di
INT
github.com/mirkobrombin/go-foundation/pkg/dispatcher
INT
github.com/mirkobrombin/go-foundation/pkg/hosting
INT
github.com/mirkobrombin/go-foundation/pkg/scheduler
INT
github.com/mirkobrombin/go-foundation/pkg/srv
STD
testing
Handler
Handler is the interface for declarative struct-tagged endpoints.
type Handler interface
Methods
App
App orchestrates DI, HTTP, dispatching, and scheduling into a single entrypoint.
type App struct
Methods
Log sets the application logger.
Parameters
Returns
func (*App) Log(logger *slog.Logger) *App
{
a.logger = logger
return a
}
Provide registers a named dependency for injection into handler structs.
Parameters
Returns
func (*App) Provide(name string, instance any) *App
{
a.builder.Provide(name, instance)
return a
}
RegisterHTTP registers a struct-tagged HTTP handler.
func (*App) RegisterHTTP(h Handler) *App
{
a.handlerReg = append(a.handlerReg, h)
return a
}
RegisterAction registers a named action handler for dispatch.
Parameters
Returns
func (*App) RegisterAction(name string, handler func(ctx context.Context, payload ...any) (any, error)) *App
{
a.dispatch.Register(name, handler)
return a
}
Dispatch calls a named action handler.
Parameters
Returns
func (*App) Dispatch(ctx context.Context, name string, payload ...any) (any, error)
{
return a.dispatch.Dispatch(ctx, name, payload...)
}
Schedule registers a cron job.
Parameters
Returns
func (*App) Schedule(name, cronExpr string, handler func(ctx context.Context) error) *App
{
a.sched.Register(scheduler.Job{Name: name, Cron: cronExpr, Handler: handler})
return a
}
Use adds middleware to the HTTP server.
Parameters
Returns
func (*App) Use(mw srv.Middleware) *App
{
a.server.Use(mw)
return a
}
Configure allows direct customization of the underlying srv.Server.
Parameters
Returns
func (*App) Configure(fn func(*srv.Server)) *App
{
fn(a.server)
return a
}
Build constructs the DI container and registers all handlers.
Returns
func (*App) Build() (*di.Container, error)
{
container, err := a.builder.Build()
if err != nil {
return nil, err
}
a.container = container
for _, h := range a.handlerReg {
a.server.RegisterHandler(h, container)
}
return container, nil
}
Listen starts the HTTP server and scheduler, then blocks until shutdown.
Parameters
Returns
func (*App) Listen(addr string) error
{
if a.container == nil {
if _, err := a.Build(); err != nil {
return fmt.Errorf("app: build failed: %w", err)
}
}
if addr == "" {
addr = ":8080"
}
h := hosting.NewBuilder().
WithAddr(addr).
AddHostedService(&schedulerHost{sched: a.sched}).
Build()
h.Server = a.server
return h.Run(context.Background())
}
Fields
| Name | Type | Description |
|---|---|---|
| container | *di.Container | |
| server | *srv.Server | |
| dispatch | *dispatcher.Dispatcher | |
| sched | *scheduler.Scheduler | |
| builder | *di.Builder | |
| logger | *slog.Logger | |
| handlerReg | []Handler |
New
New creates a new App with default components.
Returns
func New() *App
{
return &App{
builder: di.NewBuilder(),
server: srv.New(),
dispatch: dispatcher.New(),
sched: scheduler.New(),
logger: slog.Default(),
}
}
schedulerHost
type schedulerHost struct
Methods
Parameters
Returns
func (*schedulerHost) Start(ctx context.Context) error
{
go s.sched.Start(ctx)
return nil
}
Parameters
Returns
func (*schedulerHost) Stop(ctx context.Context) error
{
return s.sched.Stop(ctx)
}
Fields
| Name | Type | Description |
|---|---|---|
| sched | *scheduler.Scheduler |
greetEndpoint
type greetEndpoint struct
Methods
Parameters
Returns
func (*greetEndpoint) Handle(_ context.Context) (any, error)
{
return greetResponse{Message: "hello " + e.Name}, nil
}
Fields
| Name | Type | Description |
|---|---|---|
| Meta | struct{} | method:"GET" path:"/greet" |
| Name | string | query:"name" default:"world" |
greetResponse
type greetResponse struct
Fields
| Name | Type | Description |
|---|---|---|
| Message | string | json:"message" |
TestApp_New
Parameters
func TestApp_New(t *testing.T)
{
a := New()
if a == nil {
t.Fatal("New() returned nil")
}
}
TestApp_Provide
Parameters
func TestApp_Provide(t *testing.T)
{
a := New()
a.Provide("db", "fake-connection")
}
TestApp_RegisterHTTP
Parameters
func TestApp_RegisterHTTP(t *testing.T)
{
a := New()
a.RegisterHTTP(&greetEndpoint{})
}
TestApp_RegisterAction
Parameters
func TestApp_RegisterAction(t *testing.T)
{
a := New()
a.RegisterAction("test", func(ctx context.Context, payload ...any) (any, error) {
return "ok", nil
})
}
TestApp_Dispatch
Parameters
func TestApp_Dispatch(t *testing.T)
{
a := New()
a.RegisterAction("ping", func(ctx context.Context, payload ...any) (any, error) {
return "pong", nil
})
result, err := a.Dispatch(context.Background(), "ping")
if err != nil {
t.Fatalf("Dispatch: %v", err)
}
if result != "pong" {
t.Errorf("result = %v, want pong", result)
}
}
TestApp_Schedule
Parameters
func TestApp_Schedule(t *testing.T)
{
a := New()
a.Schedule("cleanup", "0 0 * * *", func(ctx context.Context) error {
return nil
})
}