-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d24fab
commit 565de4a
Showing
4 changed files
with
79 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,42 @@ | ||
//go:build webapp | ||
// +build webapp | ||
//go:build !webapp | ||
// +build !webapp | ||
|
||
package main | ||
|
||
import ( | ||
"embed" | ||
"io/fs" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
//go:embed ui/build/* | ||
var staticFiles embed.FS | ||
|
||
type spaHandler struct { | ||
staticFS embed.FS | ||
staticPath string | ||
indexPath string | ||
} | ||
|
||
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
// get the absolute path to prevent directory traversal | ||
path, err := filepath.Abs(r.URL.Path) | ||
if err != nil { | ||
// if we failed to get the absolute path respond with a 400 bad request and stop | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
// Join internally call path.Clean to prevent directory traversal | ||
path := filepath.Join(h.staticPath, r.URL.Path) | ||
|
||
// check whether a file exists or is a directory at the given path | ||
fi, err := os.Stat(path) | ||
if os.IsNotExist(err) || fi.IsDir() { | ||
// file does not exist or path is a directory, serve index.html | ||
http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath)) | ||
return | ||
} | ||
// prepend the path with the path to the static directory | ||
path = filepath.Join(h.staticPath, path) | ||
|
||
_, err = h.staticFS.Open(path) | ||
if os.IsNotExist(err) { | ||
// file does not exist, serve index.html | ||
index, err := h.staticFS.ReadFile(filepath.Join(h.staticPath, h.indexPath)) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
w.Header().Set("Content-Type", "text/html; charset=utf-8") | ||
w.WriteHeader(http.StatusAccepted) | ||
w.Write(index) | ||
return | ||
} else if err != nil { | ||
|
||
if err != nil { | ||
// if we got an error (that wasn't that the file doesn't exist) stating the | ||
// file, return a 500 internal server error and stop | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// get the subdirectory of the static dir | ||
statics, err := fs.Sub(h.staticFS, h.staticPath) | ||
// otherwise, use http.FileServer to serve the static dir | ||
http.FileServer(http.FS(statics)).ServeHTTP(w, r) | ||
// otherwise, use http.FileServer to serve the static file | ||
http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r) | ||
} | ||
|
||
func getSpaHandler() http.Handler { | ||
return spaHandler{staticFS: staticFiles, staticPath: "ui/build", indexPath: "index.html"} | ||
return spaHandler{staticPath: "./ui/build", indexPath: "index.html"} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//go:build webapp | ||
// +build webapp | ||
|
||
package main | ||
|
||
import ( | ||
"embed" | ||
"io/fs" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
//go:embed ui/build/* | ||
var staticFiles embed.FS | ||
|
||
type spaHandler struct { | ||
staticFS embed.FS | ||
staticPath string | ||
indexPath string | ||
} | ||
|
||
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
// get the absolute path to prevent directory traversal | ||
path, err := filepath.Abs(r.URL.Path) | ||
if err != nil { | ||
// if we failed to get the absolute path respond with a 400 bad request and stop | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
// prepend the path with the path to the static directory | ||
path = filepath.Join(h.staticPath, path) | ||
|
||
_, err = h.staticFS.Open(path) | ||
if os.IsNotExist(err) { | ||
// file does not exist, serve index.html | ||
index, err := h.staticFS.ReadFile(filepath.Join(h.staticPath, h.indexPath)) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
w.Header().Set("Content-Type", "text/html; charset=utf-8") | ||
w.WriteHeader(http.StatusAccepted) | ||
w.Write(index) | ||
return | ||
} else if err != nil { | ||
// if we got an error (that wasn't that the file doesn't exist) stating the | ||
// file, return a 500 internal server error and stop | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// get the subdirectory of the static dir | ||
statics, err := fs.Sub(h.staticFS, h.staticPath) | ||
// otherwise, use http.FileServer to serve the static dir | ||
http.FileServer(http.FS(statics)).ServeHTTP(w, r) | ||
} | ||
|
||
func getSpaHandler() http.Handler { | ||
return spaHandler{staticFS: staticFiles, staticPath: "ui/build", indexPath: "index.html"} | ||
} |