forked from h2non/imaginary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.go
52 lines (44 loc) · 1.18 KB
/
source.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
package main
import (
"net/http"
"net/url"
)
type ImageSourceType string
type ImageSourceFactoryFunction func(*SourceConfig) ImageSource
type SourceConfig struct {
AuthForwarding bool
Authorization string
MountPath string
Type ImageSourceType
AllowedOrigings []*url.URL
MaxAllowedSize int
}
var imageSourceMap = make(map[ImageSourceType]ImageSource)
var imageSourceFactoryMap = make(map[ImageSourceType]ImageSourceFactoryFunction)
type ImageSource interface {
Matches(*http.Request) bool
GetImage(*http.Request) ([]byte, error)
}
func RegisterSource(sourceType ImageSourceType, factory ImageSourceFactoryFunction) {
imageSourceFactoryMap[sourceType] = factory
}
func LoadSources(o ServerOptions) {
for name, factory := range imageSourceFactoryMap {
imageSourceMap[name] = factory(&SourceConfig{
Type: name,
MountPath: o.Mount,
AuthForwarding: o.AuthForwarding,
Authorization: o.Authorization,
AllowedOrigings: o.AllowedOrigins,
MaxAllowedSize: o.MaxAllowedSize,
})
}
}
func MatchSource(req *http.Request) ImageSource {
for _, source := range imageSourceMap {
if source.Matches(req) {
return source
}
}
return nil
}