relay

package module
v0.0.0-...-9be449a Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2018 License: MIT Imports: 1 Imported by: 0

README

relay

Simple golang middleware for https://github.com/valyala/fasthttp

notes

Be careful, middlewares should be stateless.

example

code:

func main() {
	handler := relay.New()

	handler.Use(doSomething1)
	handler.Use(doSomething2)
	handler.Use(doSomething3)

	fasthttp.ListenAndServe("127.0.0.1:80", handler.Handle)
	c := make(chan bool)
	<-c
}

func doSomething1 (ctx *fasthttp.RequestCtx, next relay.Next) error {
	fmt.Println("in 1")
	err := next()
	fmt.Println("out 1")
	return err
}

func doSomething2 (ctx *fasthttp.RequestCtx, next relay.Next) error {
	fmt.Println("in 2")
	err := next()
	fmt.Println("out 2")
	return err
}

func doSomething3 (ctx *fasthttp.RequestCtx, next relay.Next) error {
	fmt.Println("in 3")
	err := next()
	fmt.Println("out 3")
	return err
}

output:

in 1
in 2
in 3
out 3
out 2
out 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Middleware

type Middleware func(ctx *fasthttp.RequestCtx, next Next) error

Middleware represents any func processing ctx

type Next

type Next func() error

Next is a func to be called by Middleware

type Relay

type Relay interface {
	Use(mws ...Middleware)
	Handle(ctx *fasthttp.RequestCtx)
}

Relay represents common relay interface

func New

func New() Relay

New returns new relay instance

Jump to

Keyboard shortcuts

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