Documentation
¶
Overview ¶
Package app is the entry point of a C4Stack application. Main runs the app's commands: serve, migrate and every operator command the app or its modules register.
The migrate command needs only Config.DatabaseURL and Config.Migrations. It never runs Setup, so a migration job needs nothing but database credentials. Every other command runs Setup first.
Index ¶
- func Main(cfg Config)
- type App
- func (a *App) Command(cmd Command)
- func (a *App) DB() *sql.DB
- func (a *App) Every(name string, interval time.Duration, run func(context.Context) error)
- func (a *App) Log() *slog.Logger
- func (a *App) Name() string
- func (a *App) Require(m Migrations) error
- func (a *App) Serve(s web.Server)
- func (a *App) SetLogger(l *slog.Logger)
- type Command
- type Config
- type Migrations
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App is what Setup wires together: the database, the logger, commands, background jobs and the server.
func New ¶
New returns an App on an open database without running Setup. Main uses it, and so do tests and tools that wire modules themselves.
func (*App) DB ¶
DB returns the database pool. It is nil while help lists commands, so modules must not query the database while they are constructed.
func (*App) Every ¶
Every runs job every interval while the app serves. The first run is a minute after startup, or after interval if that is shorter. A job that may not run twice at once across replicas must take its own lock.
func (*App) Require ¶
func (a *App) Require(m Migrations) error
Require returns an error unless the app's Config lists m. Module constructors call it, so enabling a module without its migrations fails at startup.
type Command ¶
type Command struct {
// Name is one or more words, such as "credits adjust".
Name string
Usage string // arguments, such as "<account-id> <cents>"
Help string
Run func(ctx context.Context, args []string) error
}
Command is an operator command run as `<app> <name> [args]`.
type Config ¶
type Config struct {
// Name is the app's name, used in help output and logs.
Name string
// DatabaseURL is the PostgreSQL connection URL. It is the only setting the migrate
// command reads.
DatabaseURL string
// Migrations are applied in this order by migrate, and checked by readiness. List
// module migrations first, then the app's own.
Migrations []Migrations
// Setup wires the app for every command except migrate: it loads the rest of the
// configuration, constructs providers and modules, and calls Serve.
Setup func(ctx context.Context, a *App) error
}
Config describes an application to Main.
type Migrations ¶
Migrations are one namespace's migration files: NNN_name.sql at the root of Files. Published files must never change; add a new file instead.