otterscan/cmd/otter/server.go

52 lines
1.5 KiB
Go
Raw Normal View History

2022-10-25 10:20:17 +00:00
package main
import (
"encoding/json"
"net/http"
"strings"
"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"
"github.com/wmitsuda/otterscan/cmd/otter/cli/httpcfg"
)
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)
2022-10-25 12:32:28 +00:00
r.HandleFunc("/signatures/{hash}", triemap.HttpHandler(sigs.Both))
r.HandleFunc("/topic0/{hash}", triemap.HttpHandler(topics.Both))
2022-10-25 10:20:17 +00:00
r.HandleFunc("/config.json", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]any{
"erigonURL": cfg.OtsRpcDaemonUrl,
"beaconAPI": cfg.OtsBeaconApiUrl,
2022-10-25 10:36:43 +00:00
"assetsURLPrefix": cfg.OtsExternalAssetUrl,
2022-10-25 10:20:17 +00:00
})
})
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)
})
}