func HandleError(s *client.Schemas, t func(http.ResponseWriter, *http.Request) error) http.Handler { return api.ApiHandler(s, http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { if err := t(rw, req); err != nil { apiContext := api.GetApiContext(req) apiContext.WriteErr(err) } })) }
//NewRouter creates and configures a mux router func NewRouter() *mux.Router { schemas = &client.Schemas{} // ApiVersion apiVersion := schemas.AddType("apiVersion", client.Resource{}) apiVersion.CollectionMethods = []string{} // Schema schemas.AddType("schema", client.Schema{}) // Question question := schemas.AddType("question", model.Question{}) question.CollectionMethods = []string{} // Output output := schemas.AddType("output", model.Output{}) output.CollectionMethods = []string{} // Template template := schemas.AddType("template", model.Template{}) refreshAction := client.Action{} tempActions := make(map[string]client.Action) tempActions["refresh"] = refreshAction template.CollectionActions = tempActions delete(template.ResourceFields, "rancherCompose") delete(template.ResourceFields, "dockerCompose") delete(template.ResourceFields, "uuid") delete(template.ResourceFields, "questions") delete(template.ResourceFields, "TemplateVersionRancherVersion") delete(template.ResourceFields, "iconLink") delete(template.ResourceFields, "readmeLink") delete(template.ResourceFields, "projectURL") delete(template.ResourceFields, "version") f1 := template.ResourceFields["output"] f1.Type = "output" template.ResourceFields["output"] = f1 // Template Version templateVersion := schemas.AddType("templateVersion", model.Template{}) templateVersion.CollectionMethods = []string{} f2 := templateVersion.ResourceFields["questions"] f2.Type = "array[question]" templateVersion.ResourceFields["questions"] = f2 f3 := templateVersion.ResourceFields["output"] f3.Type = "output" templateVersion.ResourceFields["output"] = f3 // Catalog catalog := schemas.AddType("catalog", manager.Catalog{}) delete(catalog.ResourceFields, "catalogLink") // Error err := schemas.AddType("error", model.CatalogError{}) err.CollectionMethods = []string{} // API framework routes router := mux.NewRouter().StrictSlash(true) router.Methods("GET").Path("/").Handler(api.VersionsHandler(schemas, "v1-catalog")) router.Methods("GET").Path("/v1-catalog/schemas").Handler(api.SchemasHandler(schemas)) router.Methods("GET").Path("/v1-catalog/schemas/{id}").Handler(api.SchemaHandler(schemas)) router.Methods("GET").Path("/v1-catalog").Handler(api.VersionHandler(schemas, "v1-catalog")) // Application routes for _, route := range routes { router. Methods(route.Method). Path(route.Pattern). Name(route.Name). Handler(api.ApiHandler(schemas, route.HandlerFunc)) } router.GetRoute("RefreshCatalog").Queries("action", "refresh") router.GetRoute("RefreshCatalogTemplates").Queries("action", "refresh") return router }