server

package
v0.5.11 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package server wires the HTTP surface: middleware, health probes, metrics, graceful shutdown, and a chi router that other packages mount routes onto.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	Addr            string
	ShutdownTimeout time.Duration
	// APIKey, when non-empty, gates /metrics on the main port behind the same
	// bearer token used by the /v1 routes. A dedicated MetricsAddr port is
	// unauthenticated instead.
	APIKey string
	// MetricsAddr, when set and distinct from Addr, serves /metrics on its own
	// unauthenticated listener instead of the main router — a port meant to stay
	// in-cluster (keep it off any public route).
	MetricsAddr string
	// UIAddr, when set and distinct from Addr, serves the admin UI on its own
	// listener instead of the main router (see MountUI). The shell embeds the
	// API key, so isolating it keeps that key off the main port — expose UIAddr
	// only on a trusted (LAN) gateway.
	UIAddr string
}

Options configures the server without importing the config package.

type ReadinessFunc

type ReadinessFunc func(context.Context) error

ReadinessFunc reports whether the service is ready to serve traffic. A nil error means ready; a non-nil error is surfaced on /readyz.

type Server

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

Server owns the HTTP server and its router.

func New

func New(opts Options, log *slog.Logger, reg *prometheus.Registry) *Server

New builds a Server with base middleware, /healthz, /readyz and /metrics. Additional routes are mounted via Router before calling Run.

func (*Server) MountUI added in v0.5.10

func (s *Server) MountUI(spa http.Handler)

MountUI arranges for the SPA handler to be served. When UIAddr is set and distinct from Addr, spa is served ONLY on that dedicated listener, which delegates requests matching an API route to the main router so the same-origin SPA can still call /v1 — keeping the token-embedding shell off the main port. Otherwise spa is mounted as a catch-all on the main router, serving it on the main port (single-listener default).

func (*Server) Router

func (s *Server) Router() chi.Router

Router exposes the underlying router so other packages can mount routes.

func (*Server) Run

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

Run starts the HTTP server and blocks until ctx is cancelled, then performs a graceful shutdown bounded by the configured timeout.

func (*Server) SetReady

func (s *Server) SetReady(fn ReadinessFunc)

SetReady installs the readiness check used by /readyz.

Jump to

Keyboard shortcuts

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