Help us shape the future of Go! Take the 2026 Go Developer Survey

app

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Main

func Main(cfg Config)

Main runs the command named by the program's arguments and exits.

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

func New(cfg Config, db *sql.DB) *App

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) Command

func (a *App) Command(cmd Command)

Command registers an operator command.

func (*App) DB

func (a *App) DB() *sql.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

func (a *App) Every(name string, interval time.Duration, run func(context.Context) error)

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) Log

func (a *App) Log() *slog.Logger

Log returns the app's logger.

func (*App) Name

func (a *App) Name() string

Name returns the app's name.

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.

func (*App) Serve

func (a *App) Serve(s web.Server)

Serve sets the server the serve command runs.

func (*App) SetLogger

func (a *App) SetLogger(l *slog.Logger)

SetLogger replaces the logger, which by default writes JSON to standard error.

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

type Migrations struct {
	Namespace string
	Files     fs.FS
}

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.

Directories

Path Synopsis
Package apptest gives integration tests an App on a fresh, disposable database.
Package apptest gives integration tests an App on a fresh, disposable database.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL