http

package
v0.0.0-...-095633c Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2024 License: Apache-2.0, Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package http provides easy access to Hcnet's best practices for building http servers. The primary method to use is `Serve`, which sets up an server that can support http/2 and can gracefully quit after receiving a SIGINT signal.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EqualFileSystems

func EqualFileSystems(fs, otherFS http.FileSystem, currentPath string) bool

EqualFileSystems traverses two http.FileSystem instances and returns true if they are equal

func GetChiRoutePattern

func GetChiRoutePattern(r *stdhttp.Request) string

GetChiRoutePattern returns the chi route pattern from the given request context. Author: https://github.com/rliebz From: https://github.com/go-chi/chi/issues/270#issuecomment-479184559 https://github.com/go-chi/chi/blob/master/LICENSE

func HeadersMiddleware

func HeadersMiddleware(headers stdhttp.Header, ignoredPrefixes ...string) func(next stdhttp.Handler) stdhttp.Handler

HeadersMiddleware sends headers

func LoggingMiddleware

func LoggingMiddleware(next stdhttp.Handler) stdhttp.Handler

LoggingMiddleware is a middleware that logs requests to the logger.

func LoggingMiddlewareWithOptions

func LoggingMiddlewareWithOptions(options Options) func(stdhttp.Handler) stdhttp.Handler

LoggingMiddlewareWithOptions is a middleware that logs requests to the logger. Requires an Options struct to accept additional information.

func NewAPIMux

func NewAPIMux(l *log.Entry) *chi.Mux

NewAPIMux returns a new server mux configured with the common defaults used for a web API in hcnet.

func NewMux

func NewMux(l *log.Entry) *chi.Mux

NewMux returns a new server mux configured with the common defaults used across all hcnet services.

func Run

func Run(conf Config)

Run starts an http server using the provided config struct.

This method configures the process to listen for termination signals (SIGINT and SIGTERM) to trigger a graceful shutdown by way of the graceful package (https://github.com/tylerb/graceful).

func SetLoggerMiddleware

func SetLoggerMiddleware(l *log.Entry) func(stdhttp.Handler) stdhttp.Handler

SetLogger is a middleware that sets a logger on the context.

func StripTrailingSlashMiddleware

func StripTrailingSlashMiddleware(ignoredPrefixes ...string) func(next stdhttp.Handler) stdhttp.Handler

StripTrailingSlashMiddleware strips trailing slash.

func XFFMiddleware

func XFFMiddleware(config XFFMiddlewareConfig) func(next stdhttp.Handler) stdhttp.Handler

XFFMiddleware is a middleware that replaces http.Request.RemoteAddr with a visitor value based on a given config:

  • If BehindCloudflare is true CF-Connecting-IP header is used.
  • If BehindAWSLoadBalancer is true the last value of X-Forwarded-For header is used.
  • If none of above is set the first value of X-Forwarded-For header is used. Note: it's easy to spoof the real IP address if the application is not behind a proxy that maintains a X-Forwarded-For header.

Please note that the new RemoteAddr value may not contain the port part!

Types

type Config

type Config struct {
	Handler             stdhttp.Handler
	ListenAddr          string
	TLS                 *config.TLS
	ShutdownGracePeriod time.Duration
	ReadTimeout         time.Duration
	WriteTimeout        time.Duration
	IdleTimeout         time.Duration
	TCPKeepAlive        time.Duration
	OnStarting          func()
	OnStopping          func()
	OnStopped           func()
}

Config represents the configuration of an http server that can be provided to `Run`.

type Options

type Options struct {
	ExtraHeaders []string
}

Options allow the middleware logger to accept additional information.

type SimpleHTTPClientInterface

type SimpleHTTPClientInterface interface {
	PostForm(url string, data url.Values) (*stdhttp.Response, error)
	Get(url string) (*stdhttp.Response, error)
}

SimpleHTTPClientInterface helps mocking http.Client in tests

type XFFMiddlewareConfig

type XFFMiddlewareConfig struct {
	BehindCloudflare      bool
	BehindAWSLoadBalancer bool
}

XFFMiddlewareConfig provides a configuration for XFFMiddleware.

Directories

Path Synopsis
Package httpauthz contains helper functions for parsing the 'Authorization' header in HTTP requests.
Package httpauthz contains helper functions for parsing the 'Authorization' header in HTTP requests.
Package httptest enhances the stdlib net/http/httptest package by integrating it with gopkg.in/gavv/httpexpect.v1, reducing the boilerplate needed for http tests.
Package httptest enhances the stdlib net/http/httptest package by integrating it with gopkg.in/gavv/httpexpect.v1, reducing the boilerplate needed for http tests.
Package mutil contains various functions that are helpful when writing http middleware.
Package mutil contains various functions that are helpful when writing http middleware.

Jump to

Keyboard shortcuts

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