The App API provides the core application lifecycle management for Kratos. It handles server startup, graceful shutdown, service registration, and lifecycle hooks.
Types
AppInfo
Interface for accessing application context information.
type AppInfo interface {
ID() string
Name() string
Version() string
Metadata() map[string]string
Endpoint() []string
}
Returns the unique application instance ID
Returns the application version
Returns the service metadata key-value pairs
Returns the list of service endpoints
App
Application components lifecycle manager.
type App struct {
// contains filtered or unexported fields
}
Functions
New
Creates a new application lifecycle manager.
func New(opts ...Option) *App
Variable number of configuration options
Returns a configured App instance
Example:
app := kratos.New(
kratos.ID(id),
kratos.Name("myapp"),
kratos.Version("v1.0.0"),
kratos.Server(httpSrv, grpcSrv),
)
NewContext
Returns a new Context that carries the AppInfo value.
func NewContext(ctx context.Context, s AppInfo) context.Context
The AppInfo to attach to the context
Context with AppInfo attached
FromContext
Returns the AppInfo value stored in context.
func FromContext(ctx context.Context) (s AppInfo, ok bool)
Context to retrieve AppInfo from
The retrieved AppInfo instance
Whether the AppInfo was found in the context
Methods
Run
Executes all OnStart hooks and starts the application.
func (a *App) Run() error
Returns an error if startup fails
The Run method:
- Builds the service instance
- Executes BeforeStart hooks
- Starts all registered servers
- Registers the service with the registry (if configured)
- Executes AfterStart hooks
- Waits for termination signals
- Gracefully shuts down on signal reception
Example:
if err := app.Run(); err != nil {
log.Fatal(err)
}
Stop
Gracefully stops the application.
func (a *App) Stop() error
Returns an error if shutdown fails
The Stop method:
- Executes BeforeStop hooks
- Deregisters from service registry
- Cancels the application context
- Stops all servers with timeout
- Executes AfterStop hooks
Returns the application instance ID.
func (a *App) ID() string
Name
Returns the service name.
func (a *App) Name() string
Version
Returns the application version.
func (a *App) Version() string
Returns the service metadata.
func (a *App) Metadata() map[string]string
Endpoint
Returns the service endpoints.
func (a *App) Endpoint() []string
Options
Sets the service ID.
func ID(id string) Option
Name
Sets the service name.
func Name(name string) Option
Version
Sets the service version.
func Version(version string) Option
Sets the service metadata.
func Metadata(md map[string]string) Option
Server
Registers transport servers.
func Server(srv ...transport.Server) Option
Registrar
Sets the service registry.
func Registrar(r registry.Registrar) Option
RegistrarTimeout
Sets the registrar timeout duration.
func RegistrarTimeout(t time.Duration) Option
Default: 10 seconds
StopTimeout
Sets the application stop timeout.
func StopTimeout(t time.Duration) Option
Context
Sets the application context.
func Context(ctx context.Context) Option
Logger
Sets the application logger.
func Logger(logger log.Logger) Option
Signal
Sets the exit signals to listen for.
func Signal(sigs ...os.Signal) Option
Default: SIGTERM, SIGQUIT, SIGINT
BeforeStart
Registers a function to run before app starts.
func BeforeStart(fn func(context.Context) error) Option
AfterStart
Registers a function to run after app starts.
func AfterStart(fn func(context.Context) error) Option
BeforeStop
Registers a function to run before app stops.
func BeforeStop(fn func(context.Context) error) Option
AfterStop
Registers a function to run after app stops.
func AfterStop(fn func(context.Context) error) Option