feat: serve files using embed.FS (#4847)

This commit is contained in:
shaouai
2025-05-10 23:43:13 +08:00
committed by GitHub
parent e55158b0f7
commit 5564c43197
4 changed files with 179 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ package fileserver
import (
"net/http"
pathpkg "path"
"strings"
"sync"
)
@@ -29,6 +30,18 @@ func createFileChecker(fs http.FileSystem) func(string) bool {
fileChecker := make(map[string]bool)
return func(path string) bool {
// Emulate http.Dir.Opens path normalization for embed.FS.Open.
// http.FileServer redirects any request ending in "/index.html"
// to the same path without the final "index.html".
// So the path here may be empty or end with a "/".
// http.Dir.Open uses this logic to clean the path,
// correctly handling those two cases.
// embed.FS doesnt perform this normalization, so we apply the same logic here.
path = pathpkg.Clean("/" + path)[1:]
if path == "" {
path = "."
}
lock.RLock()
exist, ok := fileChecker[path]
lock.RUnlock()