| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- package app
- import (
- "net/http"
- "strings"
- )
- // ResourceProvider is the interface that describes a resource provider that
- // tells the Handler how to locate and get the package and static resources.
- //
- // Package resources are the resource required to operate go-app.
- //
- // Static resources are resources such as app.wasm, CSS files, images.
- //
- // The resource provider is used to serve static resources when it satisfies the
- // http.Handler interface.
- type ResourceProvider interface {
- // Package returns the path where the package resources are located.
- Package() string
- // Static returns the path where the static resources directory (/web) is
- // located.
- Static() string
- // AppWASM returns the app.wasm file path.
- AppWASM() string
- }
- // LocalDir returns a resource provider that serves static resources from a
- // local directory located at the given path.
- func LocalDir(root string) ResourceProvider {
- root = strings.Trim(root, "/")
- return localDir{
- Handler: http.FileServer(http.Dir(root)),
- root: root,
- appWASM: root + "/web/app.wasm",
- }
- }
- type localDir struct {
- http.Handler
- root string
- appWASM string
- }
- func (d localDir) Package() string {
- return d.root
- }
- func (d localDir) Static() string {
- return d.root
- }
- func (d localDir) AppWASM() string {
- return d.appWASM
- }
- // RemoteBucket returns a resource provider that provides resources from a
- // remote bucket such as Amazon S3 or Google Cloud Storage.
- func RemoteBucket(url string) ResourceProvider {
- url = strings.TrimSuffix(url, "/")
- url = strings.TrimSuffix(url, "/web")
- return remoteBucket{
- root: url,
- appWASM: url + "/web/app.wasm",
- }
- }
- type remoteBucket struct {
- root string
- appWASM string
- }
- func (b remoteBucket) Package() string {
- return ""
- }
- func (b remoteBucket) Static() string {
- return b.root
- }
- func (b remoteBucket) AppWASM() string {
- return b.appWASM
- }
- // GitHubPages returns a resource provider that provides resources from GitHub
- // pages. This provider must only be used to generate static websites with the
- // GenerateStaticWebsite function.
- func GitHubPages(repoName string) ResourceProvider {
- return CustomProvider("", repoName)
- }
- // CustomProvider returns a resource provider that serves static resources from
- // a local directory located at the given path and prefixes URL paths with the
- // given prefix.
- func CustomProvider(path, prefix string) ResourceProvider {
- root := strings.Trim(path, "/")
- prefix = "/" + strings.Trim(prefix, "/")
- return localDir{
- Handler: http.FileServer(http.Dir(root)),
- root: prefix,
- appWASM: prefix + "/web/app.wasm",
- }
- }
- // ProxyResource is a proxy descriptor that maps a given resource to an URL
- // path.
- type ProxyResource struct {
- // The URL path from where a static resource is accessible.
- Path string
- // The path of the static resource that is proxied. It must start with
- // "/web/".
- ResourcePath string
- }
|