Skip to content

sanae10001/go-simple-middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

negroni-middlewares

Simple go http middleware

Support negroni and standard http library

  • Basic auth
  • Body limit
  • Request id
  • JWT

Example

jwt:

func main() {
	h := func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("OK"))
	}
	j := jwt.New(jwt.Config{SigningKey: []byte("asecretsigningstring")})

	mux := http.NewServeMux()
	mux.Handle("/", j.Handler(http.HandlerFunc(h)))

	http.ListenAndServe(":8080", mux)
}

jwt for negroni

func main() {
	h := func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("OK"))
	}
	j := jwt.New(jwt.Config{SigningKey: []byte("asecretsigningstring")})

	mux := http.NewServeMux()
	mux.HandleFunc("/", h)
	
	n := negroni.New()
	n.UseFunc(j.HandlerWithNext)
	n.UseHandler(mux)

	http.ListenAndServe(":8080", n)
}