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 ¶
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 ¶
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 ¶
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)