malcolm

package module
v0.0.0-...-ce60a12 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2025 License: MIT Imports: 6 Imported by: 0

README

malcolm

Just some default middleware functions I tend to use a lot.

To use simply type

go get github.com/RAshkettle/malcolm

Performance

goarch: arm64
cpu: Apple M3
BenchmarkCommonHeaders-8     4423387        254.0 ns/op
BenchmarkNoSurf-8            1275955        875.4 ns/op
BenchmarkRecoverPanic-8     82381987         16.15 ns/op
PASS
ok   giithub.com/RAshkettle/malcolm 5.024s

Documentation

Overview

Package malcolm contains middleware that is commonly used in go http servers

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientError

func ClientError(w http.ResponseWriter, status int)

ClientError is a middleware function that will return an error for actions on the client side.

w: the http.ResponseWriter to write the error to.

status: the HTTP status code to return.

Example
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	ClientError(w, http.StatusNotFound)
})

http.ListenAndServe(":8080", mux)

func CommonHeaders

func CommonHeaders(next http.Handler) http.Handler

CommonHeaders is a middleware that sets common security headers for all HTTP responses.

next: the next http.Handler in the chain.

Returns a new http.Handler with common security headers set.

Example
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Hello, World!"))
})

// To use the CommonHeaders middleware, you can wrap your handler with it.
http.ListenAndServe(":8080", CommonHeaders(mux))

func NoSurf

func NoSurf(next http.Handler) http.Handler

NoSurf is a middleware that provides CSRF protection using the nosurf package.

next: the next http.Handler in the chain.

Returns a new http.Handler with CSRF protection.

Example
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Hello, World!"))
})

// To use the NoSurf middleware, you can wrap your handler with it.
http.ListenAndServe(":8080", NoSurf(mux))

Types

type MiddleWare

type MiddleWare struct {
	Logger *slog.Logger
}

func NewMiddleWare

func NewMiddleWare(logger *slog.Logger) *MiddleWare

NewMiddleWare creates a new MiddleWare struct.

logger: a slog.Logger to use for logging. If nil, a default logger is used.

Returns a new MiddleWare struct.

Example
logger := slog.New(slog.NewJSONHandler(nil, nil))
m := NewMiddleWare(logger)

if m.logger == nil {
	fmt.Println("logger is nil")
}

func (*MiddleWare) RecoverPanic

func (m *MiddleWare) RecoverPanic(next http.Handler) http.Handler

RecoverPanic is a middleware that recovers from any panics and writes a 500 Internal Server Error response.

next: the next http.Handler in the chain.

Returns a new http.Handler that recovers from panics.

Example
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	panic("test panic")
})

m := NewMiddleWare(nil)

// To use the RecoverPanic middleware, you can wrap your handler with it.
http.ListenAndServe(":8080", m.RecoverPanic(mux))

func (*MiddleWare) ServerError

func (m *MiddleWare) ServerError(w http.ResponseWriter, r *http.Request, err error)

ServerError is a middleware function that will log and return an error if it occurs on the server side.

w: the http.ResponseWriter to write the error to.

r: the http.Request that caused the error.

err: the error that occurred.

Example
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	m := NewMiddleWare(nil)
	m.ServerError(w, r, fmt.Errorf("test error"))
})

http.ListenAndServe(":8080", mux)

Jump to

Keyboard shortcuts

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