mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-08-24 06:38:42 +08:00
remove NewStaticContentHandlerFunc
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"embed"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -107,21 +108,25 @@ func main() {
|
||||
|
||||
h := asynqmon.New(asynqmon.Options{
|
||||
RedisConnOpt: redisConnOpt,
|
||||
Middlewares: []asynqmon.MiddlewareFunc{loggingMiddleware},
|
||||
StaticContentHandler: asynqmon.NewStaticContentHandler(
|
||||
staticContents,
|
||||
"ui-assets",
|
||||
"index.html",
|
||||
),
|
||||
})
|
||||
defer h.Close()
|
||||
|
||||
r := mux.NewRouter()
|
||||
r.PathPrefix("/api").Handler(h)
|
||||
r.PathPrefix("/").Handler(&staticContentHandler{
|
||||
contents: staticContents,
|
||||
staticDirPath: "ui-assets",
|
||||
indexFileName: "index.html",
|
||||
})
|
||||
|
||||
r.Use(loggingMiddleware)
|
||||
|
||||
c := cors.New(cors.Options{
|
||||
AllowedMethods: []string{"GET", "POST", "DELETE"},
|
||||
})
|
||||
|
||||
srv := &http.Server{
|
||||
Handler: c.Handler(h),
|
||||
Handler: c.Handler(r),
|
||||
Addr: fmt.Sprintf(":%d", flagPort),
|
||||
WriteTimeout: 10 * time.Second,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
|
58
cmd/asynqmon/static.go
Normal file
58
cmd/asynqmon/static.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"errors"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// staticFileServer implements the http.Handler interface, so we can use it
|
||||
// to respond to HTTP requests. The path to the static directory and
|
||||
// path to the index file within that static directory are used to
|
||||
// serve the SPA in the given static directory.
|
||||
type staticContentHandler struct {
|
||||
contents embed.FS
|
||||
staticDirPath string
|
||||
indexFileName string
|
||||
}
|
||||
|
||||
// ServeHTTP inspects the URL path to locate a file within the static dir
|
||||
// on the SPA handler.
|
||||
// If path '/' is requested, it will serve the index file, otherwise it will
|
||||
// serve the file specified by the URL path.
|
||||
func (h *staticContentHandler) 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 {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if path == "/" {
|
||||
path = h.indexFilePath()
|
||||
} else {
|
||||
path = filepath.Join(h.staticDirPath, path)
|
||||
}
|
||||
|
||||
bytes, err := h.contents.ReadFile(path)
|
||||
// If path is error (e.g. file not exist, path is a directory), serve index file.
|
||||
var pathErr *fs.PathError
|
||||
if errors.As(err, &pathErr) {
|
||||
bytes, err = h.contents.ReadFile(h.indexFilePath())
|
||||
}
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := w.Write(bytes); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (h *staticContentHandler) indexFilePath() string {
|
||||
return filepath.Join(h.staticDirPath, h.indexFileName)
|
||||
}
|
Reference in New Issue
Block a user