Skip to content

Commit

Permalink
refactor routing to support a wildcard path at root for rollupName
Browse files Browse the repository at this point in the history
  • Loading branch information
steezeburger committed Jan 19, 2024
1 parent 446bee2 commit fa495fb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
31 changes: 23 additions & 8 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions web/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import { svelte } from '@sveltejs/vite-plugin-svelte'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [svelte()],
base: 'static_assets',
})

0 comments on commit fa495fb

Please sign in to comment.