Example #1
0
// CreateAdminRouter creates the routes for handling requests to the web interface.
// This function returns an http.Handler to be used in http.ListenAndServe().
func CreateRouter() http.Handler {
	router := mux.NewRouter().StrictSlash(true)
	router.HandleFunc("/login", auth.Login).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(handleCRConfig, auth.RequireLogin))
	router.HandleFunc("/config/csconfig/{hostname}", auth.Use(handleCSConfig, auth.RequireLogin))
	addApiHandlers(router)
	return auth.Use(router.ServeHTTP, auth.GetContext)
}
Example #2
0
func addApiHandlers(router *mux.Router) {
	for route, funcs := range api.ApiHandlers() {
		wrapRouter := func(f api.ApiHandlerFunc) func(w http.ResponseWriter, r *http.Request) {
			return func(w http.ResponseWriter, r *http.Request) {
				setHeaders(w, funcs.Methods())
				body, err := ioutil.ReadAll(r.Body)
				response, err := f(mux.Vars(r), body)
				if err != nil {
					log.Println(err)
				}
				jresponse := output.MakeApiResponse(response, nil, err)
				w.Header().Set("Content-Type", "application/json")
				enc := json.NewEncoder(w)
				enc.Encode(jresponse)
			}
		}
		for method, f := range funcs {
			router.HandleFunc(apiPath+route, auth.Use(wrapRouter(f), auth.RequireLogin)).Methods(method.String())
		}
	}
}