Documentation
¶
Overview ¶
Package server provides the core HTTP server implementation for the application, including environment configuration, middleware management, static asset handling, development server proxying, and graceful shutdown capabilities.
The Server type embeds http.ServeMux and extends it with middleware support, asset manifest management, and environment-specific behaviors. It supports both development and production environments, allowing for flexible asset resolution and request handling.
Usage:
- Create a new server instance using New() with appropriate Options.
- Register middleware and route handlers as needed.
- Serve static assets and pages using provided methods.
- Start the server with Serve(), which handles graceful shutdown.
Example:
options := &server.Options{Env: server.Dev, Port: ":8080"}
srv := server.New(options)
srv.Serve()
Index ¶
- Constants
- func ActionsHandler(server *Server, actions map[string]ActionFunc, ...) http.Handler
- func Async(comp html.Component, fallback html.Node) html.Component
- func Data[T any](ctx context.Context) (*T, error)
- func DefaultLayout(server *Server, head html.Node, children html.Node) html.Node
- func Form(name string, items ...html.Item) html.Node
- func FormData[T any](r *http.Request) (*T, error)
- func NotFound(ctx context.Context) html.Node
- func PageHandler(server *Server, page Page, layout Layout, middlewares ...middleware.Middleware) http.Handler
- func Redirect(ctx context.Context, to string) html.Node
- func RedirectComponent(to string) html.Component
- func RedirectWith(ctx context.Context, to string, status int) html.Node
- func RedirectWithComponent(to string, status int) html.Component
- func SetCookie(ctx context.Context, cookie *http.Cookie) html.Node
- func SetCookieComponent(cookie *http.Cookie) html.Component
- type ActionFunc
- type Environment
- type Layout
- type Options
- type Page
- type PageFunc
- type RequestDetail
- type Server
- func (s *Server) Asset(name string) string
- func (s *Server) Env() Environment
- func (s *Server) HandlePage(pattern string, page Page, layout Layout, middlewares ...middleware.Middleware)
- func (s *Server) RegisterDevHandlers()
- func (s *Server) Serve()
- func (s *Server) SetBuildDir(name string, dir fs.FS, vite fs.FS) error
- func (s *Server) SetNotFoundPage(page Page, layout Layout)
- func (s *Server) Use(middlewares ...middleware.Middleware)
- type StaticRenderer
Constants ¶
const ( // Reprensents a development environment of the app. Dev = Environment("DEVELOPMENT") // Reprensents a production environment of the app. Prod = Environment("PRODUCTION") )
Variables ¶
This section is empty.
Functions ¶
func ActionsHandler ¶ added in v0.7.0
func ActionsHandler(server *Server, actions map[string]ActionFunc, middlewares ...middleware.Middleware) http.Handler
func DefaultLayout ¶ added in v0.6.3
func PageHandler ¶ added in v0.7.0
func PageHandler(server *Server, page Page, layout Layout, middlewares ...middleware.Middleware) http.Handler
PageHandler creates an HTTP handler that processes requests using the provided function `fn`, which takes a context and a pointer to a parameter struct of type P. The handler applies the specified layout function `layout` to the resulting HTML node, and supports an optional list of middleware functions. The handler automatically scans and populates the parameter struct from the request's query parameters, headers, cookies, and path variables. If scanning fails, it responds with a 400 Bad Request and an error node. Otherwise, it renders the node returned by `fn`, applies the layout, and supports chunked rendering if needed. All provided and application-level middlewares are applied in order.
Type Parameters:
- P: The type of the parameter struct to be populated from the request.
Parameters:
- app: The application context containing shared resources and middlewares.
- fn: A function that generates an HTML node given a context and a pointer to P.
- layout: A layout function to wrap the generated HTML node. If nil, a default passthrough is used.
- middlewares: Optional HTTP middleware functions to wrap the handler.
Returns:
- http.Handler: The composed HTTP handler ready to be registered with a router or server.
func RedirectComponent ¶
Types ¶
type ActionFunc ¶ added in v0.7.0
type ActionFunc func(http.ResponseWriter, *http.Request) error
type Environment ¶
type Environment string
Environment describes the app environment and lets the other parts of the app choose environment specific behaviour. Valid values are "DEVELOPMENT" and "PRODUCTION".
type Layout ¶ added in v0.6.3
Layout defines a function type that takes a context and an html.Node as input, and returns a modified html.Node. It is typically used to apply layout transformations or wrappers to HTML nodes within a given context.
type Options ¶
type Options struct {
Env Environment
Port string
DevServer *url.URL
Logger *slog.Logger
Mux *http.ServeMux
}
Options holds the configuration settings for the server, including environment, port, development server URL, logger, and HTTP request multiplexer.
type RequestDetail ¶
type Server ¶
func New ¶
New creates and returns a new Server instance using the provided Options. It sets default values for any missing options, including the HTTP mux, development server URL, environment, and logger. The function also attaches default middleware for logging and recovery, and sets the default "not found" page handler.
Parameters:
options - a pointer to an Options struct containing configuration for the server.
Returns:
A pointer to the initialized Server.
func (*Server) Asset ¶
Asset returns the URL or path for a given asset name based on the current application context. In development mode, it constructs the asset URL using the development server and webfiles path. In production mode, it retrieves the asset entry from the application's entries map. If called outside of a server rendering context or if the asset is not found, the function logs a fatal error.
Parameters:
ctx - The context, expected to be of type *Context. name - The name of the asset to retrieve.
Returns:
The URL or path to the requested asset as a string.
func (*Server) Env ¶ added in v0.7.2
func (s *Server) Env() Environment
Returns the configured server environment
func (*Server) HandlePage ¶
func (s *Server) HandlePage(pattern string, page Page, layout Layout, middlewares ...middleware.Middleware)
func (*Server) RegisterDevHandlers ¶
func (s *Server) RegisterDevHandlers()
func (*Server) SetNotFoundPage ¶ added in v0.6.3
func (*Server) Use ¶
func (s *Server) Use(middlewares ...middleware.Middleware)
Adds middleware(s) to the application's middleware stack.
type StaticRenderer ¶
type StaticRenderer struct {
// contains filtered or unexported fields
}
func NewStaticRenderer ¶
func NewStaticRenderer() *StaticRenderer
func (*StaticRenderer) Clear ¶
func (r *StaticRenderer) Clear()
Directories
¶
| Path | Synopsis |
|---|---|
|
Package font provides utilities for managing and generating Google Fonts URLs, including font weights, subsets, and display options.
|
Package font provides utilities for managing and generating Google Fonts URLs, including font weights, subsets, and display options. |
|
Package i18n provides internationalization (i18n) support for the application, enabling the loading and rendering of localized messages using JSON translation files.
|
Package i18n provides internationalization (i18n) support for the application, enabling the loading and rendering of localized messages using JSON translation files. |
|
Package middleware provides HTTP middleware utilities for handling color scheme preferences, locale selection, caching, logging, and gzip compression.
|
Package middleware provides HTTP middleware utilities for handling color scheme preferences, locale selection, caching, logging, and gzip compression. |