mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-10-03 02:32:00 +08:00
Merge 8023113044
into d1b889456d
This commit is contained in:
19
static.go
19
static.go
@@ -6,7 +6,7 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -28,12 +28,7 @@ type uiAssetsHandler struct {
|
|||||||
// If path '/' is requested, it will serve the index file, otherwise it will
|
// If path '/' is requested, it will serve the index file, otherwise it will
|
||||||
// serve the file specified by the URL path.
|
// serve the file specified by the URL path.
|
||||||
func (h *uiAssetsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *uiAssetsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
// Get the absolute path to prevent directory traversal.
|
path := r.URL.Path
|
||||||
path, err := filepath.Abs(r.URL.Path)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the path relative to the root path.
|
// Get the path relative to the root path.
|
||||||
if !strings.HasPrefix(path, h.rootPath) {
|
if !strings.HasPrefix(path, h.rootPath) {
|
||||||
@@ -49,7 +44,7 @@ func (h *uiAssetsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *uiAssetsHandler) indexFilePath() string {
|
func (h *uiAssetsHandler) indexFilePath() string {
|
||||||
return filepath.Join(h.staticDirPath, h.indexFileName)
|
return path.Join(h.staticDirPath, h.indexFileName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *uiAssetsHandler) renderIndexFile(w http.ResponseWriter) error {
|
func (h *uiAssetsHandler) renderIndexFile(w http.ResponseWriter) error {
|
||||||
@@ -78,15 +73,15 @@ func (h *uiAssetsHandler) renderIndexFile(w http.ResponseWriter) error {
|
|||||||
// and serves if a file is found.
|
// and serves if a file is found.
|
||||||
// If a requested file is not found in the filesystem, it serves the index file to
|
// If a requested file is not found in the filesystem, it serves the index file to
|
||||||
// make sure when user refreshes the page in SPA things still work.
|
// make sure when user refreshes the page in SPA things still work.
|
||||||
func (h *uiAssetsHandler) serveFile(w http.ResponseWriter, path string) (code int, err error) {
|
func (h *uiAssetsHandler) serveFile(w http.ResponseWriter, p string) (code int, err error) {
|
||||||
if path == "/" || path == "" {
|
if p == "/" || p == "" {
|
||||||
if err := h.renderIndexFile(w); err != nil {
|
if err := h.renderIndexFile(w); err != nil {
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
return http.StatusOK, nil
|
return http.StatusOK, nil
|
||||||
}
|
}
|
||||||
path = filepath.Join(h.staticDirPath, path)
|
p = path.Join(h.staticDirPath, p)
|
||||||
bytes, err := h.contents.ReadFile(path)
|
bytes, err := h.contents.ReadFile(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If path is error (e.g. file not exist, path is a directory), serve index file.
|
// If path is error (e.g. file not exist, path is a directory), serve index file.
|
||||||
var pathErr *fs.PathError
|
var pathErr *fs.PathError
|
||||||
|
Reference in New Issue
Block a user