httpserver

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 17 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package httpserver is the public HTTP server component: a net/http Server with sane timeouts over a Go 1.22+ ServeMux, health probes on the public port, and 404/405 rendered as the standard error envelope.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(mux *http.ServeMux) http.Handler

Handler wraps mux so unmatched requests render the error envelope instead of net/http's plain-text 404 and 405. Matched requests go straight to the mux.

func NewMux

func NewMux() *http.ServeMux

NewMux returns the ServeMux services register routes on.

func NewSPA

func NewSPA(cfg SPAConfig) (http.Handler, error)

NewSPA returns the catch-all handler: files from cfg.FS at their paths, the shell (index.html) for every other GET so client-side routes deep-link, and the error envelope for everything that is neither.

func RegisterHealth

func RegisterHealth(mux *http.ServeMux, reg *health.Registry)

RegisterHealth serves /healthz and /readyz on the public mux. Platform health checks (Tsuru, Fly, Docker HEALTHCHECK) probe the app port and cannot reach the admin server.

Types

type Config

type Config struct {
	Addr              string
	ReadHeaderTimeout time.Duration
	ReadTimeout       time.Duration
	WriteTimeout      time.Duration
	IdleTimeout       time.Duration
}

Config is the server's listen address and timeouts.

type Option

type Option func(*Server)

Option configures the Server.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets the logger.

func WithName

func WithName(name string) Option

WithName sets the component name used in logs and errors (default "http").

type SPAConfig

type SPAConfig struct {
	// FS holds the built application: index.html at the root next to its
	// assets (the output directory of Vite, esbuild or webpack).
	FS fs.FS
	// ServerPrefixes are paths the server owns ("/api/", "/webhooks/"): a
	// request under one that matches no route is a 404 error, never the
	// shell.
	ServerPrefixes []string
	// Immutable lists the path prefixes whose files are content-hashed and
	// can be cached forever. Default: "/assets/", where Vite and most
	// bundlers put them. Everything else revalidates.
	Immutable []string
	// Head returns HTML inserted before </head> of the shell on every
	// request: meta tags carrying per-request values such as a CSRF token,
	// the locale or public configuration. It must escape what it prints.
	Head func(r *http.Request) string
}

SPAConfig describes a single-page application served by the same process as its API.

type Server

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

Server is a lifecycle Component and Runner around http.Server.

func New

func New(cfg Config, handler http.Handler, opts ...Option) *Server

New builds a server for handler. Start binds, Run serves, Stop drains.

func (*Server) Addr

func (s *Server) Addr() string

Addr returns the bound address once Start has succeeded.

func (*Server) Name

func (s *Server) Name() string

Name implements lifecycle.Component.

func (*Server) Run

func (s *Server) Run(context.Context) error

Run serves until Stop. It returns nil after a clean shutdown.

func (*Server) Start

func (s *Server) Start(context.Context) error

Start binds the listener so a busy port fails the boot, not the first request.

func (*Server) Stop

func (s *Server) Stop(ctx context.Context) error

Stop drains in-flight requests within ctx. If the deadline passes it closes the remaining connections, so a slow client cannot hold the process open, and reports that it had to.

Jump to

Keyboard shortcuts

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