Skip to content

Instantly share code, notes, and snippets.

@moraes
Forked from kisielk/handlers.go
Last active December 11, 2015 12:08
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 moraes/4598513 to your computer and use it in GitHub Desktop.
Save moraes/4598513 to your computer and use it in GitHub Desktop.
package foo
import (
"net/http"
)
type MethodHandler map[string]http.Handler
func (h MethodHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if handler, ok := h[req.Method]; ok {
handler.ServeHTTP(w, req)
} else {
if len(h) > 0 {
header := ""
for k := range h {
header += k + ", "
}
w.Header().Set("Allow", header[:len(header)-2])
}
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment