Example #1
0
func HandlerFunc(f func(http.ResponseWriter, *http.Request, *Adapter)) http.Handler {
	return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
		userId, password, _ := req.BasicAuth()
		if ada, status, err := New(userId, password); err != nil {
			util.Error(rw, err.Error(), status)
		} else {
			f(rw, req, ada)
		}
	})
}
Example #2
0
func (r Resource) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	if len(r) == 0 {
		// The resource actually does not exist.
		util.NotFound(rw, req)
	} else if h, ok := r[req.Method]; !ok {
		// We don't support this method.
		var allow []string
		for k := range r {
			allow = append(allow, k)
		}
		sort.Strings(allow)
		rw.Header().Set("Allow", strings.Join(allow, ", "))
		if req.Method == "OPTIONS" {
			rw.WriteHeader(http.StatusOK)
		} else {
			util.Error(rw, "Method not allowed.", http.StatusMethodNotAllowed)
		}
	} else {
		// We can handle it.
		h.ServeHTTP(rw, req)
	}
}