Skip to content

Commit

Permalink
Fix static handler
Browse files Browse the repository at this point in the history
  • Loading branch information
pomo-mondreganto committed Nov 17, 2023
1 parent 1170730 commit c0ce829
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
5 changes: 2 additions & 3 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down
21 changes: 21 additions & 0 deletions pkg/neohttp/static.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit c0ce829

Please sign in to comment.