redo server routing to be simpler
This commit is contained in:
parent
9807b21fd3
commit
9f2de2de97
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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";
|
||||
|
|
Loading…
Reference in New Issue