Categorygithub.com/stevecallear/strudel
modulepackage
1.2.0
Repository: https://github.com/stevecallear/strudel.git
Documentation: pkg.go.dev

# README

Strudel

Build Status codecov Go Report Card

Strudel provides structured error handling and logging middleware for use with Janice handlers. It uses the excellent Logrus, jsend and httpsnoop packages.

The package is intended to help reduce the initial middleware boilerplate when building an HTTP API. Realistically, error handing and logging requirements will change as an app develops, so the intention here is to simply provide drop in/out components that allow rapid prototyping.

Getting started

go get github.com/stevecallear/strudel

Example

package main

import (
	"net/http"

	"github.com/stevecallear/janice"
	"github.com/stevecallear/strudel"
)

func main() {
	chain := janice.New(strudel.ErrorHandling)

	mux := http.NewServeMux()
	mux.Handle("/", chain.Then(func(w http.ResponseWriter, r *http.Request) error {
		return strudel.NewError("resource not found").
			WithCode(http.StatusNotFound).
			WithField("resourceId", "abc123").
			WithLogField("sensitiveId", "cde456")
	}))

	h := janice.New(strudel.RequestTracking, strudel.Recovery, strudel.RequestLogging).Then(janice.Wrap(mux))
	http.ListenAndServe(":8080", h)
}

# Functions

ErrorHandling is an error handling middleware function.
NewError returns a new error.
Recovery is a panic recovery middleware function.
RequestLogging is a request logging middleware function.
RequestTracking is a request tracking middleware function.

# Variables

GetRequestID returns the id for the specified request.
Logger is the logger used for all middleware.

# Structs

No description provided by the author

# Type aliases

No description provided by the author