Created
May 18, 2017 08:19
-
-
Save owulveryck/c8cb652789c921d381af8653b1ec18c1 to your computer and use it in GitHub Desktop.
Exclude a ping from the rest of the middlewares processing / Negroni / Gorilla
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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