embed-file-system.go 554 B

1234567891011121314151617181920212223242526272829303132
  1. package common
  2. import (
  3. "embed"
  4. "github.com/gin-contrib/static"
  5. "io/fs"
  6. "net/http"
  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. }