assets.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2016 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package assets
  15. import (
  16. "io/fs"
  17. "net/http"
  18. )
  19. var (
  20. // read-only filesystem created by "embed" for embedded files
  21. content fs.FS
  22. FileSystem http.FileSystem
  23. // if prefix is not empty, we get file content from disk
  24. prefixPath string
  25. )
  26. // if path is empty, load assets in memory
  27. // or set FileSystem using disk files
  28. func Load(path string) {
  29. prefixPath = path
  30. if prefixPath != "" {
  31. FileSystem = http.Dir(prefixPath)
  32. } else {
  33. FileSystem = http.FS(content)
  34. }
  35. }
  36. func Register(fileSystem fs.FS) {
  37. subFs, err := fs.Sub(fileSystem, "static")
  38. if err == nil {
  39. content = subFs
  40. }
  41. }