Created
January 18, 2013 00:58
-
-
Save kisielk/4561395 to your computer and use it in GitHub Desktop.
Experimenting with Go http handlers.
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 httphandlers | |
import ( | |
"net/http" | |
) | |
type MethodHandler struct { | |
handlers map[string]http.Handler | |
} | |
func (h *MethodHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { | |
handler := h.GetHandler(req.Method) | |
if handler == nil { | |
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) | |
} else { | |
handler.ServeHTTP(w, req) | |
} | |
} | |
func (h *MethodHandler) SetHandler(method string, handler http.Handler) { | |
h.handlers[method] = handler | |
} | |
func (h *MethodHandler) DeleteHandler(method string) { | |
delete(h.handlers, method) | |
} | |
func (h *MethodHandler) GetHandler(method string) http.Handler { | |
return h.handlers[method] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment