hmiddleware

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package hmiddleware provides common HTTP middleware.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ACMEValidationMiddleware added in v0.0.4

func ACMEValidationMiddleware(validationURL *url.URL) func(http.Handler) http.Handler

ACMEValidationMiddleware implements the HTTP01 based redirect protocol specific to heroku ACM.

func CORS

func CORS(next http.Handler) http.Handler

CORS adds Cross-Origin Resource Sharing headers to all outgoing requests. This is known as something that is kind of hard to get right. See docs at https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS for more information.

Example
r := chi.NewRouter()
r.Use(CORS)

// OR

var h http.Handler

//nolint: gosec
if err := http.ListenAndServe(":"+os.Getenv("PORT"), CORS(h)); err != nil {
	log.Fatal(err)
}
Output:

func DisableKeepalive

func DisableKeepalive(next http.Handler) http.Handler

DisableKeepalive instructs the Go HTTP stack to close the incoming HTTP connection once all requests processed by this middleware are complete.

Example
r := chi.NewRouter()
r.Use(DisableKeepalive)

// OR

var h http.Handler

//nolint: gosec
if err := http.ListenAndServe(":"+os.Getenv("PORT"), DisableKeepalive(h)); err != nil {
	log.Fatal(err)
}
Output:

func EnsureTLS

func EnsureTLS(next http.Handler) http.Handler

EnsureTLS ensures all incoming requests identify as having been proxied via https from the upstream reverse proxy. The way that this uses to check relies on the `X-Forwarded-Proto` header which is not defined by any formal standard. For more information on this header, see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto.

Example
r := chi.NewRouter()
r.Use(EnsureTLS)

// OR

var h http.Handler

//nolint: gosec
if err := http.ListenAndServe(":"+os.Getenv("PORT"), EnsureTLS(h)); err != nil {
	log.Fatal(err)
}
Output:

func PostRequestLogger added in v0.0.16

func PostRequestLogger(l logrus.FieldLogger) func(next http.Handler) http.Handler

PostRequestLogger is a middleware for the github.com/sirupsen/logrus to log requests. It logs things similar to heroku logs and adds remote_addr and user_agent. If you are using Chi, consider using the RequestLogger middleware instead.

func PreRequestLogger added in v0.0.16

func PreRequestLogger(l logrus.FieldLogger) func(next http.Handler) http.Handler

PreRequestLogger is a middleware for the github.com/sirupsen/logrus to log requests. It logs things similar to heroku logs and adds remote_addr and user_agent. If you are using Chi, consider using the RequestLogger middleware instead.

func RequestID added in v0.0.17

func RequestID(next http.Handler) http.Handler

func Tags added in v0.0.20

func Tags(next http.Handler) http.Handler

Tags provides a middleware function for seeding the context with a per-request tags that are eventually retrieved and logged by the StructuredLogger

Example
r := chi.NewRouter()
r.Use(Tags)
logger := logrus.StandardLogger()
l := &StructuredLogger{
	Logger: logger,
}
r.Use(middleware.RequestLogger(l))

r.Get("/", func(w http.ResponseWriter, r *http.Request) {
	// foo=bar will show up in the HTTP request logs generated by StructuredLogger
	tags.Extract(r.Context()).Set("foo", "bar")
	w.WriteHeader(http.StatusOK)
})
Output:

Types

type StructuredLogger added in v0.0.17

type StructuredLogger struct {
	Logger logrus.FieldLogger
}

StructuredLogger implements the LogFormatter interface from Chi. LogFormatter initiates the beginning of a new LogEntry per request. See https://github.com/go-chi/chi/blob/708d187cdc2beff37b6835250dd574f395ebaa03/middleware/logger.go#L54 For an example of how to use this middleware to combine Logrus logging with the Chi router, see https://github.com/go-chi/chi/blob/cca4135d8dddff765463feaf1118047a9e506b4a/_examples/logging/main.go#L2

func (*StructuredLogger) NewLogEntry added in v0.0.17

func (l *StructuredLogger) NewLogEntry(r *http.Request) middleware.LogEntry

NewLogEntry creates a new LogEntry at the start of a request.

type StructuredLoggerEntry added in v0.0.17

type StructuredLoggerEntry struct {
	Logger logrus.FieldLogger
	// contains filtered or unexported fields
}

StructuredLoggerEntry implements the LogEntry interface from Chi. LogEntry records the final log when a request completes. See https://github.com/go-chi/chi/blob/708d187cdc2beff37b6835250dd574f395ebaa03/middleware/logger.go#L60 For an example of how to use this middleware to combine Logrus logging with the Chi router, see https://github.com/go-chi/chi/blob/cca4135d8dddff765463feaf1118047a9e506b4a/_examples/logging/main.go#L2

func (*StructuredLoggerEntry) Panic added in v0.0.17

func (l *StructuredLoggerEntry) Panic(v interface{}, stack []byte)

Panic is called by Chi's Recoverer middleware. See https://github.com/go-chi/chi/blob/baf4ef5b139e284b297573d89daf587457153aa3/middleware/recoverer.go

func (*StructuredLoggerEntry) Write added in v0.0.17

func (l *StructuredLoggerEntry) Write(status, bytes int, header http.Header, elapsed time.Duration, extra interface{})

Write creates a new LogEntry at the end of a request.

Directories

Path Synopsis
Package httpmetrics provides middleware for collecting metrics about http servers.
Package httpmetrics provides middleware for collecting metrics about http servers.

Jump to

Keyboard shortcuts

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