diff --git a/cmd/server/main.go b/cmd/server/main.go index 2927731..f4bcd17 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -26,6 +26,7 @@ import ( logs "github.com/c4t-but-s4d/neo/v2/internal/server/logs" "github.com/c4t-but-s4d/neo/v2/pkg/grpcauth" "github.com/c4t-but-s4d/neo/v2/pkg/mu" + "github.com/c4t-but-s4d/neo/v2/pkg/neohttp" "github.com/c4t-but-s4d/neo/v2/pkg/neosync" epb "github.com/c4t-but-s4d/neo/v2/proto/go/exploits" fspb "github.com/c4t-but-s4d/neo/v2/proto/go/fileserver" @@ -89,9 +90,7 @@ func main() { httpMux := http.NewServeMux() httpMux.Handle("/metrics", promhttp.Handler()) - - staticFS := http.FileServer(http.Dir(cfg.StaticDir)) - httpMux.Handle("/", staticFS) + httpMux.Handle("/", neohttp.StaticHandler(cfg.StaticDir)) muHandler := mu.NewHandler(s, mu.WithHTTPHandler(httpMux)) httpServer := &http.Server{ diff --git a/pkg/neohttp/static.go b/pkg/neohttp/static.go new file mode 100644 index 0000000..5c858c0 --- /dev/null +++ b/pkg/neohttp/static.go @@ -0,0 +1,21 @@ +package neohttp + +import ( + "net/http" + "os" + "path/filepath" +) + +func StaticHandler(dir string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + filePath := filepath.Join(dir, r.URL.Path) + + if r.URL.Path == "" || r.URL.Path == "/" { + filePath = filepath.Join(dir, "index.html") + } else if _, err := os.Stat(filePath); err != nil { + filePath = filepath.Join(dir, "index.html") + } + + http.ServeFile(w, r, filePath) + } +}