application

package
v0.1.0-alpha.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package application provides core application lifecycle management.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnknownCommand = errors.New("unknown command")

ErrUnknownCommand is returned when an unknown CLI command is provided.

Functions

This section is empty.

Types

type Application

type Application struct {
	// contains filtered or unexported fields
}

Application manages startup tasks and services for the application lifecycle.

func New

func New() *Application

New creates and returns a new Application instance.

func (*Application) Health

func (a *Application) Health(ctx context.Context) *Health

Health returns the current health status of the application.

func (*Application) OnStart

func (a *Application) OnStart(task Runner, config StartupTaskConfig)

OnStart registers a new startup task with the given runner and configuration.

func (*Application) OnStartFunc

func (a *Application) OnStartFunc(task RunnerFunc, config StartupTaskConfig)

OnStartFunc registers a startup task using a RunnerFunc.

func (*Application) RegisterDatabase

func (a *Application) RegisterDatabase(dbName string, db *database.Database)

RegisterDatabase adds a database to the application.

func (*Application) RegisterDomain

func (a *Application) RegisterDomain(name, dbName string, domain Domain)

RegisterDomain registers a domain repository in the specified database.

func (*Application) RegisterRepository

func (a *Application) RegisterRepository(dbName string, repoName string, repository any)

RegisterRepository adds a repository to the application.

func (*Application) RegisterService

func (a *Application) RegisterService(serviceName string, service Runner)

RegisterService adds a named service to the application.

func (*Application) Run

func (a *Application) Run(ctx context.Context) error

Run parses CLI arguments and executes the appropriate command. Supported commands: run (start services), migrate (run database migrations). Returns nil on success, ErrUnknownCommand for unknown commands.

type Domain

type Domain interface {
	GetRepository() any
}

Domain describes a domain module that exposes its repository.

type ErrDatabaseMigrationFailed

type ErrDatabaseMigrationFailed struct {
	// contains filtered or unexported fields
}

ErrDatabaseMigrationFailed is an error type that represents a failed database migration.

func (*ErrDatabaseMigrationFailed) Error

Error returns the formatted error message for ErrDatabaseMigrationFailed.

func (*ErrDatabaseMigrationFailed) Unwrap

func (e *ErrDatabaseMigrationFailed) Unwrap() error

Unwrap returns the underlying error for ErrDatabaseMigrationFailed.

type ErrStartupTaskFailed

type ErrStartupTaskFailed struct {
	// contains filtered or unexported fields
}

ErrStartupTaskFailed represents an error that occurs when a startup task fails.

func (*ErrStartupTaskFailed) Error

func (e *ErrStartupTaskFailed) Error() string

Error returns the formatted error message for ErrStartupTaskFailed.

func (*ErrStartupTaskFailed) Unwrap

func (e *ErrStartupTaskFailed) Unwrap() error

Unwrap returns the underlying error for ErrStartupTaskFailed.

type Health

type Health struct {
	StartedAt time.Time                 `json:"startedAt"`
	Services  map[string]*ServiceHealth `json:"services"`
}

Health contains overall application health and service states.

func NewHealth

func NewHealth() *Health

NewHealth creates an ApplicationHealth with initialized storage.

func (*Health) FailService

func (h *Health) FailService(serviceName string, err error)

FailService marks the given service as failed and stores the error.

func (*Health) SetServiceData

func (h *Health) SetServiceData(serviceName string, data any)

SetServiceData stores additional health payload for the given service.

func (*Health) StartApplication

func (h *Health) StartApplication()

StartApplication marks application start time.

func (*Health) StartService

func (h *Health) StartService(serviceName string)

StartService marks the given service as started and stores start time.

func (*Health) String

func (h *Health) String() string

type HealthCheckHandler

type HealthCheckHandler struct {
	// contains filtered or unexported fields
}

HealthCheckHandler serves application health information as JSON.

func NewHealthCheckHandler

func NewHealthCheckHandler(app healther) *HealthCheckHandler

NewHealthCheckHandler creates a HealthCheckHandler for the given application.

func (*HealthCheckHandler) ServeHTTP

func (h *HealthCheckHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Healthchecker

type Healthchecker interface {
	// Healthcheck returns the health status of the service.
	Healthcheck(context.Context) any
}

Healthchecker represents a service that can perform health checks.

type Runner

type Runner interface {
	// Run executes the task with the given context and returns an error if any.
	Run(context.Context) error
}

Runner is an interface that defines the Run method for executing a task with context.

type RunnerFunc

type RunnerFunc func(context.Context) error

RunnerFunc is a function type that implements the Runner interface.

func (RunnerFunc) Run

func (rf RunnerFunc) Run(ctx context.Context) error

Run executes the RunnerFunc with the given context and returns the result.

type ServiceHealth

type ServiceHealth struct {
	Status    ServiceStatus `json:"status"`
	StartedAt *time.Time    `json:"startedAt"`
	StoppedAt *time.Time    `json:"stoppedAt,omitempty"`
	Error     string        `json:"error,omitempty"`
	Data      any           `json:"data,omitempty"`
}

ServiceHealth contains health information for a single service.

type ServiceStatus

type ServiceStatus string

ServiceStatus represents the lifecycle state of a service.

const (
	// ServiceStatusNotStarted indicates service has not started yet.
	ServiceStatusNotStarted ServiceStatus = "NOT_STARTED"
	// ServiceStatusStarted indicates service is currently running.
	ServiceStatusStarted ServiceStatus = "STARTED"
	// ServiceStatusError indicates service finished with an error.
	ServiceStatusError ServiceStatus = "ERROR"
)

type StartupTaskConfig

type StartupTaskConfig struct {
	Name         string // Name of the startup task
	AbortOnError bool   // Whether to abort application startup if this task fails
}

StartupTaskConfig contains configuration options for a startup task.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL