Categorygithub.com/zerodha/fastglue
modulepackage
1.8.1
Repository: https://github.com/zerodha/fastglue.git
Documentation: pkg.go.dev

# README

fastglue

Overview Go Reference Zerodha Tech

fastglue is an opinionated, bare bones wrapper that glues together fasthttp and fasthttprouter to act as a micro HTTP framework. It helps eliminate boilerplate that would otherwise be required when using these two libraries to write HTTP servers. It enables:

  • Performance benefits of fasthttp + fasthttprouter.
  • Pre/post middleware hooks on HTTP handlers.
  • Simple middlewares for validating (existence, length range) of params in HTTP requests.
  • Functions for unmarshalling request payloads (Form encoding, JSON, XML) into arbitrary structs.
  • Shortcut functions for registering handlers, GET(), POST() etc.
  • Shortcut for fasthttp listening on TCP and Unix sockets.
  • Shortcut for graceful shutdown hook on the fasthttp server.
  • Opinionated JSON API response and error structures.
  • Shortcut functions for sending strings, bytes, JSON in the envelope structure without serialization or allocation.

Install

go get -u github.com/zerodha/fastglue

Usage

import "github.com/zerodha/fastglue"

Examples

# Packages

No description provided by the author

# Functions

BadMethodHandler produces an enveloped JSON response for 405 errors.
New creates and returns a new instance of Fastglue.
NewGlue creates and returns a new instance of Fastglue with custom error handlers pre-bound.
NewMockServer initializes a mock HTTP server against which any request be sent, and the request can be responded to with a mock response.
NotFoundHandler produces an enveloped JSON response for 404 errors.
ReqLenParams is an (opinionated) middleware that checks if a given set of parameters are set in the GET or POST params and if each of them meets a minimum length criteria.
ReqLenRangeParams is an (opinionated) middleware that checks if a given set of parameters are set in the GET or POST params and if each of them meets a minimum and maximum length range criteria.
ReqParams is an (opinionated) middleware that checks if a given set of parameters are set in the GET or POST params.
ScanArgs takes a fasthttp.Args set, takes its keys and values and applies them to a given struct using reflection.

# Constants

AuthBasic represents HTTP BasicAuth scheme.
AuthToken represents the key:value Token auth scheme.
JSON is an alias for the JSON content type.
PLAINTEXT is an alias for the plaintext content type.
XML is an alias for the XML content type.

# Structs

Envelope is a highly opinionated, "standardised", JSON response structure.
Fastglue is the "glue" wrapper over fasthttp and fasthttprouter.
MockRequest represents a single mock request.
MockResponse represents a mock response produced by the mock server.
MockServer is a mock HTTP server.
Request is a wrapper over fasthttp's RequestCtx that's injected into request handlers.

# Type aliases

ErrorType defines string error constants (eg: TokenException) to be sent with JSON responses.
FastMiddleware is the fastglue middleware handler function that can be registered using Before() and After() functions.
FastRequestHandler is the fastglue HTTP request handler function that wraps over the fasthttp handler.