mirror of
https://github.com/priyanshujain/sanderling.git
synced 2026-10-02 19:17:10 +00:00
fix(inspect): allow AssetsFS override in ServerOptions; drop unused request param from serveIndex
This commit is contained in:
1 parent
a8fd4de1af
commit
474114976e
1 file changed
+10
-4
@@ -18,6 +18,8 @@ import (
|
|||||||
type ServerOptions struct {
|
type ServerOptions struct {
|
||||||
RunsDirectory string
|
RunsDirectory string
|
||||||
DevTarget string
|
DevTarget string
|
||||||
|
// AssetsFS overrides the default embedded dist FS. Intended for tests.
|
||||||
|
AssetsFS fs.FS
|
||||||
}
|
}
|
||||||
|
|
||||||
// Server holds the HTTP handlers for `sanderling inspect`.
|
// Server holds the HTTP handlers for `sanderling inspect`.
|
||||||
@@ -33,11 +35,15 @@ type Server struct {
|
|||||||
// server reverse-proxies non-API GETs to it; otherwise it serves embedded
|
// server reverse-proxies non-API GETs to it; otherwise it serves embedded
|
||||||
// assets from the dist FS.
|
// assets from the dist FS.
|
||||||
func NewServer(options ServerOptions) (*Server, error) {
|
func NewServer(options ServerOptions) (*Server, error) {
|
||||||
|
assetsFS := options.AssetsFS
|
||||||
|
if assetsFS == nil {
|
||||||
|
assetsFS = Assets()
|
||||||
|
}
|
||||||
server := &Server{
|
server := &Server{
|
||||||
options: options,
|
options: options,
|
||||||
cache: NewCache(options.RunsDirectory),
|
cache: NewCache(options.RunsDirectory),
|
||||||
watcher: NewWatcher(options.RunsDirectory),
|
watcher: NewWatcher(options.RunsDirectory),
|
||||||
assets: spaHandler(Assets()),
|
assets: spaHandler(assetsFS),
|
||||||
}
|
}
|
||||||
if options.DevTarget != "" {
|
if options.DevTarget != "" {
|
||||||
proxy, err := newDevProxy(options.DevTarget)
|
proxy, err := newDevProxy(options.DevTarget)
|
||||||
@@ -207,12 +213,12 @@ func spaHandler(assets fs.FS) http.Handler {
|
|||||||
return http.HandlerFunc(func(responseWriter http.ResponseWriter, request *http.Request) {
|
return http.HandlerFunc(func(responseWriter http.ResponseWriter, request *http.Request) {
|
||||||
clean := strings.TrimPrefix(path.Clean(request.URL.Path), "/")
|
clean := strings.TrimPrefix(path.Clean(request.URL.Path), "/")
|
||||||
if clean == "" {
|
if clean == "" {
|
||||||
serveIndex(responseWriter, request, assets)
|
serveIndex(responseWriter, assets)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
file, err := assets.Open(clean)
|
file, err := assets.Open(clean)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serveIndex(responseWriter, request, assets)
|
serveIndex(responseWriter, assets)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
file.Close()
|
file.Close()
|
||||||
@@ -220,7 +226,7 @@ func spaHandler(assets fs.FS) http.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveIndex(responseWriter http.ResponseWriter, request *http.Request, assets fs.FS) {
|
func serveIndex(responseWriter http.ResponseWriter, assets fs.FS) {
|
||||||
file, err := assets.Open("index.html")
|
file, err := assets.Open("index.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(responseWriter, "index.html missing from embedded assets", http.StatusInternalServerError)
|
http.Error(responseWriter, "index.html missing from embedded assets", http.StatusInternalServerError)
|
||||||
|
|||||||
Reference in new issue
Block a user