// CreateRouter creates the routes for handling requests to the web interface. // This function returns an http.Handler to be used in http.ListenAndServe(). func CreateRouter(db *sqlx.DB) http.Handler { router := mux.NewRouter().StrictSlash(true) router.HandleFunc("/login", auth.GetLoginFunc(db)).Methods("POST") router.HandleFunc(apiPath+"{table}", auth.Use(optionsHandler, auth.DONTRequireLogin)).Methods("OPTIONS") router.HandleFunc(apiPath+"{table}/{id}", auth.Use(optionsHandler, auth.DONTRequireLogin)).Methods("OPTIONS") router.HandleFunc("/config/cr/{cdn}/CRConfig.json", auth.Use(getHandleCRConfigFunc(db), auth.RequireLogin)) router.HandleFunc("/config/csconfig/{hostname}", auth.Use(getHandleCSConfigFunc(db), auth.RequireLogin)) addApiHandlers(router, db) return auth.Use(router.ServeHTTP, auth.GetContext) }
// 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()) } } }