cattp

package module
v0.0.0-...-1df1a57 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2023 License: GPL-3.0 Imports: 5 Imported by: 0

README

cattp

Context wrapper for Go's native HTTP package

Mega thanks to @shurizzle for his invaluable help in learning the concepts applied.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Host string
	Port string
	URL  string
}

type Handler

type Handler[T any] interface {
	ServeHTTP(http.ResponseWriter, *http.Request, T)
}

Defining custom interface with ServeHTTP signature containing custom parameter to register custom handlers with DB We need to expect not anymore "http.Handler" but simply Handler

type HandlerFunc

type HandlerFunc[T any] func(http.ResponseWriter, *http.Request, T)

Used to type any function that has this signature into a type that implemets the Handler interface, to allow it to be registerd on the mux with a corresponding pattern. Upon pattern match, this function will be invoked it call the user's defined callback function 'f' with all the arguments passed from the multiplexer adapter middleware (the one that turns the standard http.Handler signature with our custom ServeHTTP with db argument)

func (HandlerFunc[T]) ServeHTTP

func (f HandlerFunc[T]) ServeHTTP(w http.ResponseWriter, r *http.Request, c T)

type Router

type Router[T any] struct {
	Mux     *http.ServeMux
	Context T
	// contains filtered or unexported fields
}

func New

func New[T any](context T) *Router[T]

func (*Router[T]) Handle

func (router *Router[T]) Handle(pattern string, handler Handler[T])

func (*Router[T]) HandleFunc

func (router *Router[T]) HandleFunc(pattern string, handler func(w http.ResponseWriter, r *http.Request, context T))

func (*Router[T]) Listen

func (router *Router[T]) Listen(conf *Config) error

func (*Router[T]) NotFoundHandle

func (router *Router[T]) NotFoundHandle(handler Handler[T])

Register on our Mux the Handler Type provided in args for "Not Found" It must be a Handler Type, otherwise use 'NotFoundHandleFunc'

func (*Router[T]) NotFoundHandleFunc

func (router *Router[T]) NotFoundHandleFunc(handler func(http.ResponseWriter, *http.Request, T))

Commodity wrapper for registering a custom passed Not Found function calls the 'NotFoundHandle' function, but allow us to pass a custom function that matches the signature.

func (*Router[T]) ServeHTTP

func (router *Router[T]) ServeHTTP(w http.ResponseWriter, r *http.Request)

Allows the Router to behave as Handler for incoming HTTP Requests by wrapping the it's internal Mux Handler, acting as middleware.

Jump to

Keyboard shortcuts

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