func (root *RootHandler) Bind(router SimpleRouter, endpoint rest.ServerResource, handler rest.Handler, resourceRoot string) { if handler == nil { panic(fmt.Sprintf("handler can't be nil", endpoint)) } resourcePathT := path.Join(resourceRoot, endpoint.ResourceT()) httpMethod := endpoint.Verb() errMessage := "can't bind. method named %s is missing from type %s" handlerName := reflect.TypeOf(handler).Name() var fn rest.HandlerFunc if httpMethod == "GET" { if h, ok := handler.(rest.GetHandler); !ok { panic(fmt.Sprintf(errMessage, httpMethod, handlerName, resourcePathT)) } else { fn = h.Get } } else if httpMethod == "POST" { if h, ok := handler.(rest.PostHandler); !ok { panic(fmt.Sprintf(errMessage, httpMethod, handlerName, resourcePathT)) } else { fn = h.Post } } else if httpMethod == "PUT" { if h, ok := handler.(rest.PutHandler); !ok { panic(fmt.Sprintf(errMessage, httpMethod, handlerName, resourcePathT)) } else { fn = h.Put } } else if httpMethod == "DELETE" { if h, ok := handler.(rest.DeleteHandler); !ok { panic(fmt.Sprintf(errMessage, httpMethod, handlerName, resourcePathT)) } else { fn = h.Delete } } else if httpMethod == "HEAD" { if h, ok := handler.(rest.HeadHandler); !ok { panic(fmt.Sprintf(errMessage, httpMethod, handlerName, resourcePathT)) } else { fn = h.Head } } else if httpMethod == "PATCH" { if h, ok := handler.(rest.PatchHandler); !ok { panic(fmt.Sprintf(errMessage, httpMethod, handlerName, resourcePathT)) } else { fn = h.Patch } } wrappedHandler := root.createHttpHandler(fn, endpoint) router.RegisterRoute(httpMethod, resourcePathT, wrappedHandler) lg.Inform(fmt.Sprintf("Bound endpoint %s %s", httpMethod, resourcePathT)) }
func setResponseContentType(response *rest.Response, req *http.Request, resp http.ResponseWriter, endpoint rest.ServerResource) { // TODO: ideally we'd match the preferred accept type, with a type we can respond with, // for now, until we support this, just ignore the accept string and return the first // content type we know we can return if response.ContentType == "" { cts := endpoint.ResponseContentTypes() if cts != nil && len(cts) > 0 { response.ContentType = cts[0] } else if cts = req.Header["Content-Type"]; cts != nil && len(cts) > 0 { response.ContentType = cts[0] //try returning the same as the requested type } else if cts = endpoint.RequestContentTypes(); cts != nil && len(cts) > 0 { response.ContentType = cts[0] } } resp.Header()["Content-Type"] = []string{response.ContentType} }