httpkit

package module
v0.0.0-...-3e1a49f Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

README

HTTP Kit for Go Backend Engineers

This kit is my personal utility, and I have used it in both my professional and hobby projects. I hope you will find it useful as well. I would be happy to receive your feedback and suggestions.

Features

  • Modifies the httprouter to allow the handler to return errors.
  • HTTP Server with Graceful Shutdown.
  • Helpers for creating middleware for both net/http and the modified httprouter.
  • Helpers for request decoding and response encoding.
  • A middleware for recording logs. This middleware can be used to implement the Canonical Log Line.

TODOS

  • Add a Content Negotiation Middleware
  • Add more encoders and decoders.
  • Add more common middlewares.
  • Add Open Telemetry support.

Examples

Creating a Server with Graceful Shutdown
package main

import (
	"fmt"
	"github.com/josestg/httpkit"
	"log/slog"
	"net/http"
	"os"
	"syscall"
	"time"
)

func main() {
	log := slog.Default()

	mux := httpkit.NewServeMux(
		// Register an error handler that will be called when there is still unresolved error after all the middlewares
		// and the handler have been called. We can think this as the last hope when all else fails.
		httpkit.Opts.LastResortErrorHandler(LastResortErrorHandler(log)),

		// Set the global middlewares for the mux. These middlewares will be applied to all the handlers that are
		// registered to this mux.
		//
		// For middlewares that are applied to a specific handler, `mux.Route` takes variadic `httpkit.MuxMiddleware`
		// as the last argument, which will be applied to that handler only.
		httpkit.Opts.Middleware(GlobalMiddleware()),

		// And more options are available under `httpkit.Opts` namespace.
	)

	// Using `Route` instead of `HandleFunc` will be more convenient when working with Swagger docs because the swagger
	// docs and the route definition will be close to each other. Which will make it easier to keep them in sync.
	mux.Route(httpkit.Route{
		Method: http.MethodPost,
		Path:   "/",
		Handler: func(w http.ResponseWriter, r *http.Request) error {
			data := struct {
				Message string `json:"message"`
			}{}

			if err := httpkit.ReadJSON(r.Body, &data); err != nil {
				return fmt.Errorf("could not read json: %w", err)
			}

			// do something with the data.
			data.Message += " --updated"
			return httpkit.WriteJSON(w, data, http.StatusOK)
		},
	})

	// this middleware will be applied to the router itself. Meaning that it will be called before and after
	// the router calls the handler.
	mid := httpkit.ReduceNetMiddleware(
		// record both request and response body, along with latency and status code.
		httpkit.LogEntryRecorder,

		// another example of a middleware that will be applied to the router itself.
		func(next http.Handler) http.Handler {
			return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				log.Info("before router")
				next.ServeHTTP(w, r)
				entry, ok := httpkit.GetLogEntry(w)
				if ok {
					log.Info("after router", "method", r.Method, "path", r.URL.Path,
						"status", entry.StatusCode, "latency", time.Duration(entry.RespondedAt-entry.RequestedAt))
				}
			})
		},
	)

	// apply the middleware to the router.
	handler := mid.Then(mux)

	srv := http.Server{
		Addr:         "0.0.0.0:8080",
		Handler:      handler,
		ReadTimeout:  5 * time.Second,
		WriteTimeout: 10 * time.Second,
	}

	// create a graceful server runner.
	run := httpkit.NewGracefulRunner(
		&srv,
		httpkit.RunOpts.WaitTimeout(10*time.Second),              // wait for 10 seconds before force shutdown.
		httpkit.RunOpts.Signals(syscall.SIGINT, syscall.SIGTERM), // listen to SIGINT and SIGTERM signals.
		httpkit.RunOpts.EventListener(func(evt httpkit.RunEvent, data string) { // listen to events and log them.
			switch evt {
			default:
				log.Info(data)
			case httpkit.RunEventAddr:
				log.Info("http server listening", "addr", data)
			case httpkit.RunEventSignal:
				log.Info("http server received shutdown signal", "signal", data)
			}
		}),
	)

	if err := run.ListenAndServe(); err != nil {
		log.Error("http server error", "error", err)
		os.Exit(1)
	}
}

