-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
mux.go
111 lines (91 loc) · 2.58 KB
/
mux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package fsimpl
import (
"fmt"
"io/fs"
"net/url"
"sort"
"strings"
)
// FSMux allows you to dynamically look up a registered filesystem for a given
// URL. All filesystems provided in this module can be registered, and
// additional filesystems can be registered given an implementation of
// FSProvider.
// FSMux is itself an FSProvider, which provides the superset of all registered
// filesystems.
type FSMux map[string]func(*url.URL) (fs.FS, error)
var _ FSProvider = (FSMux)(nil)
// NewMux returns an FSMux ready for use.
func NewMux() FSMux {
return FSMux(map[string]func(*url.URL) (fs.FS, error){})
}
// Add registers the given filesystem provider for its supported URL schemes. If
// any of its schemes are already registered, they will be overridden.
func (m FSMux) Add(fs FSProvider) {
for _, scheme := range fs.Schemes() {
m[scheme] = fs.New
}
}
// Lookup returns an appropriate filesystem for the given URL. Use Add to
// register providers.
func (m FSMux) Lookup(u string) (fs.FS, error) {
base, err := url.Parse(u)
if err != nil {
return nil, err
}
return m.New(base)
}
// Schemes - implements FSProvider
func (m FSMux) Schemes() []string {
schemes := make([]string, 0, len(m))
for scheme := range m {
schemes = append(schemes, scheme)
}
sort.Strings(schemes)
return schemes
}
// New - implements FSProvider
func (m FSMux) New(u *url.URL) (fs.FS, error) {
f, ok := m[u.Scheme]
if !ok {
return nil, fmt.Errorf("no filesystem registered for scheme %q", u.Scheme)
}
return f(u)
}
// FSProvider provides a filesystem for a set of defined schemes
type FSProvider interface {
// Schemes returns the valid URL schemes for this filesystem
Schemes() []string
// New returns a filesystem from the given URL
New(u *url.URL) (fs.FS, error)
}
// FSProviderFunc -
func FSProviderFunc(f func(*url.URL) (fs.FS, error), schemes ...string) FSProvider {
return fsp{f, schemes}
}
type fsp struct {
newFunc func(*url.URL) (fs.FS, error)
schemes []string
}
func (p fsp) Schemes() []string {
return p.schemes
}
func (p fsp) New(u *url.URL) (fs.FS, error) {
return p.newFunc(u)
}
// WrappedFSProvider is an FSProvider that returns the given fs.FS.
// When given a URL with a non-root path (i.e. not '/'), fs.Sub will be used to
// return a filesystem appropriate for the URL.
func WrappedFSProvider(fsys fs.FS, schemes ...string) FSProvider {
return fsp{
newFunc: func(u *url.URL) (fs.FS, error) {
dir := u.Path
if dir == "/" {
dir = "."
} else if strings.HasPrefix(dir, "/") {
dir = strings.TrimLeft(dir, "/")
}
return fs.Sub(fsys, dir)
},
schemes: schemes,
}
}