Documentation
¶
Overview ¶
Package http provides small HTTP wrappers and helpers around the standard library net/http package.
This package primarily re-exports common net/http types and constants behind go-service aliases and provides a few convenience helpers used by transport wiring, such as:
- NewClient, which wraps a RoundTripper with OpenTelemetry instrumentation and applies a client timeout,
- NewServer, which builds an http.Server using configured timeouts and protocol settings,
- Handle/HandleFunc, which register handlers wrapped with OpenTelemetry instrumentation,
- Pattern and ParseServiceMethod, which help standardize route naming for telemetry.
Start with `NewClient` and `NewServer`.
Index ¶
- Constants
- Variables
- func Handle(mux *ServeMux, pattern string, handler http.Handler)
- func HandleFunc(mux *ServeMux, pattern string, handler http.HandlerFunc)
- func NewClient(rt http.RoundTripper, timeout time.Duration) *http.Client
- func ParseServiceMethod(req *http.Request) (string, string)
- func Pattern(name env.Name, pattern string) string
- func Protocols() *http.Protocols
- func StatusText(code int) string
- func Transport(cfg *tls.Config) *http.Transport
- type Client
- type Handler
- type HandlerFunc
- type Header
- type MaxBytesError
- type Request
- type Response
- type ResponseWriter
- type RoundTripper
- type ServeMux
- type Server
Constants ¶
const MethodDelete = http.MethodDelete
MethodDelete is an alias of http.MethodDelete.
const MethodGet = http.MethodGet
MethodGet is an alias of http.MethodGet.
const MethodPatch = http.MethodPatch
MethodPatch is an alias of http.MethodPatch.
const MethodPost = http.MethodPost
MethodPost is an alias of http.MethodPost.
const MethodPut = http.MethodPut
MethodPut is an alias of http.MethodPut.
const StatusBadRequest = http.StatusBadRequest
StatusBadRequest is an alias of http.StatusBadRequest.
const StatusConflict = http.StatusConflict
StatusConflict is an alias of http.StatusConflict.
const StatusForbidden = http.StatusForbidden
StatusForbidden is an alias of http.StatusForbidden.
const StatusGatewayTimeout = http.StatusGatewayTimeout
StatusGatewayTimeout is an alias of http.StatusGatewayTimeout.
const StatusInternalServerError = http.StatusInternalServerError
StatusInternalServerError is an alias of http.StatusInternalServerError.
const StatusNotFound = http.StatusNotFound
StatusNotFound is an alias of http.StatusNotFound.
const StatusNotImplemented = http.StatusNotImplemented
StatusNotImplemented is an alias of http.StatusNotImplemented.
const StatusOK = http.StatusOK
StatusOK is an alias of http.StatusOK.
const StatusRequestEntityTooLarge = http.StatusRequestEntityTooLarge
StatusRequestEntityTooLarge is an alias of http.StatusRequestEntityTooLarge.
StatusServiceUnavailable is an alias of http.StatusServiceUnavailable.
const StatusTooManyRequests = http.StatusTooManyRequests
StatusTooManyRequests is an alias of http.StatusTooManyRequests.
StatusUnauthorized is an alias of http.StatusUnauthorized.
Variables ¶
var ( // DefaultTransport is an alias for http.DefaultTransport. DefaultTransport = http.DefaultTransport // ErrUseLastResponse is an alias for http.ErrUseLastResponse. ErrUseLastResponse = http.ErrUseLastResponse // ErrServerClosed is an alias for http.ErrServerClosed. ErrServerClosed = http.ErrServerClosed // NoBody is an alias for http.NoBody. NoBody = http.NoBody )
Functions ¶
func Handle ¶ added in v2.205.0
Handle registers handler for pattern on mux and wraps it with OpenTelemetry instrumentation.
The handler is wrapped with telemetry.NewHandler using the provided pattern as the handler name. This is useful for consistent HTTP server span naming and handler metrics attribution.
func HandleFunc ¶ added in v2.205.0
func HandleFunc(mux *ServeMux, pattern string, handler http.HandlerFunc)
HandleFunc registers handler for pattern on mux and wraps it with OpenTelemetry instrumentation.
This helper ensures that handlers registered via this package are consistently instrumented by wrapping them with telemetry.NewHandler before registration.
func NewClient ¶ added in v2.205.0
NewClient constructs an HTTP client with OpenTelemetry instrumentation and a request timeout.
The returned client wraps the provided RoundTripper with a telemetry transport and installs an httptrace-based client trace derived from the request context. This enables client-side spans and timing events to be captured by the configured OpenTelemetry instrumentation.
The provided timeout is assigned to http.Client.Timeout (total time limit for requests, including connection time, redirects, and reading the response body).
func ParseServiceMethod ¶ added in v2.189.0
ParseServiceMethod derives a logical "service" and "method" name from an HTTP request.
This helper is intended for consistent telemetry naming. It attempts to derive names from the request path when it follows the conventional go-service route shape:
/<service>/<method>
If the request path matches that shape (as determined by net/grpc/strings.SplitServiceMethod), ParseServiceMethod returns the extracted service/method pair.
Otherwise it falls back to:
- method: lower-cased HTTP method (e.g. "get", "post")
- service: a best-effort name derived from the path:
- "root" when the path is empty or "/"
- otherwise the path without the leading "/" (e.g. "/health" -> "health")
func Pattern ¶ added in v2.61.0
Pattern constructs a route pattern of the form "/<name><pattern>".
This helper is used to namespace routes by service name so different services can share a router/mux without colliding, and so route names are consistent across telemetry, server registration, and tests.
Example:
Pattern(name, "/debug/pprof/") // -> "/my-service/debug/pprof/"
func Protocols ¶
Protocols constructs an http.Protocols value enabling the HTTP protocols supported by go-service.
The returned configuration enables:
- HTTP/1.1
- HTTP/2 (when negotiated, typically over TLS)
- h2c (unencrypted HTTP/2)
This helper is used by go-service HTTP server and transport construction to consistently enable the same protocol set across servers and clients.
func StatusText ¶ added in v2.21.0
StatusText returns the standard HTTP status text for the given status code.
This is a thin wrapper around net/http.StatusText.
func Transport ¶
Transport constructs a tuned *http.Transport with reasonable defaults and an optional TLS config.
This helper is intended for services and clients that want consistent HTTP transport behavior without having to re-specify common timeouts and connection pool limits.
Defaults applied:
- Proxy: http.ProxyFromEnvironment
- ForceAttemptHTTP2: true (enables HTTP/2 where supported by the server and TLS config)
- Dialer: 1m connect timeout, 30s TCP keepalive
- Connection pool limits: 100 max total idle, 100 max per host, 100 max conns per host
- Timeouts: 90s idle conn timeout, 10s TLS handshake timeout, 1s expect-continue timeout
- Protocols: set via Protocols() (go-service HTTP protocol configuration)
TLS behavior:
- If cfg is non-nil it is assigned to Transport.TLSClientConfig.
- If cfg is nil, the standard library defaults apply.
Types ¶
type Client ¶ added in v2.21.0
Client is an alias for net/http.Client.
It is provided so go-service code can depend on a consistent import path while preserving standard library semantics.
type Handler ¶ added in v2.8.0
Handler is an alias for net/http.Handler.
It is provided so go-service code can depend on a consistent import path while preserving standard library semantics.
func MaxBytesHandler ¶ added in v2.331.0
MaxBytesHandler wraps h so inbound request bodies are limited to n bytes.
This is a thin wrapper around net/http.MaxBytesHandler.
type HandlerFunc ¶ added in v2.22.0
type HandlerFunc = http.HandlerFunc
HandlerFunc is an alias for net/http.HandlerFunc.
It is provided so go-service code can depend on a consistent import path while preserving standard library semantics.
type Header ¶ added in v2.21.0
Header is an alias for net/http.Header.
It is provided so go-service code can depend on a consistent import path while preserving standard library semantics.
type MaxBytesError ¶ added in v2.331.0
type MaxBytesError = http.MaxBytesError
MaxBytesError is an alias for net/http.MaxBytesError.
It is returned when MaxBytesReader or MaxBytesHandler observes an inbound request body exceeding the configured byte limit.
type Request ¶ added in v2.21.0
Request is an alias for net/http.Request.
It is provided so go-service code can depend on a consistent import path while preserving standard library semantics.
func NewRequestWithContext ¶ added in v2.21.0
func NewRequestWithContext(ctx context.Context, method, url string, body io.Reader) (*Request, error)
NewRequestWithContext constructs a new outgoing HTTP request with ctx.
This is a thin wrapper around net/http.NewRequestWithContext. The returned request is canceled when ctx is canceled.
type Response ¶ added in v2.21.0
Response is an alias for net/http.Response.
It is provided so go-service code can depend on a consistent import path while preserving standard library semantics.
type ResponseWriter ¶ added in v2.22.0
type ResponseWriter = http.ResponseWriter
ResponseWriter is an alias for net/http.ResponseWriter.
It is provided so go-service code can depend on a consistent import path while preserving standard library semantics.
type RoundTripper ¶ added in v2.21.0
type RoundTripper = http.RoundTripper
RoundTripper is an alias for net/http.RoundTripper.
It is provided so go-service code can depend on a consistent import path while preserving standard library semantics.
type ServeMux ¶ added in v2.5.0
ServeMux is an alias for net/http.ServeMux.
It is provided so go-service code can depend on a consistent import path while preserving standard library semantics.
func NewServeMux ¶ added in v2.7.0
func NewServeMux() *ServeMux
NewServeMux constructs a new HTTP request multiplexer.
This is a thin wrapper around net/http.NewServeMux.
type Server ¶
Server is an alias for net/http.Server.
It is provided so go-service code can depend on a consistent import path while preserving standard library semantics.
func NewServer ¶
NewServer constructs an HTTP server with common timeout defaults and supported protocol settings.
Timeouts are derived from options first (if present) and fall back to the provided timeout value:
- read_timeout
- write_timeout
- idle_timeout
- read_header_timeout
Protocols are configured via Protocols().
Note: options.Duration uses MustParseDuration under the hood; invalid option values will panic at server construction time.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package client provides a content-aware HTTP client wrapper used by go-service.
|
Package client provides a content-aware HTTP client wrapper used by go-service. |
|
Package config contains HTTP transport configuration types used by go-service.
|
Package config contains HTTP transport configuration types used by go-service. |
|
Package content provides HTTP content negotiation helpers used by go-service.
|
Package content provides HTTP content negotiation helpers used by go-service. |
|
Package errors provides HTTP-specific error helpers for go-service.
|
Package errors provides HTTP-specific error helpers for go-service. |
|
Package meta provides HTTP-specific context metadata helpers and middleware for go-service.
|
Package meta provides HTTP-specific context metadata helpers and middleware for go-service. |
|
Package mvc provides a small MVC-style HTML rendering layer for go-service HTTP servers.
|
Package mvc provides a small MVC-style HTML rendering layer for go-service HTTP servers. |
|
Package rest provides REST-style HTTP handler registration and client helpers for go-service.
|
Package rest provides REST-style HTTP handler registration and client helpers for go-service. |
|
Package rpc provides RPC-style HTTP handler registration and client helpers for go-service.
|
Package rpc provides RPC-style HTTP handler registration and client helpers for go-service. |
|
Package server provides HTTP server adapters and lifecycle wiring for go-service.
|
Package server provides HTTP server adapters and lifecycle wiring for go-service. |
|
Package status provides helpers for working with HTTP status codes and status errors in go-service.
|
Package status provides helpers for working with HTTP status codes and status errors in go-service. |
|
Package strings provides HTTP-specific string helpers for net-layer middleware.
|
Package strings provides HTTP-specific string helpers for net-layer middleware. |
|
Package telemetry provides minimal, stable helpers for wiring OpenTelemetry instrumentation into net/http clients and servers.
|
Package telemetry provides minimal, stable helpers for wiring OpenTelemetry instrumentation into net/http clients and servers. |