embed-file-system.go 555 B

123456789101112131415161718192021222324252627282930313233
  1. package common
  2. import (
  3. "embed"
  4. "io/fs"
  5. "net/http"
  6. "github.com/gin-contrib/static"
  7. )
  8. // Credit: https://github.com/gin-contrib/static/issues/19
  9. type embedFileSystem struct {
  10. http.FileSystem
  11. }
  12. func (e embedFileSystem) Exists(prefix string, path string) bool {
  13. _, err := e.Open(path)
  14. if err != nil {
  15. return false
  16. }
  17. return true
  18. }
  19. func EmbedFolder(fsEmbed embed.FS, targetPath string) static.ServeFileSystem {
  20. efs, err := fs.Sub(fsEmbed, targetPath)
  21. if err != nil {
  22. panic(err)
  23. }
  24. return embedFileSystem{
  25. FileSystem: http.FS(efs),
  26. }
  27. }