From 9f2de2de9745d9f2ceb2b9d7f3f73a4dbf581728 Mon Sep 17 00:00:00 2001 From: a Date: Tue, 25 Oct 2022 16:54:28 -0500 Subject: [PATCH] redo server routing to be simpler --- cmd/otter/server.go | 51 ++++++++++++++++++--------------------------- src/App.tsx | 2 +- 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/cmd/otter/server.go b/cmd/otter/server.go index 98528d0..42c497a 100644 --- a/cmd/otter/server.go +++ b/cmd/otter/server.go @@ -3,7 +3,7 @@ package main import ( "encoding/json" "net/http" - "strings" + "os" "gfx.cafe/open/4bytes/sigs" "gfx.cafe/open/4bytes/topics" @@ -14,38 +14,27 @@ import ( ) func RouteServer(r chi.Router, cfg httpcfg.HttpCfg) { - r.Group(func(r chi.Router) { - filesDir := http.Dir(cfg.OtsStaticDir) - r.Use(middleware.Logger) - r.Use(middleware.Recoverer) - r.HandleFunc("/signatures/{hash}", triemap.HttpHandler(sigs.Both)) - r.HandleFunc("/topic0/{hash}", triemap.HttpHandler(topics.Both)) - r.HandleFunc("/config.json", func(w http.ResponseWriter, r *http.Request) { - json.NewEncoder(w).Encode(map[string]any{ - "erigonURL": cfg.OtsRpcDaemonUrl, - "beaconAPI": cfg.OtsBeaconApiUrl, - "assetsURLPrefix": cfg.OtsExternalAssetUrl, - }) + r.Use(middleware.Logger) + r.Use(middleware.Recoverer) + r.HandleFunc("/signatures/{hash}", triemap.HttpHandler(sigs.Both)) + r.HandleFunc("/topic0/{hash}", triemap.HttpHandler(topics.Both)) + r.HandleFunc("/config.json", func(w http.ResponseWriter, r *http.Request) { + json.NewEncoder(w).Encode(map[string]any{ + "erigonURL": cfg.OtsRpcDaemonUrl, + "beaconAPI": cfg.OtsBeaconApiUrl, + "assetsURLPrefix": cfg.OtsExternalAssetUrl, }) - FileServer(r, "/", filesDir) }) -} -// FileServer conveniently sets up a http.FileServer handler to serve -// static files from a http.FileSystem. -func FileServer(r chi.Router, path string, root http.FileSystem) { - if strings.ContainsAny(path, "{}*") { - panic("FileServer does not permit any URL parameters.") - } - if path != "/" && path[len(path)-1] != '/' { - r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP) - path += "/" - } - path += "*" - r.Get(path, func(w http.ResponseWriter, r *http.Request) { - rctx := chi.RouteContext(r.Context()) - pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*") - fs := http.StripPrefix(pathPrefix, http.FileServer(root)) - fs.ServeHTTP(w, r) + filesDir := http.Dir(cfg.OtsStaticDir) + fileServer := http.FileServer(filesDir) + r.Handle("/*", http.StripPrefix("/", fileServer)) + + r.Get("/*", func(w http.ResponseWriter, r *http.Request) { + if _, err := os.Stat(cfg.OtsStaticDir + r.RequestURI); os.IsNotExist(err) { + http.StripPrefix(r.RequestURI, fileServer).ServeHTTP(w, r) + } else { + fileServer.ServeHTTP(w, r) + } }) } diff --git a/src/App.tsx b/src/App.tsx index a6417a5..bb8d9aa 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,5 +1,5 @@ import React, { Suspense } from "react"; -import { HashRouter as Router, Routes, Route } from "react-router-dom"; +import { BrowserRouter as Router, Routes, Route } from "react-router-dom"; import WarningHeader from "./WarningHeader"; import Home from "./Home"; import Main from "./Main";