blackbox

package module
v0.0.0-...-03a2197 Latest Latest
Warning

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

Go to latest
Published: May 2, 2021 License: MIT Imports: 3 Imported by: 0

README

Black Box Router

Package blackbox provides simple to use, extensible, flexible, and easily testable typesafe server routing for your web apps and microservices.

Blackbox does that by utilising a functional approach to request handling, where routing nodes (type Router) provide filtering handling and return an exposed Response instance that can be tested and examined.

  • For more information on testing, please see the test folder.
  • To see a working example, look at this example.

Why Black Box

  1. Seamless interop with the standard net/http.Server: blackbox.Router implements the net/http.Handler interface
  2. Pleasure to test: due to the convenient Handler interface definition, blackbox.Handler actually returns blackbox.Response instead of writing it to net/http.ResponseWriter
  3. Well tested with an average coverage of 85% across all packages
  4. Extendable architecture: if you need more filters or additional middleware, just implement your own by implementing interfaces like blackbox.Filter
  5. As a bonus, blackbox.Response supports JSON serialization out of the box: just call resp.EncodeJSON(object) and you're golden!

Documentation

Overview

Package blackbox provides simple to use, extensible, flexible, and easily testable typesafe server routing for your web apps and microservices.

Blackbox does that by utilising a functional approach to request handling, where routing nodes (type Router) provide filtering handling and return an exposed Response instance that can be tested and examined.

For more information on testing, please see the 'test' folder.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Filter

type Filter interface {
	Accepts(r *http.Request) bool
}

Filter represents any filtering entity that can decide whether it wants to deal with a given net/http.Request through its Accept predicate.

type FilterFunc

type FilterFunc func(*http.Request) bool

FilterFunc is a special function type that implements the Filter interface.

func (FilterFunc) Accepts

func (f FilterFunc) Accepts(r *http.Request) bool

Accepts helps FilterFunc implement the Filter interface.

type Handler

type Handler interface {
	Handle(r *http.Request) *Response
}

Handler interface represents a functional handler that returns the Response instance taking net/http.Request as its only parameter.

type HandlerFunc

type HandlerFunc func(r *http.Request) *Response

HandlerFunc is a special function type that implements the Handler interface.

func (HandlerFunc) Handle

func (h HandlerFunc) Handle(r *http.Request) *Response

Handle helps HandlerFunc implement the Handler interface.

type Response

type Response struct {
	// See https://pkg.go.dev/net/http#pkg-constants for status constants.
	Status  int
	Headers map[string]string
	Cookies map[string]*http.Cookie
	Body    *bytes.Buffer
}

Response represents a standard HTTP response.

func NewResponse

func NewResponse() *Response

NewResponse returns a newly created Response instance with status 200 OK, and pre-allocated Headers map and Body writer.

func (*Response) DecodeJSON

func (resp *Response) DecodeJSON(data interface{}) error

DecodeJSON uses Go's default encoding/json library to decode data from Request.Body as JSON.

func (*Response) EncodeJSON

func (resp *Response) EncodeJSON(data interface{}) error

EncodeJSON uses Go's default encoding/json library to encode data as JSON and write it into Request.Body.

func (*Response) Respond

func (resp *Response) Respond(w http.ResponseWriter)

Respond writes information stored within this Response instance into the given net/http.ResponseWriter, thus, sending it to the client.

Respond helps Response implement Responder interface.

func (*Response) WithCookie

func (resp *Response) WithCookie(cookie *http.Cookie) *Response

WithCookie sets cookie in Response.Cookies (overwriting any existing cookies with the same name) and returns reference to that same Response.

func (*Response) WithHeader

func (resp *Response) WithHeader(name, value string) *Response

WithHeader sets header in Response.Headers (overwriting any exising headers with the same name) and returns reference to that same Response.

func (*Response) WithStatus

func (resp *Response) WithStatus(status int) *Response

WithStatus sets Response status and returns reference to that same Response.

func (*Response) Write

func (resp *Response) Write(b []byte) *Response

Write writes given bytes to Response.Body and returns reference to the Response.

func (*Response) WriteString

func (resp *Response) WriteString(s string) *Response

WriteString writes given string to Response.Body and returns reference to the Response.

type Route

type Route interface {
	Filter
	Handler
}

Route is a filtering entity that has a Handler attached to it in case it decides to accept given net/http.Request.

type Router

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

Router is the main feature of the blackbox package. It allows you to build extensible, testable and typesafe routing for your web app or microservice.

Router implements

  • net/http.Handler
  • Filter
  • Handler
  • Route

func New

func New() *Router

New returns a newly create Router instance with pre-allocated filters and routes and a default catch-all handler that always responds with status 501 Not Implemented. Feel free to customise it using available methods!

func (*Router) Accepts

func (rtr *Router) Accepts(r *http.Request) bool

Accepts helps Router implement the Filter and Route interfaces.

func (*Router) Handle

func (rtr *Router) Handle(r *http.Request) (resp *Response)

Handle helps Router implement the Handler and Route interfaces.

func (*Router) ServeHTTP

func (rtr *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP helps Router implement net/http.Handler interface for effective interop with the net/http.Server.

func (*Router) Subrouter

func (rtr *Router) Subrouter() (sub *Router)

Subrouter adds a newly created subrouter instance to the Router, returning a reference to that subrouter for future modification.

func (*Router) WithFilterFuncs

func (rtr *Router) WithFilterFuncs(filters ...FilterFunc) *Router

WithFilterFuncs accepts mutilple FilterFunc's and adds them to the Router, returning a reference to the same Router instance.

func (*Router) WithFilters

func (rtr *Router) WithFilters(filters ...Filter) *Router

WithFilters accepts multiple Filter's and adds them to the Router, returning a reference to the same Router instance.

func (*Router) WithHandler

func (rtr *Router) WithHandler(handler Handler) *Router

WithHandler accepts a Handler and adds it to the Router, returning a reference to the same Router instance.

func (*Router) WithHandlerFunc

func (rtr *Router) WithHandlerFunc(f HandlerFunc) *Router

WithHandlerFunc accepts a HandlerFunc and adds it to the Router, returning a reference to the same Router instance.

func (*Router) WithRoute

func (rtr *Router) WithRoute(sub Route) *Router

WithRoute accepts a Route, adds it to the registered routes, and returns reference to the accepting Router.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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