Bootstrap Application
The Parsley bootstrap package provides a streamlined way to manage your application's lifecycle. It automates the process of initializing the service registry, configuring modules, and running the main application entry point.
The Application Interface
To use the bootstrap package, your application must implement the bootstrap.Application interface. This interface defines a single method:
type Application interface {
Run(ctx context.Context) error
}
The Run method serves as the entry point for your application logic, such as starting a web server or a background worker.
Running the Application
Use bootstrap.RunParsleyApplication to initialize and start your application. This function takes a context, an application factory function, and optional module configuration functions.
func RunParsleyApplication(ctx context.Context, appFactoryFunc any, configure ...types.ModuleFunc) error
Example
The following example demonstrates how to bootstrap a web application using the chi router.
First, define your application struct and its constructor:
package internal
import (
"context"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/matzefriedrich/parsley-docs/examples/integrations/chi/internal/route_handlers"
"github.com/matzefriedrich/parsley/pkg/bootstrap"
)
type parsleyApplication struct {
router chi.Router
}
var _ bootstrap.Application = &parsleyApplication{}
// NewApp Creates the main application service instance. This constructor function gets invoked by Parsley; add parameters for all required services.
func NewApp(router chi.Router, routeHandlers []route_handlers.RouteHandler) bootstrap.Application {
for _, routeHandler := range routeHandlers {
routeHandler.Register(router)
}
return &parsleyApplication{
router: router,
}
}
// Run The entrypoint for the Parsley application.
func (a *parsleyApplication) Run(_ context.Context) error {
return http.ListenAndServe(":5504", a.router)
}
Then, in your main function, use bootstrap.RunParsleyApplication to start the application:
package main
import (
"context"
"github.com/matzefriedrich/parsley-docs/examples/integrations/chi/internal"
"github.com/matzefriedrich/parsley-docs/examples/integrations/chi/internal/modules"
"github.com/matzefriedrich/parsley/pkg/bootstrap"
)
func main() {
ctx := context.Background()
err := bootstrap.RunParsleyApplication(ctx, internal.NewApp,
modules.ConfigureChi,
modules.ConfigureGreeter)
if err != nil {
panic(err)
}
}
How It Works
When you call bootstrap.RunParsleyApplication, the following steps are performed:
- Registry Initialization: A new service registry is created.
- Module Configuration: All provided
ModuleFuncfunctions are executed to register services and configurations. - Application Registration: The
appFactoryFuncis registered as a singleton service of typebootstrap.Application. - Resolution: The
bootstrap.Applicationinstance is resolved from the registry. - Execution: The
Runmethod of the resolved application instance is invoked with the provided context.