mux

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 15, 2021 License: MIT Imports: 10 Imported by: 0

README

mux

Documentation

Overview

Package mux is a minimal web framework that wraps the https://github.com/julienschmidt/httprouter.

Index

Constants

View Source
const Key = keyType(0)

Key is a key to stores and retrieves the State from the request context.

Variables

View Source
var ErrJSONDecoding = errors.New("mux: error decodes JSON")
View Source
var ErrStateMissing = errors.New("mux: the initial state is missing from the request context")

ErrStateMissing is the error if the initial state is missing from the request context.

Functions

func FromJSON

func FromJSON(r io.Reader, v interface{}) error

FromJSON reads JSON-encoded data from the request body and stores it in the value pointed to by v

func IsShutdownError

func IsShutdownError(err error) bool

IsShutdownError checks if the wrapped error contains the *errorShutdown

func NewShutdownError

func NewShutdownError(message string) error

NewShutdownError returns an error that causes the framework to signal a graceful shutdown.

func ToJSON

func ToJSON(ctx context.Context, w http.ResponseWriter, data interface{}, status int) error

ToJSON encodes the given data to JSON and write it the given w.

Types

type Error

type Error struct {
	Status int   `json:"status"`
	Err    error `json:"err"`
}

Error is used to pass an error during the request through the application with web specific context.

func NewRequestError

func NewRequestError(err error, status int) *Error

NewRequestError wraps a provided error with an HTTP status code. This function should be used when handlers encounter expected (trusted) errors.

func (Error) Error

func (re Error) Error() string

Error implements error interface.

type HandleFunc

type HandleFunc func(w http.ResponseWriter, r *http.Request) error

HandleFunc is just a http.HandleFunc that can returns an error. By returning an error, now we can make a centralized error handling.

type Middleware

type Middleware func(handleFunc HandleFunc) HandleFunc

Middleware is a function that will be executed before or/and after the given handleFunc has been executed.

There are two types of middleware in this Router. First is the Global middlewares and the second is the Route middlewares. The Global middlewares will be applied to all handleFunc, meanwhile the Route middlewares only applied specific to the handleFunc in the given route.

type Params

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

Params is a Param-slice, as returned by the router. The slice is ordered, the first URL parameter is also the first slice value. It is therefore safe to read values by the index.

func GetParam

func GetParam(ctx context.Context) *Params

GetParam pulls the URL parameters from a request context, or returns nil if none are present

func (*Params) ByName

func (pr *Params) ByName(name string) string

ByName returns the value of the first Param which key matches the given name. If no matching Param is found, an empty string is returned.

type Router

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

Router is an HTTP request multiplexer. It matches the URL of each incoming request against a list of registered patterns and calls the handler for the pattern that most closely matches the URL.

func NewRouter

func NewRouter(sc ShutdownChannel, middlewares ...Middleware) *Router

NewRouter creates a new mux router.

func (*Router) Delete

func (mr *Router) Delete(path string, handleFunc HandleFunc)

Delete is the syntactic sugar for Handle("DELETE", path, handleFunc)

func (*Router) Get

func (mr *Router) Get(path string, handleFunc HandleFunc)

Get is the syntactic sugar for Handle("GET", path, handleFunc)

func (*Router) Handle

func (mr *Router) Handle(method, path string, handleFunc HandleFunc)

Handle registers the handleFunc for the given HTTP method and URL path.

func (*Router) MiddlewareHandle

func (mr *Router) MiddlewareHandle(method, path string, handleFunc HandleFunc, middlewares ...Middleware)

MiddlewareHandle registers the handleFunc with the Route middlewares for the given HTTP method and URL path.

func (*Router) Patch

func (mr *Router) Patch(path string, handleFunc HandleFunc)

Patch is the syntactic sugar for Handle("PATCH", path, handleFunc)

func (*Router) Post

func (mr *Router) Post(path string, handleFunc HandleFunc)

Post is the syntactic sugar for Handle("POST", path, handleFunc)

func (*Router) Put

func (mr *Router) Put(path string, handleFunc HandleFunc)

Put is the syntactic sugar for Handle("PUT", path, handleFunc)

func (*Router) ServeHTTP

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

ServeHTTP implements the http.Handler interface.

func (*Router) SignalShutdown

func (mr *Router) SignalShutdown()

SignalShutdown sends a shutdown signal through the shutdown channel.

func (*Router) With

func (mr *Router) With(middleware Middleware, middlewares ...Middleware) *withMiddleware

With is a helper method to make Handle with middleware at the route level more easier to read. For example:

Without using With the Handle method looks like this:

mux.Handle("GET", "/some-path", handleFunc, []Middleware{m1, m2, m3,...mk})

By using With it will look like this:

mux.With(m1, m2, m3, ..., mk).Handle("GET", "/some-path", handleFunc)

type ShutdownChannel

type ShutdownChannel chan os.Signal

ShutdownChannel is a channel that Mux used to tell the application to shutdown gracefully by sending a termination signal (syscall.SIGTERM).

type State

type State struct {
	TimeCreated time.Time
	StatusCode  int
}

State is the initial state for each request.

func GetState

func GetState(ctx context.Context) (*State, error)

GetState gets the initial state form the given context.

Jump to

Keyboard shortcuts

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