Skip to content

Instantly share code, notes, and snippets.

@kisielk
Created January 18, 2013 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kisielk/4561395 to your computer and use it in GitHub Desktop.
Save kisielk/4561395 to your computer and use it in GitHub Desktop.
Experimenting with Go http handlers.
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