server

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package server implements the brainjar HTTP server. It wraps a stdlib ServeMux and provides lifecycle management (listen, graceful shutdown). Routes are registered on the returned mux; middleware is assembled by the caller and wrapped via Handler() before serving.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	// Logger is attached to the server for informational logging. Defaults
	// to slog.Default() when nil. Middleware receive their own logger
	// where needed.
	Logger *slog.Logger
}

Options configures a Server. Zero values select sane defaults.

type Server

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

Server is the brainjar HTTP server.

func New

func New(host string, port int) *Server

New creates a Server bound to host:port. Callers register routes on Mux(), wrap it with middleware via Handler(), then ListenAndServe.

func NewWithOptions

func NewWithOptions(host string, port int, opts Options) *Server

NewWithOptions creates a Server with explicit options.

func (*Server) Handler

func (s *Server) Handler(h http.Handler)

Handler replaces the server's HTTP handler, typically with a middleware chain wrapping Mux(). Call after routes are registered and before ListenAndServe. If never called, the mux is served directly — no middleware, no auth.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() (addr string, errCh <-chan error, err error)

ListenAndServe binds the listener and starts serving. It returns the resolved address (useful when port=0) and a channel that receives the first non-ErrServerClosed error from Serve, or nil on clean shutdown.

func (*Server) Mux

func (s *Server) Mux() *http.ServeMux

Mux returns the underlying ServeMux for route registration.

func (*Server) RegisterHealth

func (s *Server) RegisterHealth()

RegisterHealth wires the /healthz endpoint. It returns 200 with a JSON body regardless of middleware — probes hit this directly. Liveness: no I/O, no dependency checks. Failing this means the process is wedged and the orchestrator should restart it.

func (*Server) RegisterReady

func (s *Server) RegisterReady(db *sql.DB)

RegisterReady wires the /readyz endpoint. It returns 200 when the process is ready to serve traffic (DB is reachable), 503 otherwise. Readiness: dependency checks live here. Failing this pulls the pod out of load balancing without restarting it — transient DB blips don't turn into restart loops.

func (*Server) Shutdown

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

Shutdown gracefully shuts down the server. In-flight requests get until ctx expires to complete.

Jump to

Keyboard shortcuts

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