-
Notifications
You must be signed in to change notification settings - Fork 14
/
geocoding.go
59 lines (45 loc) · 1.73 KB
/
geocoding.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
package registry
import (
"errors"
"fmt"
"github.com/sarulabs/di"
"github.com/coretrix/hitrix/service"
"github.com/coretrix/hitrix/service/component/clock"
"github.com/coretrix/hitrix/service/component/config"
"github.com/coretrix/hitrix/service/component/geocoding"
)
const (
GeocodingProviderGoogleMaps = "google_maps"
)
func ServiceProviderGeocoding(provider string) *service.DefinitionGlobal {
return &service.DefinitionGlobal{
Name: service.GeocodingService,
Build: func(ctn di.Container) (interface{}, error) {
providerConstructor, ok := providerConstructorFactory[provider]
if !ok {
return nil, fmt.Errorf("provider constructor not found by key: %s", provider)
}
configService := ctn.Get(service.ConfigService).(config.IConfig)
useCaching, okUseCaching := configService.Bool("geocoding.use_caching")
cacheExpiryDays, okCacheExpiryDays := configService.Int64("geocoding.cache_expiry_days")
if okUseCaching && !okCacheExpiryDays {
return nil, fmt.Errorf("you must specify geocoding.cache_expiry_days")
}
provider, err := providerConstructor(configService)
if err != nil {
return nil, err
}
return geocoding.NewGeocoding(useCaching, cacheExpiryDays, ctn.Get(service.ClockService).(clock.IClock), provider), nil
},
}
}
var providerConstructorFactory = map[string]func(configService config.IConfig) (geocoding.Provider, error){
GeocodingProviderGoogleMaps: providerConstructorGoogleMaps,
}
func providerConstructorGoogleMaps(configService config.IConfig) (geocoding.Provider, error) {
apiKey, ok := configService.String("geocoding.google_maps.api_key")
if !ok {
return nil, errors.New("missing geocoding.google_maps.api_key")
}
return geocoding.NewGoogleMapsProvider(apiKey), nil
}