Documentation
¶
Overview ¶
Package httpserver provides a standard way of writing an HTTP server. It supports:
- Creating an HTTP Server listening on a static or dynamic port
- Running a Prometheus metrics server
- Creating an HTTP Server with one or more HTTP handlers
- Recording Prometheus metrics for latency (average or quantiles) & number of requests.
Deprecated: relevant parts have moved to github.com/clambin/go-common/http
Example ¶
package main
import (
"errors"
"github.com/clambin/go-common/httpserver"
"github.com/clambin/go-common/httpserver/middleware"
"github.com/prometheus/client_golang/prometheus"
"net/http"
)
func main() {
s, err := httpserver.New(
httpserver.WithAddr(":8080"),
httpserver.WithPrometheus(""),
httpserver.WithMetrics(middleware.PrometheusMetricsOptions{Application: "example", MetricsType: middleware.Summary}),
httpserver.WithHandlers([]httpserver.Handler{{
Path: "/",
Methods: []string{http.MethodGet},
Handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("Hello world"))
}),
}}),
)
if err != nil {
panic(err)
}
prometheus.MustRegister(s)
err = s.Serve()
if !errors.Is(err, http.ErrServerClosed) {
panic(err)
}
}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MethodFilter ¶ added in v0.4.0
MethodFilter only passes on the request if the http request's methos matches once of the methods. If no methods are provided, MethodFilter defaults to http.MethodGet.
deprecated: no longer needed as of Go 1.22
Types ¶
type Handler ¶
type Handler struct {
// Path of the endpoint (e.g. "/health"). Can be any path that's valid for gorilla/mux router's Path() method.
Path string
// Methods that the handler should support. If empty, defaults to http.MethodGet.
Methods []string
// Handler that implements the endpoint.
Handler http.Handler
}
Handler contains a path to be registered in the Server's HTTP server
type Option ¶
type Option func(*Server)
Option specified configuration options for Server
func WithAddr ¶ added in v0.5.0
WithAddr specifies the Server's listening address. If the port is zero, Server will listen on a random port. Use GetPort() to determine the actual listening port
func WithHandlers ¶
WithHandlers adds the specified handlers to the server
func WithMetrics ¶
func WithMetrics(metrics middleware.PrometheusMetricsOptions) Option
WithMetrics will collect the specified metrics to instrument the Server's Handlers.
func WithPrometheus ¶
WithPrometheus adds a Prometheus metrics endpoint to the server at the specified Path. Default path is "/metrics"
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server implements a configurable HTTP Server. See the different WithXXX structs for available options.
deprecated: no longer in use
func (*Server) Collect ¶ added in v0.2.0
func (s *Server) Collect(c chan<- prometheus.Metric)
Collect implements the prometheus.Collector interface
func (*Server) Describe ¶ added in v0.2.0
func (s *Server) Describe(descs chan<- *prometheus.Desc)
Describe implements the prometheus.Collector interface
func (*Server) Serve ¶ added in v0.2.0
Serve starts the HTTP server. When the server is shut down, it returns http.ErrServerClosed.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package middleware contains HTTP middleware.
|
Package middleware contains HTTP middleware. |