Middleware

Rafał Lorenz edited this page Feb 23, 2018 · 6 revisions

Passing middleware as follow A, B, C will result in A(B(C( handler ))) where handler is your handler method.

  1. Global Middlewares
  2. Method Middlewares
  3. Route Middlewares

Global Middlewares

package main

import (
	"fmt"
	"log"
	"time"
	"net/http"

        "github.com/vardius/gorouter"
)

func logger(next http.Handler) http.Handler {
  fn := func(w http.ResponseWriter, r *http.Request) {
    t1 := time.Now()
    next.ServeHTTP(w, r)
    t2 := time.Now()
    log.Printf("[%s] %q %v\n", r.Method, r.URL.String(), t2.Sub(t1))
  }

  return http.HandlerFunc(fn)
}

func example(next http.Handler) http.Handler {
  fn := func(w http.ResponseWriter, r *http.Request) {
	//do smth
    next.ServeHTTP(w, r)
  }

  return http.HandlerFunc(fn)
}

func Index(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, r *http.Request) {
	params, _ := gorouter.FromContext(r.Context())
    fmt.Fprintf(w, "hello, %s!\n", params.Value("name"))
}

func main() {
	//apply middlewares to all routes
	//can pass as many as you want
    router := gorouter.New(logger, example)

    router.GET("/", Index)
    router.GET("/hello/{name}", Hello)

    log.Fatal(http.ListenAndServe(":8080", router))
}

Method Middlewares

package main

import (
	"fmt"
	"log"
	"time"
	"net/http"

        "github.com/vardius/gorouter"
)

func logger(next http.Handler) http.Handler {
  fn := func(w http.ResponseWriter, r *http.Request) {
    t1 := time.Now()
    next.ServeHTTP(w, r)
    t2 := time.Now()
    log.Printf("[%s] %q %v\n", r.Method, r.URL.String(), t2.Sub(t1))
  }

  return http.HandlerFunc(fn)
}

func example(next http.Handler) http.Handler {
  fn := func(w http.ResponseWriter, r *http.Request) {
	//do smth
    next.ServeHTTP(w, r)
  }

  return http.HandlerFunc(fn)
}

func Index(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, r *http.Request) {
	params, _ := gorouter.FromContext(r.Context())
    fmt.Fprintf(w, "hello, %s!\n", params.Value("name"))
}

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

    router.GET("/", http.HandlerFunc(Index))
    router.GET("/hello/{name}", http.HandlerFunc(Hello))

	//apply middlewares to all routes with GET method
	//can pass as many as you want
    router.USE("GET", "", logger, example)

    log.Fatal(http.ListenAndServe(":8080", router))
}

Route Middlewares

package main

import (
	"fmt"
	"log"
	"time"
	"net/http"

        "github.com/vardius/gorouter"
)

func logger(next http.Handler) http.Handler {
  fn := func(w http.ResponseWriter, r *http.Request) {
    t1 := time.Now()
    next.ServeHTTP(w, r)
    t2 := time.Now()
    log.Printf("[%s] %q %v\n", r.Method, r.URL.String(), t2.Sub(t1))
  }

  return http.HandlerFunc(fn)
}

func example(next http.Handler) http.Handler {
  fn := func(w http.ResponseWriter, r *http.Request) {
	//do smth
    next.ServeHTTP(w, r)
  }

  return http.HandlerFunc(fn)
}

func Index(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, r *http.Request) {
	params, _ := gorouter.FromContext(r.Context())
    fmt.Fprintf(w, "hello, %s!\n", params.Value("name"))
}

func main() {
    router := gorouter.New()
    router.GET("/", http.HandlerFunc(Index))
    router.GET("/hello/{name}", http.HandlerFunc(Hello))

	//apply middlewares to route and all it children
	//can pass as many as you want
    router.USE("GET", "/hello/{name}", logger, example)

    log.Fatal(http.ListenAndServe(":8080", router))
}
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.