httpserver

package module
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: MIT Imports: 9 Imported by: 1

README

httpserver

Test GoDoc

Basic instrumented HTTP server that I found myself writing over and over again.

Documentation

Authors

  • Christophe Lambin

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Documentation

Overview

Package httpserver provides a standard way of writing an HTTP server. It supports:

  1. Creating an HTTP Server listening on a static or dynamic port
  2. Running a Prometheus metrics server
  3. Creating an HTTP Server with one or more HTTP handlers
  4. 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)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MethodFilter added in v0.4.0

func MethodFilter(methods ...string) func(next http.Handler) http.Handler

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

func WithAddr(addr string) Option

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

func WithHandlers(handlers []Handler) Option

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

func WithPrometheus(path string) Option

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 New

func New(options ...Option) (*Server, error)

New returns a Server with the specified 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) GetPort

func (s *Server) GetPort() int

GetPort returns the HTTP Server's listening port

func (*Server) Serve added in v0.2.0

func (s *Server) Serve() error

Serve starts the HTTP server. When the server is shut down, it returns http.ErrServerClosed.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP calls the server's handler. Mainly intended to be used in unit tests without starting the underlying HTTP server.

func (*Server) Shutdown

func (s *Server) Shutdown(timeout time.Duration) error

Shutdown performs a graceful shutdown of the HTTP server

Directories

Path Synopsis
Package middleware contains HTTP middleware.
Package middleware contains HTTP middleware.

Jump to

Keyboard shortcuts

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