readysite / pkg / application / options.go
1.4 KB
options.go
package application

import "io/fs"

// Option configures the application
type Option func(*App)

// WithEmailer sets the emailer implementation
func WithEmailer(emailer Emailer) Option {
	return func(app *App) {
		app.Emailer = emailer
	}
}

// WithFunc registers a template function
func WithFunc(name string, fn any) Option {
	return func(app *App) {
		app.funcs[name] = fn
	}
}

// WithValue registers a template function that returns the given value
func WithValue(name string, value any) Option {
	return func(app *App) {
		app.funcs[name] = func() any { return value }
	}
}

// WithController registers a controller with the application.
// Takes (name, controller) from factory function like controllers.Home().
func WithController(name string, controller Controller) Option {
	return func(app *App) {
		app.controllers[name] = controller
		if setupper, ok := controller.(interface{ Setup(*App) }); ok {
			setupper.Setup(app)
		}
	}
}

// WithMiddleware adds a middleware to the handler chain.
// Middlewares are applied in order, with the last-added wrapping outermost.
func WithMiddleware(mw Middleware) Option {
	return func(app *App) {
		app.middlewares = append(app.middlewares, mw)
	}
}

// WithViews sets the views filesystem for the application.
// This is primarily used for testing; production code should use Serve().
func WithViews(views fs.FS) Option {
	return func(app *App) {
		app.viewsFS = views
		app.base = app.parseBaseTemplates(views)
	}
}
← Back