-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix CI, update README and change build tag option
- Loading branch information
1 parent
d08a322
commit a1efa14
Showing
5 changed files
with
89 additions
and
87 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
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,43 +1,61 @@ | ||
//go:build !webapp | ||
// +build !webapp | ||
//go:build !apiserver | ||
// +build !apiserver | ||
|
||
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 | ||
} | ||
|
||
// no longer used, will be helpful in Zasperhub in future when running api only server | ||
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
// 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)) | ||
// 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 | ||
} | ||
|
||
if err != nil { | ||
// 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 | ||
} | ||
|
||
// otherwise, use http.FileServer to serve the static file | ||
http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r) | ||
// 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{staticPath: "./ui/build", indexPath: "index.html"} | ||
return spaHandler{staticFS: staticFiles, staticPath: "ui/build", indexPath: "index.html"} | ||
} |
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,42 @@ | ||
//go:build apiserver | ||
// +build apiserver | ||
|
||
package main | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type spaHandler struct { | ||
staticPath string | ||
indexPath string | ||
} | ||
|
||
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
// 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 | ||
} | ||
|
||
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 | ||
} | ||
|
||
// otherwise, use http.FileServer to serve the static file | ||
http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r) | ||
} | ||
|
||
func getSpaHandler() http.Handler { | ||
return spaHandler{staticPath: "./ui/build", indexPath: "index.html"} | ||
} |
This file was deleted.
Oops, something went wrong.