func (r *apiGroupHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { // don't handle URLs that aren't /apis/<groupName> if req.URL.Path != "/apis/"+r.groupName && req.URL.Path != "/apis/"+r.groupName+"/" { http.Error(w, "", http.StatusNotFound) return } apiServices, err := r.lister.List(labels.Everything()) if statusErr, ok := err.(*apierrors.StatusError); ok && err != nil { responsewriters.WriteRawJSON(int(statusErr.Status().Code), statusErr.Status(), w) return } if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } apiServicesForGroup := []*apiregistrationapi.APIService{} for _, apiService := range apiServices { if apiService.Spec.Group == r.groupName { apiServicesForGroup = append(apiServicesForGroup, apiService) } } if len(apiServicesForGroup) == 0 { http.Error(w, "", http.StatusNotFound) return } discoveryGroup := convertToDiscoveryAPIGroup(apiServicesForGroup, r.serviceLister, r.endpointsLister) if discoveryGroup == nil { http.Error(w, "", http.StatusNotFound) return } json, err := runtime.Encode(api.Codecs.LegacyCodec(), discoveryGroup) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if _, err := w.Write(json); err != nil { panic(err) } }
// Install adds the Index webservice to the given mux. func (i Index) Install(c *mux.APIContainer) { c.UnlistedRoutes.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { status := http.StatusOK if r.URL.Path != "/" && r.URL.Path != "/index.html" { // Since "/" matches all paths, handleIndex is called for all paths for which there is no handler registered. // We want to return a 404 status with a list of all valid paths, incase of an invalid URL request. status = http.StatusNotFound } var handledPaths []string // Extract the paths handled using restful.WebService for _, ws := range c.RegisteredWebServices() { handledPaths = append(handledPaths, ws.RootPath()) } // Extract the paths handled using mux handler. handledPaths = append(handledPaths, c.NonSwaggerRoutes.HandledPaths()...) sort.Strings(handledPaths) responsewriters.WriteRawJSON(status, metav1.RootPaths{Paths: handledPaths}, w) }) }
// handleVersion writes the server's version information. func (v Version) handleVersion(req *restful.Request, resp *restful.Response) { responsewriters.WriteRawJSON(http.StatusOK, *v.Version, resp.ResponseWriter) }
// TODO this should properly handle content type negotiation // if the caller asked for protobuf and you write JSON bad things happen. func (r *responder) Object(statusCode int, obj runtime.Object) { responsewriters.WriteRawJSON(statusCode, obj, r.w) }