// addApiHandlers adds each API handler to the given router. func addApiHandlers(router *mux.Router, db *sqlx.DB) { for route, funcs := range api.ApiHandlers() { for method, f := range funcs { router.HandleFunc(apiPath+route, auth.Use(wrapApiHandler(f, funcs.Methods(), db), auth.RequireLogin)).Methods(method.String()) } } }
// optionsHandler handles HTTP OPTIONS requests, writing the // appropriate options and an HTTP OK. func optionsHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) table := vars["table"] route := table if _, ok := vars["id"]; ok { route += "/{id}" } apiHandlers := api.ApiHandlers() if tableHandlers, ok := apiHandlers[route]; !ok { w.WriteHeader(http.StatusNotFound) return } else { setHeaders(w, tableHandlers.Methods()) w.WriteHeader(http.StatusOK) } }