httpserver

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	StatusEndPnt = "/status"
	HealthEndPnt = "/health"
)

Functions

func Handle

func Handle(h APIHandler) httprouter.Handle

handle adapts an apiHandler into an httprouter.Handle: encode the payload as 200 JSON, or map the error to a status and encode it. This and Health are the only places that write responses.

func HealthHandler

func HealthHandler(service, version string, checks ...Check) httprouter.Handle

HealthHandler serves a readiness report: it runs every check and returns 503 with the failure list when any fail, else 200. This is the single home of the report-body-with-non-200 shape (which the Handle adapter can't express), so each service supplies only its own list of checks rather than reimplementing the mechanism.

func StatusHandler

func StatusHandler(service, version string) httprouter.Handle

StatusHandler serves a static liveness response — always 200 while the process is up. Shared by every service so the /status contract is identical.

func WriteJSON

func WriteJSON(w http.ResponseWriter, code int, payload any) error

WriteJSON encodes payload as a JSON response with the given status code. It is exported for the handful of endpoints that must set a non-200 status on a success-shaped body (e.g. a health probe returning 503 with its report), which the Handle adapter deliberately cannot express.

Types

type APIHandler

type APIHandler func(r *http.Request, p httprouter.Params) (any, error)

APIHandler is a transport-decoupled handler: it reads the request and returns a payload or an error. It never touches the ResponseWriter or an HTTP status code — encoding and status selection are the adapter's job (handle), so the business logic stays reusable outside HTTP.

type Check

type Check struct {
	Name  string
	Probe func(context.Context) error
}

Check is a named readiness probe for one dependency. Services declare a Check per backend they depend on; HealthHandler runs them all.

type HTTPServer

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

func New

func New(port int, svc services.Service) *HTTPServer

func (*HTTPServer) Port

func (h *HTTPServer) Port() string

func (*HTTPServer) Start

func (h *HTTPServer) Start(ctx context.Context) error

Start boots the wrapped service's background work, then serves HTTP. It is the single lifecycle root for a node: the same call starts the domain service (e.g. the orders outbox relay) and the listener, so every service — orders today, the notifier next — gets identical wiring. A service start failure is returned and the listener is not started.

func (*HTTPServer) Stop

func (h *HTTPServer) Stop() error

Stop gracefully shuts down the node: the HTTP listener first (so no new requests can enqueue work), then the domain service (drain background work).

Directories

Path Synopsis
Package middleware holds HTTP middleware shared by every service that runs on the common httpserver.
Package middleware holds HTTP middleware shared by every service that runs on the common httpserver.

Jump to

Keyboard shortcuts

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