proxy-detector/pkg/getter/base.go
2024-12-11 11:13:06 +08:00

55 lines
1.2 KiB
Go

package getter
import (
"errors"
configv1 "gitea.timerzz.com/timerzz/proxy-detector/config"
"gitea.timerzz.com/timerzz/proxy-detector/pkg/proxy/structs"
)
type Getter interface {
Get() []structs.Proxy
}
// function type that creates getters
type creator func(options configv1.CrawOption) (getter Getter, err error)
// map str sourceType -> func creating getters,
// registered in package init()
var creatorMap = make(map[string]creator)
func Register(sourceType string, c creator) {
creatorMap[sourceType] = c
}
func NewGetter(sourceType string, options configv1.CrawOption) (getter Getter, err error) {
c, ok := creatorMap[sourceType]
if ok {
return c(options)
}
return nil, ErrorCreatorNotSupported
}
func CreateGetters(sourceFiles []string) (getters []Getter, err error) {
var sources []configv1.Source
for _, source := range sourceFiles {
sources, err = configv1.LoadSource(source)
if err != nil {
return nil, err
}
for _, s := range sources {
getter, err := NewGetter(s.Type, s.Options)
if err != nil {
return nil, err
}
getters = append(getters, getter)
}
}
return
}
var (
ErrorUrlNotFound = errors.New("url should be specified")
ErrorCreatorNotSupported = errors.New("type not supported")
)