forge

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConnectDB

func ConnectDB(cfg Config) (*pgxpool.Pool, error)

ConnectDB creates a pgxpool.Pool from the given Config. Call this early in main() so the pool is available for registry wiring.

func Transaction

func Transaction(ctx context.Context, pool *pgxpool.Pool, fn TransactionFunc) error

Transaction wraps pgx.BeginFunc to provide a clean transaction API. The transaction commits if fn returns nil, and rolls back if fn returns an error.

func TransactionWithJobs

func TransactionWithJobs(ctx context.Context, pool *pgxpool.Pool, client *river.Client[pgx.Tx], fn func(ctx context.Context, tx pgx.Tx, jobs *river.Client[pgx.Tx]) error) error

TransactionWithJobs wraps a transaction and provides a River client for transactional job enqueueing. Jobs enqueued with client.InsertTx will only be visible after the transaction commits. The transaction commits if fn returns nil, and rolls back if fn returns an error.

Types

type App

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

App is the forge application lifecycle manager. Create with New(), configure with builder methods, start with Listen().

func New

func New(cfg Config) *App

New creates a new App from configuration loaded via LoadConfig.

func (*App) Listen

func (a *App) Listen(addr string) error

Listen starts the HTTP server and blocks until SIGTERM/SIGINT. On shutdown: stops accepting connections, drains in-flight requests, closes the DB pool (if created internally).

func (*App) Pool

func (a *App) Pool() *pgxpool.Pool

Pool returns the database connection pool. Only valid after Listen() has been called (for test infrastructure, use forge/forgetest).

func (*App) RegisterAPIRoutes

func (a *App) RegisterAPIRoutes(fn func(huma.API)) *App

RegisterAPIRoutes sets the function that registers all API routes on the Huma API. The function receives a huma.API and should call genapi.RegisterAllRoutes(api, registry).

func (*App) RegisterHTMLRoutes

func (a *App) RegisterHTMLRoutes(fn func(chi.Router)) *App

RegisterHTMLRoutes sets the function that registers all HTML routes on a chi.Router. The function receives a session-protected chi.Router group.

func (*App) RegisterPublicRoutes

func (a *App) RegisterPublicRoutes(fn func(chi.Router)) *App

RegisterPublicRoutes sets a function that registers routes outside the RequireSession middleware group. Use this for custom unauthenticated pages.

func (*App) RequireAuth

func (a *App) RequireAuth() *App

RequireAuth enables session enforcement on HTML routes. Unauthenticated users are redirected to /auth/login.

func (*App) UseAPIKeyStore

func (a *App) UseAPIKeyStore(ks auth.APIKeyStore) *App

UseAPIKeyStore sets the API key store for API authentication.

func (*App) UseOAuth

func (a *App) UseOAuth(cfg auth.OAuthConfig, findOrCreateUser auth.UserFinder) *App

UseOAuth configures OAuth2 providers (Google/GitHub) for HTML session auth.

func (*App) UsePasswordAuth

func (a *App) UsePasswordAuth(authenticateUser auth.PasswordAuthenticator) *App

UsePasswordAuth configures email/password authentication for HTML session auth.

func (*App) UsePool

func (a *App) UsePool(pool *pgxpool.Pool) *App

UsePool sets an externally-created database connection pool. When set, Listen() will skip internal pool creation and use this pool instead. The caller is responsible for closing the pool after Listen() returns.

func (*App) UseRecovery

func (a *App) UseRecovery(mw func(http.Handler) http.Handler) *App

UseRecovery sets a custom panic recovery middleware. If not called, a default passthrough middleware is used.

func (*App) UseTokenStore

func (a *App) UseTokenStore(ts auth.TokenStore) *App

UseTokenStore sets the bearer token store for API authentication.

type Config

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

Config is the runtime configuration for a forge application. Load from forge.toml with LoadConfig, or construct programmatically for testing.

func LoadConfig

func LoadConfig(path string) (Config, error)

LoadConfig reads forge.toml from the given path and returns a Config. Environment variable overrides (FORGE_*) are applied automatically.

func (Config) DatabaseURL

func (c Config) DatabaseURL() string

DatabaseURL returns the configured database connection string.

func (Config) ServerAddr

func (c Config) ServerAddr() string

ServerAddr returns the configured host:port for the HTTP server.

type DB

type DB interface {
	Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
	Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row
}

DB is an interface compatible with pgx transaction types. It allows code to accept either a pool or a transaction.

type Error

type Error struct {
	// Status is the HTTP status code (e.g. 400, 404, 500).
	Status int

	// Code is a machine-readable error code (e.g. "not_found", "validation_error").
	Code string

	// Message is a human-readable error summary.
	Message string

	// Detail provides additional context for debugging (may be empty).
	Detail string

	// Err is the underlying error, if any.
	Err error
}

Error represents a structured application error with HTTP status code mapping. Both API (JSON) and HTML (SSE) handlers use this type for consistent error responses.

func BadRequest

func BadRequest(message string) *Error

BadRequest returns a 400 error for invalid requests.

func Forbidden

func Forbidden(message string) *Error

Forbidden returns a 403 error for authorization failures.

func ForeignKeyViolation

func ForeignKeyViolation(field string) *Error

ForeignKeyViolation returns a 400 error for foreign key constraint violations.

func InternalError

func InternalError(err error) *Error

InternalError returns a 500 error wrapping an unexpected error.

func NewValidationError

func NewValidationError(errs interface{}) *Error

NewValidationError creates a 422 error from validation errors. Accepts any type with an Error() method to avoid importing validation package.

func NotFound

func NotFound(resource string, id any) *Error

NotFound returns a 404 error for a missing resource.

func Unauthorized

func Unauthorized(message string) *Error

Unauthorized returns a 401 error for authentication failures.

func UniqueViolation

func UniqueViolation(field string) *Error

UniqueViolation returns a 409 error for unique constraint violations.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface.

func (*Error) GetStatus

func (e *Error) GetStatus() int

GetStatus returns the HTTP status code.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the wrapped error for errors.Is/errors.As compatibility.

type TransactionFunc

type TransactionFunc func(ctx context.Context, tx pgx.Tx) error

TransactionFunc is a function that executes within a database transaction.

Directories

Path Synopsis
Package forgetest provides test infrastructure utilities for forge-generated applications.
Package forgetest provides test infrastructure utilities for forge-generated applications.

Jump to

Keyboard shortcuts

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