From fa495fbfb1fb86188629a6f33322974549276f0b Mon Sep 17 00:00:00 2001 From: Jesse Snyder Date: Thu, 18 Jan 2024 18:50:00 -0700 Subject: [PATCH] refactor routing to support a wildcard path at root for rollupName --- go.mod | 1 + go.sum | 2 ++ internal/server/server.go | 31 +++++++++++++++++++++++-------- web/vite.config.js | 1 + 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index d0afffe..d848740 100644 --- a/go.mod +++ b/go.mod @@ -34,6 +34,7 @@ require ( github.com/google/uuid v1.3.0 // indirect github.com/googleapis/enterprise-certificate-proxy v0.2.4 // indirect github.com/googleapis/gax-go/v2 v2.12.0 // indirect + github.com/gorilla/mux v1.8.1 // indirect github.com/gorilla/websocket v1.4.2 // indirect github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect github.com/holiman/bloomfilter/v2 v2.0.3 // indirect diff --git a/go.sum b/go.sum index 25d51d9..defdb8c 100644 --- a/go.sum +++ b/go.sum @@ -129,6 +129,8 @@ github.com/googleapis/enterprise-certificate-proxy v0.2.4/go.mod h1:AwSRAtLfXpU5 github.com/googleapis/gax-go/v2 v2.12.0 h1:A+gCJKdRfqXkr+BIRGtZLibNXf0m1f9E4HG56etFpas= github.com/googleapis/gax-go/v2 v2.12.0/go.mod h1:y+aIqrI5eb1YGMVJfuV3185Ts/D7qKpsEkdD5+I6QGU= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= diff --git a/internal/server/server.go b/internal/server/server.go index 8bbd9b7..36c1a47 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -8,10 +8,10 @@ import ( "strconv" "time" + "github.com/LK4D4/trylock" "github.com/astriaorg/eth-faucet/internal/store" "github.com/ethereum/go-ethereum/crypto" - - "github.com/LK4D4/trylock" + "github.com/gorilla/mux" log "github.com/sirupsen/logrus" "github.com/urfave/negroni" @@ -34,14 +34,29 @@ func NewServer(sm store.RollupStoreManager, cfg *Config) *Server { } } -func (s *Server) setupRouter() *http.ServeMux { - router := http.NewServeMux() - router.Handle("/", http.FileServer(web.Dist())) +func (s *Server) setupRouter() *mux.Router { + r := mux.NewRouter() + + api := r.PathPrefix("/api").Subrouter() limiter := NewLimiter(s.cfg.proxyCount, time.Duration(s.cfg.interval)*time.Minute) - router.Handle("/api/claim", negroni.New(limiter, negroni.Wrap(s.handleClaim()))) - router.Handle("/api/info", s.handleInfo()) + api.Handle("/claim", negroni.New(limiter, negroni.Wrap(s.handleClaim()))) + api.Handle("/info", s.handleInfo()) + + fs := http.FileServer(web.Dist()) + + // NOTE - serving static files from /static allows us to handle wildcard routes properly. + // requires vite.config.js `base` property to be set to the same pattern as below. + // NOTE - rollup names don't support `_`, so using uncommon name `static_assets` + r.PathPrefix("/static_assets").Handler(http.StripPrefix("/static_assets", fs)) + + // serve the svelte app, via the filesystem, for any other route + r.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // NOTE - http.FS has the same paths as the filesystem. if we didn't strip + // the path, then it would try to find a file with the same name as the path + http.StripPrefix(r.URL.Path, fs).ServeHTTP(w, r) + }) - return router + return r } func (s *Server) Run() { diff --git a/web/vite.config.js b/web/vite.config.js index d701969..bf248f9 100644 --- a/web/vite.config.js +++ b/web/vite.config.js @@ -4,4 +4,5 @@ import { svelte } from '@sveltejs/vite-plugin-svelte' // https://vitejs.dev/config/ export default defineConfig({ plugins: [svelte()], + base: 'static_assets', })