Skip to main content
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
}
ID()
string
Returns the unique application instance ID
Name()
string
Returns the service name
Version()
string
Returns the application version
Metadata()
map[string]string
Returns the service metadata key-value pairs
Endpoint()
[]string
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
opts
...Option
Variable number of configuration options
App
*App
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
ctx
context.Context
The parent context
s
AppInfo
The AppInfo to attach to the context
context
context.Context
Context with AppInfo attached

FromContext

Returns the AppInfo value stored in context.
func FromContext(ctx context.Context) (s AppInfo, ok bool)
ctx
context.Context
Context to retrieve AppInfo from
s
AppInfo
The retrieved AppInfo instance
ok
bool
Whether the AppInfo was found in the context

Methods

Run

Executes all OnStart hooks and starts the application.
func (a *App) Run() error
error
error
Returns an error if startup fails
The Run method:
  1. Builds the service instance
  2. Executes BeforeStart hooks
  3. Starts all registered servers
  4. Registers the service with the registry (if configured)
  5. Executes AfterStart hooks
  6. Waits for termination signals
  7. 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
error
error
Returns an error if shutdown fails
The Stop method:
  1. Executes BeforeStop hooks
  2. Deregisters from service registry
  3. Cancels the application context
  4. Stops all servers with timeout
  5. Executes AfterStop hooks

ID

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

Metadata

Returns the service metadata.
func (a *App) Metadata() map[string]string

Endpoint

Returns the service endpoints.
func (a *App) Endpoint() []string

Options

ID

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

Metadata

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

Build docs developers (and LLMs) love