func LastResortErrorHandler(log *slog.Logger) httpkit.LastResortErrorHandler {
	return func(w http.ResponseWriter, r *http.Request, err error) {
		log.Error("last resort error handler", "error", err)
		data := map[string]string{
			"message": http.StatusText(http.StatusInternalServerError),
		}
		if wErr := httpkit.WriteJSON(w, data, http.StatusInternalServerError); wErr != nil {
			log.Error("could not write json", "unresolved_error", err, "write_error", wErr)
		}
	}
}

func GlobalMiddleware() httpkit.MuxMiddleware {
	return httpkit.ReduceMuxMiddleware(
		globalMiddlewareOne,
		globalMiddlewareTwo,
	)
}

func globalMiddlewareOne(next httpkit.Handler) httpkit.Handler {
	return httpkit.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
		// do something before calling the next handler.
		err := next.ServeHTTP(w, r)
		// do something after calling the next handler.
		return err
	})
}

func globalMiddlewareTwo(next httpkit.Handler) httpkit.Handler {
	return httpkit.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
		// do something before calling the next handler.
		err := next.ServeHTTP(w, r)
		// do something after calling the next handler.
		return err
	})
}

Documentation

Index

Constants

View Source
const DefaultHandler defaultHandlerNamespace = 0

DefaultHandler is a namespace for accessing default handlers.

View Source
const Opts muxOptionNamespace = 0

Opts is a namespace for accessing options.

View Source
const RunOpts runOptionNamespace = 0

RunOpts is the namespace for accessing the Option for customizing the GracefulRunner.

Variables

This section is empty.

Functions

func LogEntryRecorder

func LogEntryRecorder(next http.Handler) http.Handler

LogEntryRecorder is a middleware that records the request and response on demand. The request body is not recorded until it is read by the handler. And the response body is not recorded until it is written by the handler.

The recorded entry can be retrieved by calling GetLogEntry(w http.ResponseWriter).

func ReadJSON

func ReadJSON(r io.Reader, data any) error

ReadJSON reads json from the reader and decodes it to the data. By default, it disallows unknown fields.

func ResolveError

func ResolveError(err error) error

ResolveError marks the error as resolved.

func WriteJSON

func WriteJSON(w http.ResponseWriter, data any, code int) error

WriteJSON writes the data to the response writer as JSON. By default, it sets the content type to application/json; charset=utf-8.

Types

type GracefulRunner

type GracefulRunner struct {
	Runner
	// contains filtered or unexported fields
}

GracefulRunner is a wrapper of http.Server that can be shutdown gracefully.

func NewGracefulRunner

func NewGracefulRunner(server Runner, opts ...RunOption) *GracefulRunner

NewGracefulRunner wraps a Server with graceful shutdown capability. It will listen to SIGINT and SIGTERM signals to initiate shutdown and wait for all active connections to be closed. If still active connections after wait timeout exceeded, it will force close the server. The default wait timeout is 5 seconds.

func (*GracefulRunner) ListenAndServe

func (s *GracefulRunner) ListenAndServe() error

ListenAndServe starts listening and serving the server gracefully.

type Handler

type Handler interface {
	// ServeHTTP is just like http.Handler.ServeHTTP, but it returns an error.
	ServeHTTP(http.ResponseWriter, *http.Request) error
}

Handler is modified version of http.Handler.

type HandlerFunc

type HandlerFunc func(http.ResponseWriter, *http.Request) error

HandlerFunc is a function that implements Handler. It is used to create a Handler from an ordinary function.

func (HandlerFunc) ServeHTTP

func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) error

ServeHTTP implements Handler.

type LastResortErrorHandler

type LastResortErrorHandler func(http.ResponseWriter, *http.Request, error)

LastResortErrorHandler is the error handler that is called if after all middlewares, there is still an error occurs.

type LogBodyReader

type LogBodyReader interface {
	io.ReaderFrom
	Len() int
	Bytes() []byte
}

LogBodyReader knows how to read the request or response body.

type LogEntry

type LogEntry struct {
	StatusCode  int
	RespondedAt int64
	RequestedAt int64

	// DiscardReqBody and DiscardResBody are used to indicate whether the request
	// or response body should be discarded. By default, both are false.
	DiscardReqBody bool
	DiscardResBody bool
	// contains filtered or unexported fields
}

LogEntry contains recorded request and response information.

func GetLogEntry

func GetLogEntry(w http.ResponseWriter) (*LogEntry, bool)

GetLogEntry gets the recorded LogEntry from the given http.ResponseWriter by unwrapping the http.ResponseWriter, if not found, it returns false.

func (*LogEntry) ReqBody

func (l *LogEntry) ReqBody() LogBodyReader

ReqBody returns the request body.

func (*LogEntry) ResBody

func (l *LogEntry) ResBody() LogBodyReader

ResBody returns the response body.

type MuxConfig

type MuxConfig struct {
	// Enables automatic redirection if the current route can't be matched but a
	// handler for the path with (without) the trailing slash exists.
	// For example if /foo/ is requested but a route only exists for /foo, the
	// client is redirected to /foo with http status code 301 for GET requests
	// and 307 for all other request methods.
	RedirectTrailingSlash bool

	// If enabled, the router tries to fix the current request path, if no
	// handle is registered for it.
	// First superfluous path elements like ../ or // are removed.
	// Afterward the router does a case-insensitive lookup of the cleaned path.
	// If a handle can be found for this route, the router makes a redirection
	// to the corrected path with status code 301 for GET requests and 307 for
	// all other request methods.
	// For example /FOO and /..//Foo could be redirected to /foo.
	// RedirectTrailingSlash is independent of this option.
	RedirectFixedPath bool

	// If enabled, the router checks if another method is allowed for the
	// current route, if the current request can not be routed.
	// If this is the case, the request is answered with 'Method Not Allowed'
	// and HTTP status code 405.
	// If no other Method is allowed, the request is delegated to the NotFound
	// handler.
	HandleMethodNotAllowed bool

	// If enabled, the router automatically replies to OPTIONS requests.
	// Custom OPTIONS handlers take priority over automatic replies.
	HandleOPTIONS bool

	// An optional http.Handler that is called on automatic OPTIONS requests.
	// The handler is only called if HandleOPTIONS is true and no OPTIONS
	// handler for the specific path was set.
	// The "Allowed" header is set before calling the handler.
	GlobalOPTIONS http.Handler

	// Configurable http.Handler which is called when no matching route is
	// found.
	NotFound http.Handler

	// Configurable http.Handler which is called when a request
	// cannot be routed and HandleMethodNotAllowed is true.
	// The "Allow" header with allowed request methods is set before the handler
	// is called.
	MethodNotAllowed http.Handler

	// Function to handle panics recovered from http handlers.
	// It should be used to generate an error page and return the http error code
	// 500 (Internal Server Error).
	// The handler can be used to keep your server from crashing because of
	// unrecoverable panics.
	PanicHandler func(http.ResponseWriter, *http.Request, any)

	// LastResortErrorHandler is the error handler that is called if after all middlewares,
	// there is still an error occurs. This handler is used to catch errors that are not handled by the middlewares.
	//
	// This handler is not part of the httprouter.Router, it is used by the ServeMux.
	LastResortErrorHandler LastResortErrorHandler
}

MuxConfig is the configuration for the underlying httprouter.Router.

type MuxMiddleware

type MuxMiddleware func(Handler) Handler

MuxMiddleware is a middleware that applies to route handler. It is used to create a middleware that is compatible with httpkit.Handler.

func ReduceMuxMiddleware

func ReduceMuxMiddleware(middlewares ...MuxMiddleware) MuxMiddleware

ReduceMuxMiddleware reduces a set of mux middlewares into a single mux middleware. For example:

ReduceMuxMiddleware(m1, m2, m3).Then(h)
will be equivalent to:
m1(m2(m3(h)))

func (MuxMiddleware) Then

func (m MuxMiddleware) Then(h Handler) Handler

Then is a syntactic sugar for applying the middleware to the handler. Instead of: m(h), you can write: m.Then(h).

type MuxOption

type MuxOption func(mux *ServeMux)

MuxOption is an option for customizing the ServeMux.

type NetMiddleware

type NetMiddleware func(http.Handler) http.Handler

NetMiddleware is a middleware that applies to the root handler. It is used to create a middleware that is compatible with net/http.

func ReduceNetMiddleware

func ReduceNetMiddleware(middlewares ...NetMiddleware) NetMiddleware

ReduceNetMiddleware reduces a set of net middlewares into a single net middleware. For example:

ReduceNetMiddleware(m1, m2, m3).Then(h)
will be equivalent to:
m1(m2(m3(h)))

func (NetMiddleware) Then

Then is a syntactic sugar for applying the middleware to the handler. Instead of: m(h), you can write: m.Then(h).

type Param

type Param = httprouter.Param

Param is alias of httprouter.Param.

type Params

type Params = httprouter.Params

Params is alias of httprouter.Params.

func PathParams

func PathParams(r *http.Request) Params

PathParams gets the path variables from the request.

type ResolvedError

type ResolvedError struct {
	Err error // the original error.
}

ResolvedError is an error that has been resolved. When an error is resolved, it means that the error has been mapped to an HTTP response and the no error will be handled by the LastResortErrorHandler.

func (*ResolvedError) Error

func (e *ResolvedError) Error() string

Error implements error interface.

type Route

type Route struct {
	Method  string
	Path    string
	Handler HandlerFunc
}

Route is used to register a new handler to the ServeMux.

type RunConfig

type RunConfig struct {
	Port            int           // Port to listen to.
	ShutdownTimeout time.Duration // Maximum duration for waiting all active connections to be closed before force close.

	// RequestReadTimeout and RequestWriteTimeout are timeouts for http.Server.
	// These timeouts are used to limit the time spent reading or writing the request body.
	// see: https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts.
	RequestReadTimeout  time.Duration // Maximum duration for reading the entire request, including the body.
	RequestWriteTimeout time.Duration // Maximum duration before timing out writes of the response.
}

RunConfig is a configuration for creating a http Runner.

type RunEvent

type RunEvent uint8

RunEvent is a flag to differentiate run events.

const (
	RunEventInfo   RunEvent = iota // for telling the data is an info.
	RunEventAddr                   // for telling the data is a server address.
	RunEventError                  // for telling the data is an error.
	RunEventSignal                 // for telling the data is a signal received.
)

Sets of run events.

func (RunEvent) String

func (e RunEvent) String() string

String returns the string representation of RunEvent for logging readability.

type RunOption

type RunOption func(*GracefulRunner)

RunOption is the option for customizing the GracefulRunner.

type Runner

type Runner interface {
	// ListenAndServe starts listening and serving the server.
	// This method should block until shutdown signal received or failed to start.
	ListenAndServe() error

	// Shutdown gracefully shuts down the server, it will wait for all active connections to be closed.
	Shutdown(ctx context.Context) error

	// Close force closes the server.
	// Close is called when Shutdown timeout exceeded.
	Close() error
}

Runner is contract for server that can be started, shutdown gracefully and force closed if shutdown timeout exceeded.

type ServeMux

type ServeMux struct {
	// contains filtered or unexported fields
}

ServeMux is a wrapper of httprouter.Router with modified Handler. Instead of http.Handler, it uses Handler, which returns an error. This modification is used to simplify logic for creating a centralized error handler and logging.

The ServeMux also supports MuxMiddleware, which is a middleware that wraps the Handler for all routes. Since the ServeMux also implements http.Handler, the NetMiddleware can be used to create middleware that will be executed before the ServeMux middleware.

The ServeMux only exposes 3 methods: Route, Handle, and ServeHTTP, which are more simple than the original.

func NewServeMux

func NewServeMux(opts ...MuxOption) *ServeMux

NewServeMux creates a new ServeMux with given options. If no option is given, the Default option is applied.

func (*ServeMux) Handle

func (mux *ServeMux) Handle(method, path string, handler Handler)

Handle registers a new request handler with the given method and path.

func (*ServeMux) Route

func (mux *ServeMux) Route(r Route, mid ...MuxMiddleware)

Route is a syntactic sugar for Handle(method, path, handler) by using Route struct. This route also accepts variadic MuxMiddleware, which is applied to the route handler.

func (*ServeMux) ServeHTTP

func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP satisfies http.Handler.

Jump to

Keyboard shortcuts

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