mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-01-19 11:15:53 +08:00
Serve static contents from ui/build directory
This commit is contained in:
parent
14dc7de869
commit
44f893ad35
53
main.go
53
main.go
@ -1,7 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -19,42 +21,44 @@ import (
|
|||||||
// path to the index file within that static directory are used to
|
// path to the index file within that static directory are used to
|
||||||
// serve the SPA in the given static directory.
|
// serve the SPA in the given static directory.
|
||||||
type staticFileServer struct {
|
type staticFileServer struct {
|
||||||
staticPath string
|
staticContents embed.FS
|
||||||
indexPath string
|
staticDirPath string
|
||||||
|
indexFileName string
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeHTTP inspects the URL path to locate a file within the static dir
|
// ServeHTTP inspects the URL path to locate a file within the static dir
|
||||||
// on the SPA handler. If a file is found, it will be served. If not, the
|
// on the SPA handler.
|
||||||
// file located at the index path on the SPA handler will be served. This
|
// If path '/' is requested, it will serve the index file, otherwise it will
|
||||||
// is suitable behavior for serving an SPA (single page application).
|
// serve the file specified by the URL path.
|
||||||
func (srv *staticFileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (srv *staticFileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
// get the absolute path to prevent directory traversal
|
// Get the absolute path to prevent directory traversal.
|
||||||
path, err := filepath.Abs(r.URL.Path)
|
path, err := filepath.Abs(r.URL.Path)
|
||||||
if err != nil {
|
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)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepend the path with the path to the static directory
|
if path == "/" {
|
||||||
path = filepath.Join(srv.staticPath, path)
|
path = filepath.Join(srv.staticDirPath, srv.indexFileName)
|
||||||
|
} else {
|
||||||
|
path = filepath.Join(srv.staticDirPath, path)
|
||||||
|
}
|
||||||
|
|
||||||
// check whether a file exists at the given path
|
f, err := srv.staticContents.Open(path)
|
||||||
_, err = os.Stat(path)
|
if err != nil {
|
||||||
|
status := http.StatusInternalServerError
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
// file does not exist, serve index.html
|
status = http.StatusNotFound
|
||||||
http.ServeFile(w, r, filepath.Join(srv.staticPath, srv.indexPath))
|
}
|
||||||
|
http.Error(w, err.Error(), status)
|
||||||
return
|
return
|
||||||
} else if err != nil {
|
}
|
||||||
// if we got an error (that wasn't that the file doesn't exist) stating the
|
defer f.Close()
|
||||||
// file, return a 500 internal server error and stop
|
|
||||||
|
if _, err := io.Copy(w, f); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// otherwise, use http.FileServer to serve the static dir
|
|
||||||
http.FileServer(http.Dir(srv.staticPath)).ServeHTTP(w, r)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -62,6 +66,9 @@ const (
|
|||||||
redisAddr = "localhost:6379" // TODO: make this configurable
|
redisAddr = "localhost:6379" // TODO: make this configurable
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed ui/build/*
|
||||||
|
var staticContents embed.FS
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
inspector := asynq.NewInspector(asynq.RedisClientOpt{
|
inspector := asynq.NewInspector(asynq.RedisClientOpt{
|
||||||
Addr: redisAddr,
|
Addr: redisAddr,
|
||||||
@ -131,7 +138,11 @@ func main() {
|
|||||||
// Redis info endpoint.
|
// Redis info endpoint.
|
||||||
api.HandleFunc("/redis_info", newRedisInfoHandlerFunc(rdb)).Methods("GET")
|
api.HandleFunc("/redis_info", newRedisInfoHandlerFunc(rdb)).Methods("GET")
|
||||||
|
|
||||||
fs := &staticFileServer{staticPath: "ui/build", indexPath: "index.html"}
|
fs := &staticFileServer{
|
||||||
|
staticContents: staticContents,
|
||||||
|
staticDirPath: "ui/build",
|
||||||
|
indexFileName: "index.html",
|
||||||
|
}
|
||||||
router.PathPrefix("/").Handler(fs)
|
router.PathPrefix("/").Handler(fs)
|
||||||
|
|
||||||
c := cors.New(cors.Options{
|
c := cors.New(cors.Options{
|
||||||
|
Loading…
Reference in New Issue
Block a user