package api import ( "database/sql" "net/http" "os" ) func NewRouter(metaDB *sql.DB, marketingDB *sql.DB) http.Handler { mux := http.NewServeMux() mux.Handle("/api/templates", withAccess(withTrace(TemplatesHandler(metaDB, marketingDB)))) mux.Handle("/api/templates/", withAccess(withTrace(TemplatesHandler(metaDB, marketingDB)))) mux.Handle("/api/exports", withAccess(withTrace(ExportsHandler(metaDB, marketingDB)))) mux.Handle("/api/exports/", withAccess(withTrace(ExportsHandler(metaDB, marketingDB)))) mux.Handle("/api/metadata/fields", withAccess(withTrace(MetadataHandler(metaDB, marketingDB)))) mux.Handle("/api/creators", withAccess(withTrace(CreatorsHandler(marketingDB)))) mux.Handle("/api/creators/", withAccess(withTrace(CreatorsHandler(marketingDB)))) mux.Handle("/api/resellers", withAccess(withTrace(ResellersHandler(marketingDB)))) mux.Handle("/api/resellers/", withAccess(withTrace(ResellersHandler(marketingDB)))) mux.Handle("/api/plans", withAccess(withTrace(PlansHandler(marketingDB)))) mux.Handle("/api/plans/", withAccess(withTrace(PlansHandler(marketingDB)))) mux.HandleFunc("/api/utils/decode_key", func(w http.ResponseWriter, r *http.Request) { v := r.URL.Query().Get("v") if v == "" { w.WriteHeader(http.StatusBadRequest) w.Write([]byte("missing v")) return } d := decodeOrderKey(v) w.Header().Set("Content-Type", "application/json") w.Write([]byte("{\"decoded\":\"" + d + "\"}")) }) sd := staticDir() mux.Handle("/", http.FileServer(http.Dir(sd))) return mux } func staticDir() string { if _, err := os.Stat("web/index.html"); err == nil { return "web" } if _, err := os.Stat("../web/index.html"); err == nil { return "../web" } return "web" }