framework

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2026 License: MIT Imports: 40 Imported by: 0

Documentation

Overview

Package framework provides Gombit's runtime application lifecycle and HTTP surfaces (Gin router escape hatch and Huma contract API).

Index

Constants

View Source
const RequestIDHeader = "X-Request-Id"

RequestIDHeader is the HTTP header carrying a stable per-request ID.

View Source
const TraceIDHeader = "X-Trace-Id"

TraceIDHeader exposes the active trace ID for logs, diagnostics, and tests.

View Source
const TraceparentHeader = "Traceparent"

TraceparentHeader is the W3C trace context header.

Variables

This section is empty.

Functions

func GetRequestID

func GetRequestID(c *gin.Context) string

GetRequestID returns the request ID stored in the Gin context.

func GetRequestIDFromContext

func GetRequestIDFromContext(ctx context.Context) string

GetRequestIDFromContext reads the request ID from a request context.

func GetTraceID

func GetTraceID(c *gin.Context) string

GetTraceID returns the trace ID stored in the Gin context.

func GetTraceIDFromContext

func GetTraceIDFromContext(ctx context.Context) string

GetTraceIDFromContext reads the trace ID from a request context.

func Run

func Run(app *App) error

Run runs app until an interrupt or terminate signal is received.

func RunContext

func RunContext(ctx context.Context, app *App) error

RunContext runs app until ctx is canceled or the HTTP server fails.

Types

type App

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

App owns Gombit's runtime lifecycle and HTTP router.

func New

func New(options ...Option) (*App, error)

New creates an application using process configuration and the default router.

func (*App) API

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

API returns the Huma API used for contract-typed route registration.

func (*App) Addr

func (a *App) Addr() string

Addr returns the bound HTTP address after the app has started.

func (*App) Cache

func (a *App) Cache() cache.Cache

Cache returns the configured cache implementation.

func (*App) Config

func (a *App) Config() config.Config

Config returns the typed app configuration.

func (*App) DB

func (a *App) DB() *gorm.DB

DB returns the underlying GORM database escape hatch.

func (*App) Database

func (a *App) Database() *database.DB

Database returns the opened database handle with driver metadata.

func (*App) Logger

func (a *App) Logger() *zap.Logger

Logger returns the app's Zap logger.

func (*App) OnStart

func (a *App) OnStart(hook Hook)

OnStart registers a start hook. Hooks run in registration order.

func (*App) OnStop

func (a *App) OnStop(hook Hook)

OnStop registers a stop hook. Hooks run in reverse registration order.

func (*App) Redis

func (a *App) Redis() *redis.Client

Redis returns the underlying go-redis client when Redis is enabled.

func (*App) Router

func (a *App) Router() *gin.Engine

Router returns the underlying Gin router escape hatch.

func (*App) Tx added in v0.1.7

func (a *App) Tx(ctx context.Context, fn func(tx *gorm.DB) error) error

Tx runs fn inside a single database transaction: it commits when fn returns nil and rolls back on any error or panic. It is the framework home for transactional multi-model writes (atomically update A + B + C) and for cross-row invariants that must read and write consistently. Model Validate hooks (database.Validator) run inside this transaction too. Use struct writes (Create/Save/Updates(struct)) so Validate sees the values being written — an invariant checked in Validate then cannot commit alongside a change that violates it.

err := app.Tx(ctx, func(tx *gorm.DB) error {
    if err := tx.Create(&a).Error; err != nil { return err }
    b.Count++
    return tx.Save(&b).Error
})

type Hook

type Hook func(context.Context) error

Hook is an application lifecycle callback.

type Option

type Option func(*App) error

Option configures an App.

func WithCSRFExemptPaths added in v0.1.6

func WithCSRFExemptPaths(paths ...string) Option

WithCSRFExemptPaths marks exact request paths that opt out of cookie-mode CSRF enforcement on unsafe methods. Use it for non-browser endpoints that cannot participate in the double-submit defense — webhooks, server-to-server callbacks — which must authenticate themselves by other means (e.g. HMAC signature verification). Paths match the request path exactly, including the API prefix, e.g. "/api/v1/webhooks/github". No effect in JWT mode or when a custom router is supplied via WithRouter.

func WithCache

func WithCache(c cache.Cache) Option

WithCache attaches an application-owned cache implementation to the app.

func WithConfig

func WithConfig(cfg config.Config) Option

WithConfig sets the app configuration. DocsEnabled is taken as given: Default() leaves /docs on even if you later set Environment to production. Use config.DefaultFor(env) or set API.DocsEnabled yourself.

func WithDatabase

func WithDatabase(db *database.DB) Option

WithDatabase attaches an opened database handle to the app.

func WithEmbeddedFrontend

func WithEmbeddedFrontend(fsys fs.FS) Option

WithEmbeddedFrontend stores fsys and, when it contains index.html at its root, installs a Gin NoRoute handler after framework routes so unmatched GET paths serve the SPA. Huma /api/*, /openapi.json, /docs, and the probe routes still win because they are registered before NoRoute runs.

If fsys has no index.html (the gombit new placeholder embed), NoRoute is not installed and unknown paths keep their current 404. Split deploy is the default (C5); embedding is opt-in via gombit build --embed.

func WithLogger

func WithLogger(logger *zap.Logger) Option

WithLogger attaches a Zap logger to the app.

func WithRawBodyPaths added in v0.1.7

func WithRawBodyPaths(paths ...string) Option

WithRawBodyPaths marks exact request paths whose request body must reach the handler byte-for-byte unmodified — webhooks and other server-to-server endpoints that verify a signature over the raw body (e.g. GitHub's X-Hub-Signature-256 HMAC). The XSS input sanitizer, which otherwise re-encodes JSON request bodies, is skipped for these paths.

Such an endpoint also cannot participate in the cookie CSRF double-submit, so raw-body paths are additionally CSRF-exempt (the union with WithCSRFExemptPaths) — declaring a webhook path here is enough. The handler must authenticate the caller itself. Paths match the request path exactly, including the API prefix, e.g. "/api/v1/webhooks/github". No effect when a custom router is supplied via WithRouter.

func WithRedis

func WithRedis(client *redis.Client) Option

WithRedis attaches an application-owned Redis client as the app cache.

func WithRouter

func WithRouter(router *gin.Engine) Option

WithRouter sets the app router.

func WithShutdownTimeout

func WithShutdownTimeout(timeout time.Duration) Option

WithShutdownTimeout sets the bounded shutdown timeout.

Jump to

Keyboard shortcuts

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