Skip to content

Instantly share code, notes, and snippets.

@Bebbolus
Created November 30, 2018 16:47
Show Gist options
  • Save Bebbolus/05b49d306557563fb01fc29e023d582a to your computer and use it in GitHub Desktop.
Save Bebbolus/05b49d306557563fb01fc29e023d582a to your computer and use it in GitHub Desktop.
package main
import (
"net/http"
"strings"
)
type middleware string
func (m middleware) Pass(args string) func(http.HandlerFunc) http.HandlerFunc {
return func(f http.HandlerFunc) http.HandlerFunc {
// Define the http.HandlerFunc
return func(w http.ResponseWriter, r *http.Request) {
//split args and check if the request as this method
acceptedMethods := strings.Split(args, "|")
for _, v := range acceptedMethods {
if r.Method == v {
// Call the next middleware in chain
f(w, r)
return
}
}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
}
}
// export as symbol named "Middleware"
var Middleware middleware
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment