server

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: MIT Imports: 25 Imported by: 0

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

View Source
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 Async

func Async(comp html.Component, fallback html.Node) html.Component

func Data

func Data[T any](ctx context.Context) (*T, error)

func DefaultLayout added in v0.6.3

func DefaultLayout(server *Server, head html.Node, children html.Node) html.Node

func Form added in v0.7.0

func Form(name string, items ...html.Item) html.Node

func FormData added in v0.7.0

func FormData[T any](r *http.Request) (*T, error)

func NotFound

func NotFound(ctx context.Context) html.Node

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 Redirect

func Redirect(ctx context.Context, to string) html.Node

func RedirectComponent

func RedirectComponent(to string) html.Component

func RedirectWith

func RedirectWith(ctx context.Context, to string, status int) html.Node

func RedirectWithComponent

func RedirectWithComponent(to string, status int) html.Component

func SetCookie

func SetCookie(ctx context.Context, cookie *http.Cookie) html.Node

func SetCookieComponent

func SetCookieComponent(cookie *http.Cookie) html.Component

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

type Layout func(*Server, html.Node, html.Node) html.Node

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 Page

type Page interface {
	Page() html.Node
}

type PageFunc added in v0.6.3

type PageFunc func() html.Node

func (PageFunc) Metadata added in v0.6.3

func (PageFunc) Metadata() *metadata.Metadata

func (PageFunc) Page added in v0.6.3

func (p PageFunc) Page() html.Node

type RequestDetail

type RequestDetail struct {
	Header http.Header
	URL    *url.URL
	Host,
	Method,
	Pattern,
	RemoteAddr,
	RequestURI string
	Cookies []*http.Cookie
}

func Detail

func Detail(ctx context.Context) (*RequestDetail, error)

func (*RequestDetail) Cookie added in v0.7.0

func (d *RequestDetail) Cookie(name string) (*http.Cookie, error)

type Server

type Server struct {
	*http.ServeMux
	// contains filtered or unexported fields
}

func New

func New(options *Options) *Server

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

func (s *Server) Asset(name string) string

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) Serve

func (s *Server) Serve()

func (*Server) SetBuildDir

func (s *Server) SetBuildDir(name string, dir fs.FS, vite fs.FS) error

func (*Server) SetNotFoundPage added in v0.6.3

func (s *Server) SetNotFoundPage(page Page, layout Layout)

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) Build

func (r *StaticRenderer) Build(node html.Node) error

func (*StaticRenderer) Clear

func (r *StaticRenderer) Clear()

func (*StaticRenderer) Render

func (r *StaticRenderer) Render(ctx context.Context, w io.Writer) error

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.

Jump to

Keyboard shortcuts

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