From ebb1da8b316782c817bc91182d1fbeaed40b694e Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 2 Oct 2024 09:26:07 +0000 Subject: [PATCH] rough implementation of geodb docker service --- .env | 1 + app/controllers/api/locale.php | 45 ++++++++++++++++++++-------- app/init.php | 9 ++++++ docker-compose.yml | 9 ++++++ src/Appwrite/Utopia/Fetch/Client.php | 32 ++++++++++++++++++++ 5 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 src/Appwrite/Utopia/Fetch/Client.php diff --git a/.env b/.env index f6a6a7f642..77f1d11316 100644 --- a/.env +++ b/.env @@ -107,3 +107,4 @@ _APP_MESSAGE_EMAIL_TEST_DSN= _APP_MESSAGE_PUSH_TEST_DSN= _APP_WEBHOOK_MAX_FAILED_ATTEMPTS=10 _APP_PROJECT_REGIONS=default +_APP_GEO_SECRET=geo-secret-key \ No newline at end of file diff --git a/app/controllers/api/locale.php b/app/controllers/api/locale.php index 2917bc8416..581008511e 100644 --- a/app/controllers/api/locale.php +++ b/app/controllers/api/locale.php @@ -1,5 +1,6 @@ inject('response') ->inject('locale') ->inject('geodb') - ->action(function (Request $request, Response $response, Locale $locale, Reader $geodb) { + ->inject('geodbClient') + ->action(function (Request $request, Response $response, Locale $locale, Reader $geodb, Client $geodbClient) { $eu = Config::getParam('locale-eu'); $currencies = Config::getParam('locale-currencies'); $output = []; @@ -33,26 +35,45 @@ App::get('/v1/locale') $time = (60 * 60 * 24 * 45); // 45 days cache $output['ip'] = $ip; - $currency = null; - $record = $geodb->get($ip); - - if ($record) { - $output['countryCode'] = $record['country']['iso_code']; - $output['country'] = $locale->getText('countries.' . strtolower($record['country']['iso_code']), $locale->getText('locale.country.unknown')); - $output['continent'] = $locale->getText('continents.' . strtolower($record['continent']['code']), $locale->getText('locale.country.unknown')); - $output['continentCode'] = $record['continent']['code']; - $output['eu'] = (\in_array($record['country']['iso_code'], $eu)) ? true : false; + try { + $record = $geodbClient->fetch("/ips/{$ip}", Client::METHOD_GET); + $record = $record->json(); + $output['countryCode'] = $record['countryCode']; + $output['country'] = $locale->getText('countries.' . strtolower($record['countryCode']), $locale->getText('locale.country.unknown')); + $output['continent'] = $locale->getText('continents.' . strtolower($record['continentCode']), $locale->getText('locale.country.unknown')); + $output['continentCode'] = $record['continentCode']; + $output['eu'] = (\in_array($record['countryCode'], $eu)) ? true : false; foreach ($currencies as $code => $element) { - if (isset($element['locations']) && isset($element['code']) && \in_array($record['country']['iso_code'], $element['locations'])) { + if (isset($element['locations']) && isset($element['code']) && \in_array($record['countryCode'], $element['locations'])) { $currency = $element['code']; } } $output['currency'] = $currency; - } else { + } catch (Exception $e) { + // Fallback to hosted geodb + $record = $geodb->get($ip); + if ($record) { + $output['countryCode'] = $record['country']['iso_code']; + $output['country'] = $locale->getText('countries.' . strtolower($record['country']['iso_code']), $locale->getText('locale.country.unknown')); + $output['continent'] = $locale->getText('continents.' . strtolower($record['continent']['code']), $locale->getText('locale.country.unknown')); + $output['continentCode'] = $record['continent']['code']; + $output['eu'] = (\in_array($record['country']['iso_code'], $eu)) ? true : false; + + foreach ($currencies as $code => $element) { + if (isset($element['locations']) && isset($element['code']) && \in_array($record['country']['iso_code'], $element['locations'])) { + $currency = $element['code']; + } + } + + $output['currency'] = $currency; + } + } + + if (!$record) { $output['countryCode'] = '--'; $output['country'] = $locale->getText('locale.country.unknown'); $output['continent'] = $locale->getText('locale.country.unknown'); diff --git a/app/init.php b/app/init.php index cf0dbe44ee..9217485b4f 100644 --- a/app/init.php +++ b/app/init.php @@ -41,6 +41,7 @@ use Appwrite\Network\Validator\Email; use Appwrite\Network\Validator\Origin; use Appwrite\OpenSSL\OpenSSL; use Appwrite\URL\URL as AppwriteURL; +use Appwrite\Utopia\Fetch\Client; use Appwrite\Utopia\Request; use MaxMind\Db\Reader; use PHPMailer\PHPMailer\PHPMailer; @@ -1034,6 +1035,7 @@ $register->set('smtp', function () { $register->set('geodb', function () { return new Reader(__DIR__ . '/assets/dbip/dbip-country-lite-2024-09.mmdb'); }); + $register->set('passwordsDictionary', function () { $content = \file_get_contents(__DIR__ . '/assets/security/10k-common-passwords'); $content = explode("\n", $content); @@ -1622,6 +1624,13 @@ App::setResource('geodb', function ($register) { return $register->get('geodb'); }, ['register']); +App::setResource('geodbClient', function () { + $client = new Client(); + $client->addHeader('Authorization', 'Bearer ' . System::getEnv('_APP_GEO_SECRET')); + $client->setBaseUrl('http://appwrite-geo/v1'); + return $client; +}); + App::setResource('passwordsDictionary', function ($register) { /** @var Utopia\Registry\Registry $register */ return $register->get('passwordsDictionary'); diff --git a/docker-compose.yml b/docker-compose.yml index 6ecb0ecff8..511869791b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -192,6 +192,7 @@ services: - _APP_EXPERIMENT_LOGGING_PROVIDER - _APP_EXPERIMENT_LOGGING_CONFIG - _APP_DATABASE_SHARED_TABLES + - _APP_GEO_SECRET appwrite-console: <<: *x-logging @@ -868,6 +869,14 @@ services: - appwrite environment: - _APP_ASSISTANT_OPENAI_API_KEY + + appwrite-geo: + container_name: appwrite-geo + image: appwrite/geo:0.1.0 + networks: + - appwrite + environment: + - GEO_SECRET=$_APP_GEO_SECRET openruntimes-executor: container_name: openruntimes-executor diff --git a/src/Appwrite/Utopia/Fetch/Client.php b/src/Appwrite/Utopia/Fetch/Client.php new file mode 100644 index 0000000000..8984db2e8d --- /dev/null +++ b/src/Appwrite/Utopia/Fetch/Client.php @@ -0,0 +1,32 @@ +baseUrl = $baseUrl; + } + + public function getBaseUrl(): string + { + return $this->baseUrl; + } + + public function fetch( + string $url, + string $method = self::METHOD_GET, + array $body = [], + array $query = [], + ): Response { + $url = "{$this->baseUrl}{$url}"; + return parent::fetch($url, $method, $body, $query); + } + +}