Last active
August 29, 2015 14:07
-
-
Save sescobb27/b05b02923488ab8142f4 to your computer and use it in GitHub Desktop.
http method matcher for handlers concerning only about certain http methods
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 ( | |
"fmt" | |
"net/http" | |
) | |
func method_matcher(method string, handler http.HandlerFunc) http.HandlerFunc { | |
var err string | |
return func(res http.ResponseWriter, req *http.Request) { | |
if method != req.Method { | |
err = fmt.Sprintf("Expected %s Http Method, Got: %s Http Method", method, req.Method) | |
http.Error(res, err, http.StatusBadRequest) | |
} else { | |
handler.ServeHTTP(res, req) | |
} | |
} | |
} | |
func Get(handler http.HandlerFunc) http.HandlerFunc { | |
return method_matcher("GET", handler) | |
} | |
func Post(handler http.HandlerFunc) http.HandlerFunc { | |
return method_matcher("POST", handler) | |
} | |
func Put(handler http.HandlerFunc) http.HandlerFunc { | |
return method_matcher("PUT", handler) | |
} | |
func Delete(handler http.HandlerFunc) http.HandlerFunc { | |
return method_matcher("DELETE", handler) | |
} | |
func Patch(handler http.HandlerFunc) http.HandlerFunc { | |
return method_matcher("PATCH", handler) | |
} | |
func main() { | |
server := http.NewServeMux() | |
server.Handle("/", Get(Index_Handler)) | |
server.Handle("/users", Get(All_Handler)) | |
server.Handle("/user/new", Post(New_Handler)) | |
http.ListenAndServe(":8000", server) | |
} | |
func Index_Handler(res http.ResponseWriter, req *http.Request) { | |
res.Write([]byte("Hello World")) | |
} | |
func New_Handler(res http.ResponseWriter, req *http.Request) { | |
res.Write([]byte("Creating User")) | |
} | |
func All_Handler(res http.ResponseWriter, req *http.Request) { | |
res.Write([]byte("All Users")) | |
} | |
// > curl localhost:8000/ -d "" | |
// Expected GET Http Method, Got: POST Http Method | |
// > curl localhost:8000/ | |
// Hello World | |
// > curl localhost:8000/users | |
// All Users | |
// > curl localhost:8000/users -d "" | |
// Expected GET Http Method, Got: POST Http Method | |
// > curl localhost:8000/user -d "" | |
// Creating User | |
// > curl localhost:8000/user | |
// Expected POST Http Method, Got: GET Http Method |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment