Bootstrap Application

Manage your application's lifecycle and dependency injection using the Parsley bootstrap package.

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:

  1. Registry Initialization: A new service registry is created.
  2. Module Configuration: All provided ModuleFunc functions are executed to register services and configurations.
  3. Application Registration: The appFactoryFunc is registered as a singleton service of type bootstrap.Application.
  4. Resolution: The bootstrap.Application instance is resolved from the registry.
  5. Execution: The Run method of the resolved application instance is invoked with the provided context.