application package provides the core Application type that manages startup tasks, services, databases, and health checks throughout your application’s lifecycle.
Application Type
TheApplication type orchestrates the lifecycle of your application, managing startup tasks, services, databases, and health monitoring.
Constructor
*Application
Creates and returns a new Application instance.
Methods
RegisterService
void
Registers a named service with the application. Services are started concurrently when the application runs.
RegisterDatabase
void
Adds a database to the application for lifecycle management.
RegisterRepository
void
Registers a repository with a specific database. Repositories implementing the migrator interface will have their migrations applied.
RegisterDomain
void
Registers a domain repository in the specified database. The domain must implement the Domain interface.
OnStart
void
Registers a startup task that runs before services start.
OnStartFunc
void
Registers a startup task using a function.
Health
*Health
Returns the current health status of the application and all registered services.
Run
error
Parses CLI arguments and executes the appropriate command.Supported commands:
run- Starts all registered servicesmigrate- Runs database migrations--help,-h- Displays usage information
ErrUnknownCommand if an unknown command is provided.Interfaces
Runner
The Runner interface defines tasks that can be executed with context.func(context.Context) error
Executes the task with the given context and returns an error if any.
RunnerFunc
A function type that implements the Runner interface.Domain
The Domain interface describes a domain module that exposes its repository.func() any
Returns the repository associated with this domain.
Healthchecker
Represents a service that can perform health checks.func(context.Context) any
Returns the health status of the service. The returned value can be any serializable type.
Types
StartupTaskConfig
Configuration options for a startup task.string
Name of the startup task for logging and identification.
bool
If true, the application will terminate if this startup task fails. If false, the error is logged and execution continues.
Errors
ErrUnknownCommand
Run().
ErrDatabaseMigrationFailed
migrate command.
ErrStartupTaskFailed
AbortOnError: true fails.
Helpers
HealthCheckHandler
An HTTP handler that serves application health information as JSON.*HealthCheckHandler
Creates a health check HTTP handler for the application.Parameters:
app(*Application): The application instance to check health for
- Application start time
- Service statuses (NOT_STARTED, STARTED, ERROR)
- Service start/stop times
- Error messages for failed services
- Custom health data from Healthchecker implementations