pipes

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2019 License: MIT Imports: 2 Imported by: 2

README

Pipes Build Status

Minimalist HTTP middleware library for Go web applications running on Google App Engine. Works great with Mux library.

Why to use middlewares and pipelines?

It helps to reuse request handling code and achieve better separation of concerns.

Usage

Documentation

// Code snippet from a real web application.
apiPipeline := pipes.New().
		Use(middleware.AppEngineContext).   // HTTP request will first get Google App Engine context
		Use(panicMiddleware).               // panic handler will recover, log and present user friendly message if panic is called              
		Use(rateLimitMiddleware).           // rate limitter will make sure that our API endpoint is not overwhelmed
		Use(cookieAuthMiddleware).          // auth cookie will be validated and user object added to the context
        Use(authAPIMiddleware)              // if user is missing, unauthorised API response will be returned without running any business logic

router := mux.NewRouter()
router.HandleFunc("/api/test", apiPipeline.Use(apiHandler).Build())
router.HandleFunc("/api/test2", apiPipeline.Use(api2Handler).Build())
http.Handle("/", router)

func apiHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, next pipes.Next){
    fmt.Fprint(w, "OK")
    next(ctx)
}
func api2Handler(ctx context.Context, w http.ResponseWriter, r *http.Request, next pipes.Next){
    fmt.Fprint(w, "OK2")
    next(ctx)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContextKey

type ContextKey string

ContextKey pipes context key type.

func (ContextKey) String

func (p ContextKey) String() string

type Middleware

type Middleware = func(ctx context.Context, w http.ResponseWriter, r *http.Request, next Next)

Middleware function.

type Next

type Next = func(context.Context)

Next next middleware function.

type Pipeline

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

Pipeline instance.

func New

func New() Pipeline

New creates new empty pipeline.

func (Pipeline) Build

func (p Pipeline) Build() func(w http.ResponseWriter, r *http.Request)

Build returns a function which can run the pipeline.

func (Pipeline) Use

func (p Pipeline) Use(m Middleware) Pipeline

Use adds a new middleware to the pipeline and returns a new pipeline.

Jump to

Keyboard shortcuts

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