resource.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package app
  2. import (
  3. "net/http"
  4. "strings"
  5. )
  6. // ResourceProvider is the interface that describes a resource provider that
  7. // tells the Handler how to locate and get the package and static resources.
  8. //
  9. // Package resources are the resource required to operate go-app.
  10. //
  11. // Static resources are resources such as app.wasm, CSS files, images.
  12. //
  13. // The resource provider is used to serve static resources when it satisfies the
  14. // http.Handler interface.
  15. type ResourceProvider interface {
  16. // Package returns the path where the package resources are located.
  17. Package() string
  18. // Static returns the path where the static resources directory (/web) is
  19. // located.
  20. Static() string
  21. // AppWASM returns the app.wasm file path.
  22. AppWASM() string
  23. }
  24. // LocalDir returns a resource provider that serves static resources from a
  25. // local directory located at the given path.
  26. func LocalDir(root string) ResourceProvider {
  27. root = strings.Trim(root, "/")
  28. return localDir{
  29. Handler: http.FileServer(http.Dir(root)),
  30. root: root,
  31. appWASM: root + "/web/app.wasm",
  32. }
  33. }
  34. type localDir struct {
  35. http.Handler
  36. root string
  37. appWASM string
  38. }
  39. func (d localDir) Package() string {
  40. return d.root
  41. }
  42. func (d localDir) Static() string {
  43. return d.root
  44. }
  45. func (d localDir) AppWASM() string {
  46. return d.appWASM
  47. }
  48. // RemoteBucket returns a resource provider that provides resources from a
  49. // remote bucket such as Amazon S3 or Google Cloud Storage.
  50. func RemoteBucket(url string) ResourceProvider {
  51. url = strings.TrimSuffix(url, "/")
  52. url = strings.TrimSuffix(url, "/web")
  53. return remoteBucket{
  54. root: url,
  55. appWASM: url + "/web/app.wasm",
  56. }
  57. }
  58. type remoteBucket struct {
  59. root string
  60. appWASM string
  61. }
  62. func (b remoteBucket) Package() string {
  63. return ""
  64. }
  65. func (b remoteBucket) Static() string {
  66. return b.root
  67. }
  68. func (b remoteBucket) AppWASM() string {
  69. return b.appWASM
  70. }
  71. // GitHubPages returns a resource provider that provides resources from GitHub
  72. // pages. This provider must only be used to generate static websites with the
  73. // GenerateStaticWebsite function.
  74. func GitHubPages(repoName string) ResourceProvider {
  75. return CustomProvider("", repoName)
  76. }
  77. // CustomProvider returns a resource provider that serves static resources from
  78. // a local directory located at the given path and prefixes URL paths with the
  79. // given prefix.
  80. func CustomProvider(path, prefix string) ResourceProvider {
  81. root := strings.Trim(path, "/")
  82. prefix = "/" + strings.Trim(prefix, "/")
  83. return localDir{
  84. Handler: http.FileServer(http.Dir(root)),
  85. root: prefix,
  86. appWASM: prefix + "/web/app.wasm",
  87. }
  88. }
  89. // ProxyResource is a proxy descriptor that maps a given resource to an URL
  90. // path.
  91. type ProxyResource struct {
  92. // The URL path from where a static resource is accessible.
  93. Path string
  94. // The path of the static resource that is proxied. It must start with
  95. // "/web/".
  96. ResourcePath string
  97. }