Skip to main content
The Application type manages the complete lifecycle of your application, from startup tasks through graceful shutdown.

Application Type

Defined in application/application.go:36, the Application coordinates:
  • Startup tasks - Run sequentially before services start
  • Services - Long-running processes (HTTP servers, queue processors)
  • Databases - Database connections with migration support
  • Health checks - Service health monitoring

Basic Setup

Create and run an application:
app.Run(ctx) automatically parses command-line arguments. Run with run command to start services or migrate to run database migrations.

Registration Methods

RegisterDatabase

Register a database connection defined in application/application.go:68:
Databases are automatically migrated during the migrate command.

RegisterDomain

Register a domain and its repository from application/application.go:90:
This automatically registers the domain’s repository with the specified database for migration tracking.

RegisterService

Register a long-running service from application/application.go:78:
Services must implement the Runner interface:
Services implementing Healthchecker are automatically registered for health monitoring.

RegisterRepository

Directly register a repository from application/application.go:73:
Usually not needed - RegisterDomain() handles this automatically.

Startup Tasks

Run initialization logic before services start using application/application.go:58:

Startup Task Configuration

From application/startuptask.go:21:
Startup tasks run sequentially in registration order. If AbortOnError is true, a failure prevents service startup.

Complete Lifecycle Example

From demo-app/cmd/api/main.go, showing the full application setup:

CLI Commands

The Run method from application/application.go:180 supports these commands:
Starts all registered services:
  • Executes startup tasks sequentially
  • Starts all services concurrently
  • Waits for interrupt signal (Ctrl+C)
  • Performs graceful shutdown

Service Lifecycle

When you run ./myapp run, the application:
  1. Runs startup tasks (application/application.go:129) - Sequential execution
  2. Starts services (application/application.go:146) - Concurrent goroutines with panic recovery
  3. Updates health status (application/application.go:160) - Tracks service state
  4. Waits for shutdown (application/application.go:124) - Listens for OS signals
  5. Graceful shutdown - Services receive context cancellation
Services receive a context that’s cancelled on shutdown. Implement graceful shutdown by monitoring ctx.Done().

Graceful Shutdown

The application handles SIGINT and SIGKILL signals (application/application.go:124):
Services should respect context cancellation:
The HTTPServer automatically implements graceful shutdown with a configurable timeout.

Error Handling

Specialized errors from the application package:

Health Monitoring

Access health status via application/application.go:50:
Services implementing Healthchecker provide custom health data:

Next Steps

Domains

Learn how to create and structure domains

HTTP Routing

Set up HTTP servers and routes

Database

Configure databases and migrations