dispatcher

package module
v0.0.0-...-468232d Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2021 License: MIT Imports: 11 Imported by: 2

README

Go-Dispatcher

Install

go get gitlab.com/krobolt/go-dispatcher

Dispatcher adds named routes, http handlers and middlware to go-router.

HTTP Handlers

	func (w http.ResponseWriter, r *http.Request)

Middleware

	func() dispatcher.Middleware

Middleware can be attached to named routes, (FIFO) which can executed before and after the handler using the following pattern:

	example := func() dispatcher.Middleware {
		return func(f http.HandlerFunc) http.HandlerFunc {
			return func(w http.ResponseWriter, r *http.Request) {

				//Code to be executed before handler

				//Execute handler
				f(w, r)

				//Code be to executed after handler
			}
		}
	}

See gitlab.com/krobolt/go-router for defining routes/route patterns

Example Attach new route with "global" namespacec and "homepage" attached to index

	dir := "/"
	options := router.NewDefaultOptions()
	names := []string{"global"}

	dispatcher := dispatcher.NewDispatcher(dir, options, names)
	dispatcher.Add("GET", "/", home, []string{"homepage"})

Attach middleware to "homepage"

	dispatcher.Set("homepage", example)

Documentation

Overview

Package dispatcher http middleware routing and dispatcher. Based on https://gitlab.com/krobolt/go-router

Dispatcher adds named routes, http handlers and middlware to go-router.

Handler:

func (w http.ResponseWriter, r *http.Request)

Middleware can be attached to named routes, (FIFO) which can executed before and after the handler using the following pattern:

example := func() Middleware {
	return func(f http.HandlerFunc) http.HandlerFunc {
		return func(w http.ResponseWriter, r *http.Request) {

			//Code to be executed before handler

			//Execute handler
			f(w, r)

			//Code be to executed after handler
		}
	}
}

@See gitlab.com/krobolt/go-router for defining routes/route patterns

Example Attach new route with "global" namespacec and "homepage" attached to index

dir := "/"
options := router.NewDefaultOptions()
names := []string{"global"}

dispatcher := dispatcher.NewDispatcher(dir, options, names)
dispatcher.Add("GET", "/", home, []string{"homepage"})

Attach middleware to "homepage"

dispatcher.Set("homepage", example)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildQueryFromVars

func BuildQueryFromVars(vars map[string]interface{}) string

BuildQueryFromVars

func ETag

func ETag(key string, days int, w http.ResponseWriter, r *http.Request) bool

* Cache and HTTP2.0 Push *

func GenerateMiddlewareChain

func GenerateMiddlewareChain(f http.HandlerFunc, middlewares ...Middleware) http.HandlerFunc

GenerateMiddlewareChain wraps http.HandlerFunc with middleware collection and returns http.HandlerFunc

func GetHandlerFromHandleFunc

func GetHandlerFromHandleFunc(h http.HandlerFunc) http.Handler

GetHandlerFromHandleFunc wraps a http.HandlerFunc (SimpleHandle) and returns a http.Handler for example SimpleHandle can be passed to GetHandlerFromHandleFunc to be used in GenerateMiddlewareChain to create a http.HandlerFunc collection of SimpleHandle and Middlewares

func Serve

func Serve(port string, h http.Handler, logger *log.Logger)

Serve Application

func ServeTLS

func ServeTLS(port string, h http.Handler, logger *log.Logger, cert string, key string)

Types

type Dispatcher

type Dispatcher interface {
	ServeHTTP(w http.ResponseWriter, r *http.Request)
	Group(path string, callback func(), name []string)
	Add(method string, path string, handle http.HandlerFunc, names []string) error
	Match(method string, path string) Response
	GetMiddleware(name string) (func() Middleware, error)
	GetRouteMiddlewares(names []string) ([]Middleware, error)
	SetMiddleware(name string, m func() Middleware) error
	AddHandle(code int, h http.HandlerFunc)
}

Dispatcher is responsible for linking a route id to an http.Handlerfunc

func NewDispatcher

func NewDispatcher(dir string, options router.Options, names []string) Dispatcher

NewDispatcher returns new Dispatcher

type Handler

type Handler interface {
	GetHandler() http.HandlerFunc
	GetNames() []string
	GetID() string
}

func NewHandle

func NewHandle(ident string, HandlerFunc http.HandlerFunc, names []string) Handler

NewHandle create a new route handler

type Middleware

type Middleware func(http.HandlerFunc) http.HandlerFunc

Middleware is a wrapped http.HandlerFunc

func CacheProvider

func CacheProvider() Middleware

CacheProvider cache control middleware

func LoggingMiddleware

func LoggingMiddleware() Middleware

LoggingMiddleware log to file

type Response

type Response interface {
	GetRouterResponse() router.Response
	GetHandler() Handler
	GetStatusCode() int
	GetVars() map[string]interface{}
	GetNames() []string
}

Response Dispatcher response contains response for route as well as additional hander information

func NewResponse

func NewResponse(response router.Response, h Handler, statusCode int) Response

NewResponse Create a new dispatcher response

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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