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

Allow use multi databases #31

Merged
merged 32 commits into from
Jan 27, 2020
Merged

Conversation

peter-gribanov
Copy link
Member

@peter-gribanov peter-gribanov commented Jan 27, 2020

Fix #8

BC breack change

Renamed configuration option cache to path.

Before

gpslab_geoip:
    cache: '%kernel.cache_dir%/GeoLite2-City.mmdb'

After

gpslab_geoip:
    path: '%kernel.cache_dir%/GeoLite2-City.mmdb'

How to use

Example configuration:

gpslab_geoip:
    # Your personal licence key
    license: 'XXXXXXXXXXXXXXXX'
    # Database edition ID
    edition: 'GeoLite2-City'

Database source URL

By default, this URL is used to download a new databases https://download.maxmind.com/app/geoip_download?edition_id={edition_id}&license_key={license_key}&suffix=tar.gz

You can change this URL, for example, if you want to use a proxy to download the database. You can customize the source URL in the configuration.

gpslab_geoip:
    url: 'https://example.com/GeoLite2-City.tar.gz'

Target download path

By default, new databases downloaded in %kernel.cache_dir%/{edition_id}.mmdb, where edition_id is a character ID name from first column on download page. That is, by default, the new database will be downloaded into folder var/cache/{env}/. Keeping the database in the cache folder for each
environment may not be optimal. You can choose a common directory for all environments.

gpslab_geoip:
    path: '%kernel.project_dir%/var/GeoLite2-City.mmdb'

Localization

By default, the English locale is used for GeoIP record. You can change the locale for record and declare multiple locales for fallback.

gpslab_geoip:
    locales: [ 'ru', 'en' ]

Multiple databases

You can use multiple GeoIP databases in one application. Need update configuration file.

gpslab_geoip:
    databases:
        default:
            license: 'XXXXXXXXXXXXXXXX'
            edition: 'GeoLite2-City'
        country:
            license: 'XXXXXXXXXXXXXXXX'
            edition: 'GeoLite2-Country'
        asn:
            license: 'XXXXXXXXXXXXXXXX'
            edition: 'GeoLite2-ASN'

Using in application:

// get a GeoIP2 reader for City database
$default_reader = $this->get('geoip2.database.default_reader');
// or
//$default_reader = $this->get(Reader::class);
// or
//$default_reader = $this->get('geoip2.reader');

// get a GeoIP2 reader for Country database
$country_reader = $this->get('geoip2.database.country_reader');

// get a GeoIP2 reader for ASN database
$asn_reader = $this->get('geoip2.database.asn_reader');

You can rename the default database.

gpslab_geoip:
    default_database: 'city'
    databases:
        asn:
            license: 'XXXXXXXXXXXXXXXX'
            edition: 'GeoLite2-ASN'
        city:
            license: 'XXXXXXXXXXXXXXXX'
            edition: 'GeoLite2-City'
        country:
            license: 'XXXXXXXXXXXXXXXX'
            edition: 'GeoLite2-Country'

In order not to repeat the license key and locales for each database, you can specify them once.

gpslab_geoip:
    license: 'XXXXXXXXXXXXXXXX' # global license
    locales: [ 'ru', 'en' ] # global locales
    default_database: 'city'
    databases:
        asn:
            edition: 'GeoLite2-ASN'
            locales: [ 'fr' ] # customize locales
        city:
            edition: 'GeoLite2-City'
        country:
            edition: 'GeoLite2-Country'
            license: 'YYYYYYYYYYYYYYYY' # customize license

GeoIP data in client locale

If you want to show the GeoIP data to the user and show them in the user locale, then you can use the reader factory.

use GpsLab\Bundle\GeoIP2Bundle\Reader\ReaderFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

public function index(Request $request, ReaderFactory $factory): Response
{
    $locale = $request->getLocale();
    $ip = $request->getClientIp();

    $reader = $factory->create('default', [$locale, 'en' /* fallback */]);
    $record = $reader->city($ip);

    return new Response(sprintf('You are from %s?', $record->country->name));
}

Console commands

Update GeoIP database

Execute console command for update all databases:

php bin/console geoip2:update

If you use multiple databases, then for config:

gpslab_geoip:
    # ...
    databases:
        asn:
            # ...
        city:
            # ...
        country:
            # ...

You can update several databases:

php bin/console geoip2:update city country

Download GeoIP database

You can download custom database with console command:

php bin/console geoip2:download https://example.com/GeoLite2-City.tar.gz /path/to/GeoLite2-City.mmdb

@peter-gribanov peter-gribanov self-assigned this Jan 27, 2020
@peter-gribanov peter-gribanov added this to the Release 2.0.0 milestone Jan 27, 2020
@coveralls
Copy link

Coverage Status

Coverage increased (+72.2%) to 99.617% when pulling 8f5c325 on peter-gribanov:multi_databases into eaf6c77 on gpslab:2.0.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage increased (+72.2%) to 99.617% when pulling 8f5c325 on peter-gribanov:multi_databases into eaf6c77 on gpslab:2.0.

@coveralls
Copy link

Coverage Status

Coverage increased (+72.2%) to 99.617% when pulling 8f5c325 on peter-gribanov:multi_databases into eaf6c77 on gpslab:2.0.

@coveralls
Copy link

coveralls commented Jan 27, 2020

Coverage Status

Coverage increased (+72.3%) to 99.628% when pulling dcc6a88 on peter-gribanov:multi_databases into eaf6c77 on gpslab:2.0.

@peter-gribanov peter-gribanov merged commit 2b36033 into gpslab:2.0 Jan 27, 2020
@peter-gribanov peter-gribanov deleted the multi_databases branch January 27, 2020 16:13
@peter-gribanov peter-gribanov mentioned this pull request Feb 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants