middleware

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2022 License: MIT Imports: 1 Imported by: 1

README

middleware

middleware library for github.com/julienschmidt/httprouter.

--

Usage

A middleware is any function that takes and returns a httprouter.Handle:

type Middleware func(httprouter.Handle) httprouter.Handle

A basic passthrough middleware:

package main

import(
  "net/http"

  "github.com/julienschmidt/httprouter"
  "github.com/rileyr/middleware"
  "github.com/rileyr/middleware/wares"
)

// Define the middleware function:
func passThrough(fn httprouter.Handle) httprouter.Handle {
  return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
      fn(w, r, p)
  }
}

// Define some handler:
func handler(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
  w.Write([]byte("hello world!"))
}

func main() {
  router := httprouter.New()

  // Create a middleware stack:
  s := middleware.NewStack()

  // Use your middleware:
  s.Use(passThrough)
  s.Use(wares.RequestID)
  s.Use(wares.Logging)

  // Wrap Handlers:
  router.GET("/", s.Wrap(handler))

  http.ListenAndServe(":3000", router)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewStack

func NewStack() *stack

Types

type Middleware

type Middleware func(httprouter.Handle) httprouter.Handle

type Stack

type Stack interface {
	/*
		Adds a middleware to the stack. MWs will be
		called in the same order that they are added,
		such that:

			Use(Request ID Middleware)
			Use(Request Timing Middleware)

		would result in the request id middleware being
		the outermouts layer, called first, before the
		timing middleware.
	*/
	Use(Middleware)

	/*
		Wraps a given handle with the current stack
		from the result of Use() calls.
	*/
	Wrap(httprouter.Handle) httprouter.Handle
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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