"Web Development with Go" Middleware of shared data

This library is worth learning,

He seems to write more slip

package main

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

	//"time"

	"github.com/codegangsta/negroni"
	"github.com/gorilla/context"
)

func Authorize(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	token := r.Header.Get("X-AppToken")
	if token == "bXlVc2VybmFtZTpteVBhc3N3b3Jk" {
		log.Printf("Authorized to the system")
		context.Set(r, "user", "Shiju Varghese")
		next(w, r)
	} else {
		http.Error(w, "Not authorized.", 401)
	}
}

func index(w http.ResponseWriter, r *http.Request) {
	user := context.Get(r, "user")
	fmt.Fprintf(w, "welcome! %s", user)
}

func main() {
	mux := http.NewServeMux()

	mux.HandleFunc("/", index)

	n := negroni.Classic()
	n.Use(negroni.HandlerFunc(Authorize))
	n.UseHandler(mux)
	n.Run(":8080")

}

  

Guess you like

Origin www.cnblogs.com/aguncn/p/11965853.html