Skip to content

Instantly share code, notes, and snippets.

@owulveryck
Created May 18, 2017 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save owulveryck/c8cb652789c921d381af8653b1ec18c1 to your computer and use it in GitHub Desktop.
Save owulveryck/c8cb652789c921d381af8653b1ec18c1 to your computer and use it in GitHub Desktop.
Exclude a ping from the rest of the middlewares processing / Negroni / Gorilla
package main
import (
"fmt"
"github.com/gorilla/mux"
"github.com/urfave/negroni"
"net/http"
)
type ping struct {
Path string
Reply string
}
func (p *ping) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
if r.URL.Path == p.Path {
fmt.Fprintf(w, p.Reply)
} else {
next(w, r)
}
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "test")
})
n := negroni.New()
n.Use(negroni.NewRecovery())
n.Use(&ping{"/ping", "pong"})
n.Use(negroni.NewLogger())
n.UseHandler(r)
http.ListenAndServe(":3000", n)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment