app

package
v2.0.0-...-51e8ac7 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package app provides a framework-neutral application bootstrap for v2 requestCore applications.

The App composes the v2 Runtime with framework-specific routers, middleware, and lifecycle management. It supports Gin, Fiber, and net/http+chi as underlying frameworks.

Index

Constants

View Source
const DefaultSessionCookieName = "session"

DefaultSessionCookieName is the default cookie name for sessions.

Variables

This section is empty.

Functions

func FlashMiddleware

func FlashMiddleware() routing.Middleware

FlashMiddleware creates a middleware that initializes flash messages from the session if not already set.

func SessionMiddleware

func SessionMiddleware(mgr *session.Manager, cookieName string) routing.Middleware

SessionMiddleware creates a middleware that loads the session from the request cookie and registers a before-commit hook to persist any dirty session state back to the response cookie before headers/body are written.

This is a thin wrapper around session.Middleware that also sets the Session and Flash fields on the v2 RequestContext for handler access. The session is saved via a before-commit hook, not after the handler returns, ensuring cookies are set before the response body is written.

Types

type App

type App struct {
	Router      routing.Router
	RespHandler *v2response.Handler
	Registry    v2response.Registry
	Renderer    renderers.Renderer
	Worker      workers.Worker
	Scheduler   *workers.Scheduler
	Sessions    *session.Manager
	Middlewares []routing.Middleware
	// contains filtered or unexported fields
}

App is the v2 application instance. It composes the router, response handler, worker pool, scheduler, and session manager.

func Bootstrap

func Bootstrap(config Config) (*App, error)

Bootstrap creates a new v2 App with the given configuration. It initializes the router, response handler, worker pool, and session manager based on the specified framework.

The returned App is ready for route registration via Register() or direct router access via Router.

func (*App) Close

func (a *App) Close() error

Close stops the worker pool and scheduler, and releases resources. It uses a 10-second timeout for the shutdown.

For full graceful shutdown including the HTTP server, use Shutdown instead. Close is primarily useful in tests where the HTTP server was never started.

func (*App) Register

func (a *App) Register(prefix string, middlewares ...routing.Middleware) routing.RouteGroup

Register registers a route group with middleware.

func (*App) Shutdown

func (a *App) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the application: it stops the HTTP server, then shuts down the worker pool. Both operations are bounded by the given context.

Shutdown is safe to call concurrently with StartWithContext. It waits deterministically for StartWithContext to register the server (via a closed channel) before proceeding, eliminating the prior polling race.

http.ErrServerClosed is treated as a clean termination (return nil) because it indicates the server was already shut down, typically by a concurrent StartWithContext context cancellation.

func (*App) Start

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

Start starts the HTTP server on the given address. For Gin and chi/net/http, this uses http.Server. For Fiber, this uses fiber.App.Listen.

func (*App) StartWithContext

func (a *App) StartWithContext(ctx context.Context, addr string) error

StartWithContext starts the HTTP server with the given context. When the context is cancelled, the server shuts down gracefully.

The error handler registry is frozen before the server starts so that route handlers cannot modify error handlers at runtime.

type Config

type Config struct {
	// Framework specifies the underlying web framework.
	Framework Framework

	// Renderer is the default response renderer.
	// Default: JSONRenderer.
	Renderer renderers.Renderer

	// WorkerConfig configures the in-process worker pool.
	// Default: DefaultConfig().
	WorkerConfig workers.Config

	// SessionStore is the session store for session middleware.
	// Default: NoOpStore.
	SessionStore session.Store

	// SessionSecret is the secret used for signing session cookies.
	// Required if SessionStore is not NoOpStore.
	SessionSecret string

	// LegacyCore is the v1 RequestCoreInterface for infrastructure access.
	// May be nil for pure v2 applications.
	LegacyCore requestCore.RequestCoreInterface

	// LegacyHandler is the v1 WebHanlder for error response formatting.
	// Default: empty WebHanlder.
	LegacyHandler response.WebHanlder

	// Middlewares are global middleware applied to all routes.
	Middlewares []routing.Middleware

	// NotFound is the handler for unmatched routes.
	// If nil, a default 404 JSON response is used.
	NotFound routing.Handler

	// MethodNotAllowed is the handler for disallowed methods.
	// If nil, a default 405 JSON response is used.
	MethodNotAllowed routing.Handler
}

Config holds the configuration for bootstrapping a v2 application.

type Framework

type Framework string

Framework identifies the underlying web framework.

const (
	FrameworkGin     Framework = "gin"
	FrameworkFiber   Framework = "fiber"
	FrameworkChi     Framework = "chi"
	FrameworkNetHTTP Framework = "net/http"
)

Jump to

Keyboard shortcuts

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