server

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0 modules, 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 server runs the three listeners of C-02.FR-3: ingest (ingestion, Heartbeat, Mattermost callbacks, the Telegram webhook), app (UI and API) and internal (health and metrics). When ingest and app have the same address, one server routes the ingest paths to the ingest handler and every other path to the app handler.

Index

Constants

View Source
const (
	ListenerApp      = "app"
	ListenerIngest   = "ingest"
	ListenerInternal = "internal"
)

The names of the listeners, as log lines and errors call them.

View Source
const ContentSecurityPolicy = "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; " +
	"connect-src 'self'; font-src 'self'; object-src 'none'; base-uri 'none'; form-action 'self'; " +
	"frame-ancestors 'none'"

ContentSecurityPolicy is the Content Security Policy of every answer of the app listener (C-03.FR-16): everything from the app's own origin, images also as data: URIs, no plugins, no base URI, forms posted only to itself and no framing.

Variables

This section is empty.

Functions

func App

func App(api http.Handler, dist fs.FS, hsts bool) http.Handler

App is the handler of the app listener: every path under /api/ goes to api, which answers unknown API paths with a problem, and every other path to the SPA in dist. Every answer carries the security headers, with Strict-Transport-Security when hsts is set, which it is when MUSTER_PUBLIC_URL is https.

func Ingest

func Ingest(ingest, heartbeat, callbacks http.Handler) http.Handler

Ingest is the handler of the ingest listener: heartbeat serves the Heartbeat endpoint, callbacks the messenger callbacks, and ingest everything else, ingestion first.

func Internal

func Internal(h *Health, metrics http.Handler) http.Handler

Internal is the handler of the internal listener: /health/live, /health/ready and metrics at /metrics (getMetrics).

func IsCallbackPath

func IsCallbackPath(p string) bool

IsCallbackPath reports whether p is under the messenger callbacks, /api/v1/callbacks/.

func IsHeartbeatPath

func IsHeartbeatPath(p string) bool

IsHeartbeatPath reports whether p is the Heartbeat endpoint, with or without a token in the path.

func IsIngestPath

func IsIngestPath(p string) bool

IsIngestPath reports whether the ingest handler serves p: ingestion, the Heartbeat, and the Mattermost and Telegram callbacks.

func Merge

func Merge(app, ingest http.Handler) http.Handler

Merge routes the ingest paths to ingest and every other path to app, for one port that serves both.

func SPA

func SPA(dist fs.FS) http.Handler

SPA serves the single-page application in dist: a file that exists as itself, any other path as index.html, so that the SPA's router handles it. A missing file under assets/ answers 404 instead: a page left open across an upgrade asks for chunks of the old build, and index.html in their place would fail as a script of the wrong type. Without a built index.html it answers 503.

Types

type Addresses

type Addresses struct {
	App, Ingest, Internal string
}

Addresses are the listen addresses.

type Handlers

type Handlers struct {
	App, Ingest, Internal http.Handler
}

Handlers serve the listeners.

type Health

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

Health answers the probes of the internal listener (C-02.FR-4): liveness without checks, readiness from the database, and not ready once the shutdown has begun.

func NewHealth

func NewHealth(ping func(context.Context) error) *Health

NewHealth checks readiness with ping, which runs SELECT 1 on the main pool within a second.

func (*Health) Live

func (h *Health) Live(w http.ResponseWriter, _ *http.Request)

Live is getHealthLive: 200 without checks.

func (*Health) Ready

func (h *Health) Ready(w http.ResponseWriter, r *http.Request)

Ready is getHealthReady: 200 while the database answers, 503 otherwise and during the shutdown.

func (*Health) ShuttingDown

func (h *Health) ShuttingDown()

ShuttingDown makes readiness answer 503 from now on.

type ListenerError

type ListenerError struct {
	Listener string
	Err      error
}

ListenerError is a listener that stopped serving.

func (*ListenerError) Error

func (e *ListenerError) Error() string

func (*ListenerError) Unwrap

func (e *ListenerError) Unwrap() error

type Server

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

Server is the running listeners.

func Start

func Start(ctx context.Context, addrs Addresses, h Handlers) (*Server, error)

Start listens on every address, then serves; a listener that cannot listen closes the others and is an error naming its variable. ctx is the base context of every request.

func (*Server) Addrs

func (s *Server) Addrs() Addresses

Addrs are the addresses the listeners are bound to; Ingest equals App when one port serves both.

func (*Server) Errors

func (s *Server) Errors() <-chan error

Errors delivers a ListenerError for each listener that stops serving on its own.

func (*Server) Shutdown

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

Shutdown stops accepting connections and waits for the requests in progress until ctx ends; then it closes the remaining connections and returns ctx's error. The app and ingest listeners drain first and the internal listener last, so that readiness keeps answering, with 503, and metrics stay scrapable during the drain.

Jump to

Keyboard shortcuts

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