http

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2021 License: MIT Imports: 19 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenAndServe

func ListenAndServe(ctx context.Context, addr string, srv *http.Server) error

ListenAndServe serves an http server over TCP

func MethodAndPathCleanID

func MethodAndPathCleanID(r *http.Request) string

MethodAndPathCleanID replace string values that look like ids (uuids and int) with "*"

func New

func New(cfg *Config) (*http.Server, error)

New creates a new http server

Example:

srv, _ := server.New(&server.Config{
 Addr: ":8080",
 Handler: http.DefaultServeMux,
 Options: []server.Option{
   server.WithLogging("my-server"),
   server.WithMetrics("my-server"),
   server.WithRecovery(),
   server.WithTracing("opentracing-server:6831", "my-server"),
 }
})

srv.ListenAndServe(context.Background())

func PathWithCleanID added in v0.4.0

func PathWithCleanID(r *http.Request) string

PathWithCleanID replace string values that look like ids (uuids and int) with "*"

Types

type Config

type Config struct {
	Addr    string
	Handler http.Handler
	Options []Option
}

Config contains the http servers config options

type Option

type Option interface {
	WrapHandler(handler http.Handler) http.Handler
}

Option is the interface for all server options defined in this package

func WithCORS

func WithCORS(allowedOrigins, allowedMethods, allowedHeaders []string, allowCredentials bool) Option

WithCORS configures CORS on the webserver

func WithCORSWideOpen

func WithCORSWideOpen() Option

WithCORSWideOpen allows requests from all origins with all methods and all headers/cookies/credentials allowed.

func WithGRPC

func WithGRPC(srv *grpc.Server) Option

WithGRPC configures the webserver to serve grpc-web requests as specified in https://github.com/improbable-eng/grpc-web

func WithLogging

func WithLogging(app string) Option

WithLogging configures a logrus middleware for that server

func WithMetrics

func WithMetrics(app string, opNameFunc func(r *http.Request) string) Option

WithMetrics configures metrics collection

func WithRecovery

func WithRecovery(writer io.Writer, printStack bool) Option

WithRecovery configures panic recovery for that server

func WithTracing

func WithTracing(server, app string, tags map[string]string, opNameFunc func(r *http.Request) string) Option

WithTracing configures tracing for that server; if configuration fails, WithTracing will panic

Jump to

Keyboard shortcuts

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