Application type manages the complete lifecycle of your application, from startup tasks through graceful shutdown.
Application Type
Defined inapplication/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 inapplication/application.go:68:
migrate command.
RegisterDomain
Register a domain and its repository fromapplication/application.go:90:
RegisterService
Register a long-running service fromapplication/application.go:78:
Runner interface:
RegisterRepository
Directly register a repository fromapplication/application.go:73:
RegisterDomain() handles this automatically.
Startup Tasks
Run initialization logic before services start usingapplication/application.go:58:
Startup Task Configuration
Fromapplication/startuptask.go:21:
Complete Lifecycle Example
Fromdemo-app/cmd/api/main.go, showing the full application setup:
CLI Commands
TheRun method from application/application.go:180 supports these commands:
- run
- migrate
- --help
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:
- Runs startup tasks (application/application.go:129) - Sequential execution
- Starts services (application/application.go:146) - Concurrent goroutines with panic recovery
- Updates health status (application/application.go:160) - Tracks service state
- Waits for shutdown (application/application.go:124) - Listens for OS signals
- 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 handlesSIGINT and SIGKILL signals (application/application.go:124):
Error Handling
Specialized errors from the application package:Health Monitoring
Access health status viaapplication/application.go:50:
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