Skip to content
Provider & Plugin System

Provider & Plugin System

Overview

Providers are the plugin mechanism for Lemmego. They allow packages to register services, routes, middleware, commands, and error handlers with the application during bootstrapping.

The Provider Interface

Every provider implements this interface:

type Provider interface {
    Provide(a App) error
}

The Provide method receives the full App instance and can access configuration, register services, and set up middleware:

type MyProvider struct{}

func (p *MyProvider) Provide(a app.App) error {
    cfg := a.Config()
    svc := NewMyService(cfg)
    a.AddService(svc)
    return nil
}

Specialized Provider Interfaces

Providers can optionally implement more specific interfaces:

RouteProvider

Registers routes during bootstrapping:

type MyProvider struct{}

func (p *MyProvider) AddRoutes() app.RouteCallback {
    return func(a app.App) {
        r := a.Router()
        r.Get("/my-route", myHandler)
    }
}

CommandProvider

Adds CLI commands:

func (p *MyProvider) AddCommands() []app.Command {
    return []app.Command{
        func(a app.App) *cobra.Command {
            return &cobra.Command{
                Use:   "my:command",
                Short: "Does something",
                Run: func(cmd *cobra.Command, args []string) {
                    // implementation
                },
            }
        },
    }
}

MiddlewareProvider

Registers app-level middleware:

func (p *MyProvider) AddMiddlewares() []app.Handler {
    return []app.Handler{
        myCustomMiddleware,
    }
}

ErrMapProvider

Registers error-to-handler mappings:

func (p *MyProvider) AddErrMap() app.ErrMap {
    return app.ErrMap{
        app.ErrNotFound: notFoundHandler,
    }
}

PublishableProvider

Registers assets for publishing:

func (p *MyProvider) AddPublishables() []*app.Publishable {
    return []*app.Publishable{...}
}

ShutdownProvider

Executes cleanup on application shutdown:

func (p *MyProvider) Shutdown(ctx context.Context) error {
    return myDBConnection.Close()
}

Registration

Providers are registered in bootstrap/providers.go:

func LoadProviders() []app.Provider {
    return []app.Provider{
        &fs.Provider{},
        &session.Provider{},
        &inertia.Provider{},
        &gormconnector.Provider{UseGPA: true},
        &auth.Provider{Opts: &auth.Opts{...}},
        &MyProvider{},
    }
}

Built-in Providers

ProviderPackagePurpose
fs.Providergithub.com/lemmego/api/providers/fsFile system abstraction
session.Providergithub.com/lemmego/api/providers/sessionSession management
inertia.Providergithub.com/lemmego/inertiaInertia.js integration
gormconnector.Providergithub.com/lemmego/gormconnectorGORM database connection
bunconnector.Providergithub.com/lemmego/bunconnectorBun ORM database connection
auth.Providergithub.com/lemmego/authAuthentication