Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(api): simplifying geoip initialization #4447

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading