Skip to content

Commit 09c188b

Browse files
authored
docs: improve error handling and documentation for EmbedFolder function (#59)
- Add error handling for the `EmbedFolder` function - Add comments explaining the `embedFileSystem` struct and its `Exists` method - Add imports for `errors` and `strings` packages - Update `TestEmbedFolder` to handle errors from `EmbedFolder` function - Add comments in `embed_folder_test.go` in Chinese for test case explanations Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent aff1a9e commit 09c188b

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

embed_folder.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,41 @@ package static
22

33
import (
44
"embed"
5+
"errors"
56
"io/fs"
67
"log/slog"
78
"net/http"
9+
"strings"
810
)
911

12+
// embedFileSystem is a struct that implements the http.FileSystem interface for embedding a file system.
1013
type embedFileSystem struct {
1114
http.FileSystem
1215
}
1316

17+
// Exists method checks if the given file path exists in the embedded file system.
18+
// If the path exists, it returns true; otherwise, it returns false.
1419
func (e embedFileSystem) Exists(prefix string, path string) bool {
20+
if len(prefix) > 1 && strings.HasPrefix(path, prefix) {
21+
path = strings.TrimPrefix(path, prefix)
22+
}
23+
1524
_, err := e.Open(path)
1625
return err == nil
1726
}
1827

19-
func EmbedFolder(fsEmbed embed.FS, targetPath string) ServeFileSystem {
28+
// EmbedFolder function embeds the target folder from the embedded file system into the ServeFileSystem.
29+
// If an error occurs during the embedding process, it returns the error message.
30+
func EmbedFolder(fsEmbed embed.FS, targetPath string) (ServeFileSystem, error) {
2031
fsys, err := fs.Sub(fsEmbed, targetPath)
2132
if err != nil {
2233
slog.Error("Failed to embed folder",
2334
"targetPath", targetPath,
2435
"error", err,
2536
)
26-
return nil
37+
return nil, errors.New("failed to embed folder: " + err.Error())
2738
}
2839
return embedFileSystem{
2940
FileSystem: http.FS(fsys),
30-
}
41+
}, nil
3142
}

embed_folder_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
//go:embed test/data/server
1313
var server embed.FS
1414

15+
// embedTests 定義了一組測試用例,用於測試嵌入文件系統的行為。
1516
var embedTests = []struct {
1617
targetURL string // input
1718
httpCode int // expected http code
@@ -24,9 +25,14 @@ var embedTests = []struct {
2425
{"/static.html", 200, "<h1>Hello Gin Static</h1>", "Other file"},
2526
}
2627

28+
// TestEmbedFolder 測試 EmbedFolder 函數的行為,確保其能夠正確嵌入文件夾並提供靜態文件服務。
2729
func TestEmbedFolder(t *testing.T) {
2830
router := gin.New()
29-
router.Use(Serve("/", EmbedFolder(server, "test/data/server")))
31+
fs, err := EmbedFolder(server, "test/data/server")
32+
if err != nil {
33+
t.Fatalf("Failed to embed folder: %v", err)
34+
}
35+
router.Use(Serve("/", fs))
3036
router.NoRoute(func(c *gin.Context) {
3137
fmt.Printf("%s doesn't exists, redirect on /\n", c.Request.URL.Path)
3238
c.Redirect(301, "/")

0 commit comments

Comments
 (0)