s3manager-web/vendor/github.com/mastertinner/adapters
2017-05-12 09:34:31 +02:00
..
logging Refactor whole app 2017-05-09 01:12:18 +02:00
oauth2 Upgrade depepndencies 2017-05-12 09:34:31 +02:00
adapt.go Refactor whole app 2017-05-09 01:12:18 +02:00
adapter.go Refactor whole app 2017-05-09 01:12:18 +02:00
LICENSE Refactor whole app 2017-05-09 01:12:18 +02:00
README.md Refactor whole app 2017-05-09 01:12:18 +02:00

Adapters

Adapters is a collection of useful HTTP middleware or "Adapters". They follow the Adapter Pattern described by Mat Ryer in his blog post Writing middleware in #golang and how Go makes it so much fun.

Adapters can be chained in any way and will be executed in the order they are specified.

Usage

package main

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

        "github.com/mastertinner/adapters"
        "github.com/mastertinner/adapters/logging"
)

// IndexHandler says what it loves
func IndexHandler() http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
                fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
        })
}

func main() {
        logger := log.New(os.Stdout, "", log.Ldate|log.Ltime)
        http.Handle("/", adapters.Adapt(IndexHandler(), logging.Handler(logger)))
        log.Fatal(http.ListenAndServe(":8080", nil))
}

Adapters

This package contains the following adapters:

  • Logging: Logs the request and the time it took to serve it
  • OAuth2: Checks if a request is authenticated through OAuth 2 using Redis as a cache