2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2020-09-23 11:37:33 +00:00
|
|
|
package uiserver
|
|
|
|
|
2022-05-31 19:33:56 +00:00
|
|
|
import (
|
|
|
|
"io/fs"
|
|
|
|
)
|
2020-09-23 11:37:33 +00:00
|
|
|
|
2022-05-31 19:33:56 +00:00
|
|
|
// redirectFS is an fs.FS that serves the index.html file for any path that is
|
2020-09-23 11:37:33 +00:00
|
|
|
// not found on the underlying FS.
|
|
|
|
//
|
|
|
|
// TODO: it seems better to actually 404 bad paths or at least redirect them
|
|
|
|
// rather than pretend index.html is everywhere but this is behavior changing
|
|
|
|
// so I don't want to take it on as part of this refactor.
|
|
|
|
type redirectFS struct {
|
2022-05-31 19:33:56 +00:00
|
|
|
fs fs.FS
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 19:33:56 +00:00
|
|
|
func (fs *redirectFS) Open(name string) (fs.File, error) {
|
2020-09-23 11:37:33 +00:00
|
|
|
file, err := fs.fs.Open(name)
|
|
|
|
if err != nil {
|
2022-05-31 19:33:56 +00:00
|
|
|
file, err = fs.fs.Open("index.html")
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
return file, err
|
|
|
|
}
|