mirror of
https://github.com/cnrancher/kube-explorer.git
synced 2025-07-12 05:58:32 +00:00
- Support embed api-ui resources - The ui-path arg will be applied if provided. Also applied to api-ui resource files
44 lines
948 B
Go
44 lines
948 B
Go
package content
|
|
|
|
import (
|
|
"embed"
|
|
"io/fs"
|
|
"net/http"
|
|
"path/filepath"
|
|
)
|
|
|
|
func NewEmbedded(staticContent embed.FS, prefix string) Handler {
|
|
return newFS(&embedFS{
|
|
pathPrefix: prefix,
|
|
staticContent: staticContent,
|
|
})
|
|
}
|
|
|
|
var _ fsContent = &embedFS{}
|
|
|
|
type embedFS struct {
|
|
pathPrefix string
|
|
staticContent embed.FS
|
|
}
|
|
|
|
// Open implements fsContent.
|
|
func (e *embedFS) Open(name string) (fs.File, error) {
|
|
return e.staticContent.Open(joinEmbedFilepath(e.pathPrefix, name))
|
|
}
|
|
|
|
// ToFileServer implements fsContent.
|
|
func (e *embedFS) ToFileServer(basePaths ...string) http.Handler {
|
|
handler := fsFunc(func(name string) (fs.File, error) {
|
|
assetPath := joinEmbedFilepath(joinEmbedFilepath(basePaths...), name)
|
|
return e.Open(assetPath)
|
|
})
|
|
|
|
return http.FileServer(http.FS(handler))
|
|
}
|
|
|
|
func (e *embedFS) Refresh() error { return nil }
|
|
|
|
func joinEmbedFilepath(paths ...string) string {
|
|
return filepath.ToSlash(filepath.Join(paths...))
|
|
}
|