mirror of
https://github.com/h2non/imaginary.git
synced 2026-02-28 19:07:35 +01:00
42 lines
924 B
Go
42 lines
924 B
Go
package main
|
|
|
|
import "net/http"
|
|
|
|
type ImageSourceType string
|
|
type ImageSourceFactoryFunction func(*SourceConfig) ImageSource
|
|
|
|
type SourceConfig struct {
|
|
Type ImageSourceType
|
|
MountPath string
|
|
}
|
|
|
|
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,
|
|
})
|
|
}
|
|
}
|
|
|
|
func MatchSource(req *http.Request) ImageSource {
|
|
for _, source := range imageSourceMap {
|
|
if source.Matches(req) {
|
|
return source
|
|
}
|
|
}
|
|
return nil
|
|
}
|