Skip to content

Commit

Permalink
refactor(api): simplifying geoip initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
henrybarreto committed Dec 18, 2024
1 parent 0c293db commit 9779dfc
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,29 @@ func startServer(ctx context.Context, cfg *config, store store.Store, cache stor

servicesOptions := []services.Option{}

if cfg.GeoipMirror != "" {
if cfg.GeoipMirror != "" || cfg.GeoipMaxmindLicense != "" {
log.Info("GeoIP feature is enable")

locator, err := geolite2.NewLocator(ctx, geolite2.FetchFromMirror(cfg.GeoipMirror))
if err != nil {
log.WithError(err).Fatal("Failed to init GeoIP")
} else {
servicesOptions = append(servicesOptions, services.WithLocator(locator))
var fetcher geolite2.GeoliteFetcher

switch {
case cfg.GeoipMirror != "":
fetcher = geolite2.FetchFromMirror(cfg.GeoipMirror)
case cfg.GeoipMaxmindLicense != "":
fetcher = geolite2.FetchFromLicenseKey(cfg.GeoipMaxmindLicense)
default:
log.WithError(err).Fatal("Failed to init GeoIP because no valid value was set")
}
} else if cfg.GeoipMaxmindLicense != "" {
log.Info("GeoIP feature is enable")

locator, err := geolite2.NewLocator(ctx, geolite2.FetchFromLicenseKey(cfg.GeoipMaxmindLicense))
locator, err := geolite2.NewLocator(ctx, fetcher)

if err != nil {
log.WithError(err).Fatal("Failed to init GeoIP")
} else {
servicesOptions = append(servicesOptions, services.WithLocator(locator))
}

log.Info("GeoIP feature is enable")
}

service := services.NewService(store, nil, nil, cache, apiClient, servicesOptions...)
Expand Down

0 comments on commit 9779dfc

Please sign in to comment.