otterscan/cmd/otter/server.go

51 lines
1.5 KiB
Go
Raw Normal View History

2022-10-25 10:20:17 +00:00
package main
import (
"encoding/json"
"net/http"
2022-10-25 21:54:28 +00:00
"os"
2022-10-25 10:20:17 +00:00
"gfx.cafe/open/4bytes/sigs"
2022-10-25 12:32:28 +00:00
"gfx.cafe/open/4bytes/topics"
"gfx.cafe/open/4bytes/triemap"
2022-10-25 10:20:17 +00:00
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
2022-10-26 03:43:00 +00:00
"github.com/ledgerwatch/erigon/common/debug"
2022-10-26 03:26:11 +00:00
"github.com/wmitsuda/otterscan/lib/resources"
2022-10-25 10:20:17 +00:00
)
2022-11-15 03:22:15 +00:00
func RouteServer(r chi.Router, cfg config) {
r.Group(func(r chi.Router) {
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
2022-10-26 03:26:11 +00:00
// host the apis
r.HandleFunc("/signatures/{hash}", triemap.HttpHandler(sigs.Both))
r.HandleFunc("/topic0/{hash}", triemap.HttpHandler(topics.Both))
r.Handle("/chains/{chainId}", http.FileServer(resources.ChainsServer))
r.Handle("/tokens/{chainId}/{address}/logo.png", http.FileServer(resources.AssetsServer))
r.HandleFunc("/memstats", func(w http.ResponseWriter, r *http.Request) {
debug.PrintMemStats(false)
})
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.OtsAssetUrl,
})
})
// host the static site. (dist)
fileServer := http.FileServer(http.Dir(cfg.OtsStaticDir))
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)
}
2022-10-25 10:20:17 +00:00
})
})
}