Created
November 30, 2018 16:47
-
-
Save Bebbolus/05b49d306557563fb01fc29e023d582a to your computer and use it in GitHub Desktop.
This file contains 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 ( | |
"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