bttp

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2022 License: MIT Imports: 8 Imported by: 0

README

B(etter) (H)TTP

Better HTTP provides a handful of functions to reduce boilerplate code when developing RESTful Webservices. Functions for handling http responses and graceful shutdowns of http servers are included.

Take a look at the example to get an overview of its functionality.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EmptyData any

EmptyData is a placeholder that can be used inside the convenience function when no data should be responded.

Functions

func Handle

func Handle(f HandlerFunc) http.HandlerFunc

Handle translates a HandlerFunc into a http.HandleFunc.

func ListenGracefully

func ListenGracefully(srv *http.Server, opts ...ShutdownOpt) error

ListenGracefully starts a http.Server and handles the shutdown process, when a syscall.SIGINT or syscall.SIGTERM are sent to the process.

Types

type HandlerFunc

type HandlerFunc func(r *http.Request) Response

HandlerFunc is the custom function that can be translated into a http.HandlerFunc.

type Response

type Response struct {
	// The http status code that is used in response.
	StatusCode int
	// The headers that are used in the response.
	Headers map[string]string
	// The data that should be responded with.
	Data any
}

Response holds all information that is needed to translate into a http response.

func BadRequest

func BadRequest(data any) Response

BadRequest is a convenience function to generate a Response with a http.StatusBadRequest.

func Created

func Created(location string) Response

Created is a convenience function to generate a Response with a http.StatusCreated.

func DecodeBody

func DecodeBody(r *http.Request, v any) (bool, *Response)

DecodeBody takes a http.Request and parses it into a struct v. It returns a bool, which indicates if the parsing was successful and a Response. In case of a decoding error, the Response indicates a http.StatusBadRequest.

func InternalServerError

func InternalServerError(data any) Response

InternalServerError is a convenience function to generate a Response with a http.StatusInternalServerError.

func Ok

func Ok(data any) Response

Ok is a convenience function to generate a Response with a http.StatusOK.

type ShutdownConfig

type ShutdownConfig struct {
	// the timeout for performing a graceful shutdown.
	Timeout time.Duration
}

ShutdownConfig holds all variables needed in the shutdown process.

type ShutdownOpt

type ShutdownOpt func(c *ShutdownConfig)

ShutdownOpt is a modifier function. They can be used to modify a ShutdownConfig.

func SetTimeout

func SetTimeout(t time.Duration) ShutdownOpt

SetTimeout sets the timeout for performing a graceful shutdown.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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