Skip to content

Commit

Permalink
Merge pull request #11 from SlyngDK/frontend
Browse files Browse the repository at this point in the history
Fixed handling of not found for frontend routing
  • Loading branch information
SlyngDK authored Nov 15, 2021
2 parents a022f08 + 42653f8 commit 9bdd7e7
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions webapp/webapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"embed"
"io/fs"
"net/http"
"os"
"path"
"strings"

"github.com/sirupsen/logrus"
)
Expand All @@ -15,11 +18,20 @@ import (
var Webapp embed.FS

func WebHandler(l *logrus.Logger) func(w http.ResponseWriter, r *http.Request) {
wa, err := fs.Sub(Webapp, "dist")
if err != nil {
l.WithError(err).Error("Failed to load webapp from fs")
}

return func(w http.ResponseWriter, r *http.Request) {
wa, err := fs.Sub(Webapp, "dist")
if err != nil {
l.WithError(err).Error("Failed to load webapp from fs")
// Support frontend routing
if r.URL.Path != "/" {
_, err := fs.Stat(wa, strings.TrimPrefix(path.Clean(r.URL.Path), "/"))
if err != nil {
if os.IsNotExist(err) {
r.URL.Path = "/"
}
}
}
http.FileServer(http.FS(wa)).ServeHTTP(w, r)
}
Expand Down

0 comments on commit 9bdd7e7

Please sign in to comment.