server

package module
v1.7.20 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 10 Imported by: 13 modules, 26 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.

README

go-server

go-server is a Go module of packages for building HTTP servers and their command-line clients. Applications import it:

go get github.com/mutablelogic/go-server

Packages

Package Description
pkg/cmd Command-line applications, with a server command, global flags and defaults which are kept between commands
pkg/httpserver The HTTP server, with its timeouts
pkg/httprouter The router, with cross-origin requests, and an OpenAPI specification of its paths
pkg/httprequest Reading requests: JSON, forms, queries, and files streamed from the request
pkg/httpresponse Writing responses: JSON, errors, streams, and files, with their caching and preconditions
pkg/jsonschema JSON schemas of Go types, and validating JSON with them
pkg/openapi OpenAPI specifications, and their documentation in markdown
pkg/otel OpenTelemetry tracing
pkg/logger Structured logging
pkg/tui Tables and other output for terminals
pkg/types Common types, such as files, content types and headers
pkg/version The version of a build

Building and testing

You need Go, the version in go.mod, and Make.

Target Description
make test Runs go vet, and the unit and coverage tests
make vet Runs go vet
make unit-test Runs the unit tests, verbosely with VERBOSE=1
make race-test Runs the unit tests with the race detector
make coverage-test Reports the tests' coverage
make tidy Runs go mod tidy
make clean Removes the build directory

The tests run in GitHub Actions for pull requests and pushes to main on Linux, and for releases on Windows and macOS too.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cmd added in v1.5.6

type Cmd interface {
	// Name returns the executable name.
	Name() string

	// Description returns the application description string.
	Description() string

	// Version returns the application version string.
	Version() string

	// Context returns the lifecycle context (cancelled on SIGINT).
	Context() context.Context

	// Logger returns the structured logger.
	Logger() *slog.Logger

	// Tracer returns the OpenTelemetry tracer, or nil if OTel is not configured.
	Tracer() trace.Tracer

	// Meter returns the OpenTelemetry meter, or nil if OTel is not configured.
	Meter() metric.Meter

	// ClientEndpoint returns the HTTP endpoint URL and client options derived
	// from the global HTTP flags.
	ClientEndpoint() (string, []client.ClientOpt, error)

	// URL returns the advertised server URL, derived from the global HTTP flags. Returns
	// nil if the URL cannot be determined.
	URL() *url.URL

	// Get retrieves a default value by key. Returns nil if the key does not exist.
	Get(string) any

	// GetString retrieves a default string value by key. Returns empty string if the key
	// does not exist or the value is not a string.
	GetString(string) string

	// Set stores a default value by key and persists the store to disk.
	// Pass nil to remove a key.
	Set(string, any) error

	// Keys returns all keys in the store.
	Keys() []string

	// IsTerm returns 0 if stderr is not an interactive terminal, or the
	// width of the terminal if it is.
	IsTerm() int

	// IsDebug reports whether debug logging is enabled.
	IsDebug() bool

	// HTTPAddr returns the HTTP listen/connect address.
	HTTPAddr() string

	// HTTPPrefix returns the HTTP path prefix.
	HTTPPrefix() string

	// HTTPTimeout returns the HTTP read/write timeout.
	HTTPTimeout() time.Duration

	// WithContext returns a copy of the Cmd with the provided context.
	WithContext(context.Context) Cmd
}

Cmd provides access to the runtime context that is set up by the main entry point and passed to command Run methods.

type HTTPFileServer

type HTTPFileServer interface {
	// HandlerPath returns the route path relative to the router prefix.
	HandlerPath() string

	// HandlerFS returns the filesystem to serve.
	HandlerFS() fs.FS

	// Spec returns the OpenAPI path-item description for this
	// handler, or nil if no spec is provided.
	Spec() *openapi.PathItem
}

HTTPFileServer defines methods for static file serving handlers. When the router detects a handler that implements this interface, it uses [Router.RegisterFS] instead of [Router.RegisterFunc] so that the router prefix is correctly stripped from the request path.

type HTTPHandler

type HTTPHandler interface {
	// HandlerPath returns the route path relative to the router prefix
	// (e.g. "resource", "resource/{id}").
	HandlerPath() string

	// HandlerFunc returns the HTTP handler function.
	HandlerFunc() http.HandlerFunc

	// Spec returns the OpenAPI path-item description for this
	// handler, or nil if no spec is provided.
	Spec() *openapi.PathItem
}

HTTPHandler defines methods for HTTP handlers

type HTTPMiddleware

type HTTPMiddleware interface {
	WrapFunc(http.HandlerFunc) http.HandlerFunc
}

HTTPMiddleware defines methods for HTTP middleware

type HTTPRouter

type HTTPRouter interface {
	http.Handler

	// Spec returns the OpenAPI specification for this router.
	Spec() *openapi.Spec
}

HTTPRouter defines methods for a http router

type HTTPServer

type HTTPServer interface {
	// Spec returns the OpenAPI server entry for this instance,
	// or nil if not yet available.
	Spec() *openapi.Server
}

HTTPServer defines methods for an HTTP server instance. The router uses this interface to populate the OpenAPI spec's servers list.

Directories

Path Synopsis
pkg
cmd
httprouter
Package httprouter provides an HTTP request router with built-in cross-origin request forgery (CSRF) protection, middleware support, and OpenAPI spec generation.
Package httprouter provides an HTTP request router with built-in cross-origin request forgery (CSRF) protection, middleware support, and OpenAPI spec generation.
openapi/schema
Package schema provides Go types for the OpenAPI 3.1 specification.
Package schema provides Go types for the OpenAPI 3.1 specification.
openapi/static
Package static contains embedded static files for the OpenAPI handler.
Package static contains embedded static files for the OpenAPI handler.
provider/schema/schematest
Package schematest provides shared mock implementations of schema.Resource and schema.ResourceInstance for use in tests.
Package schematest provides shared mock implementations of schema.Resource and schema.ResourceInstance for use in tests.
tui

Jump to

Keyboard shortcuts

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