Skip to content

Instantly share code, notes, and snippets.

@codenoid
Created September 8, 2019 16:33
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 codenoid/ae85414fb31af738581a06e2c6a94da9 to your computer and use it in GitHub Desktop.
Save codenoid/ae85414fb31af738581a06e2c6a94da9 to your computer and use it in GitHub Desktop.
before_request.go
package main
import (
"log"
"net/http"
)
import (
"github.com/julienschmidt/httprouter"
)
type BeforeRequest map[int]http.Handler
func (hs BeforeRequest) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// your logic check
// ...
// ...
// to stop the handler, use redirect and return
// handle the request.
hs[0].ServeHTTP(w, r)
}
func index(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
}
func main() {
router := httprouter.New()
// Index
router.GET("/", index)
hs := make(BeforeRequest)
hs[0] = router
log.Fatal(http.ListenAndServe("0.0.0.0:2020", hs))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment