mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
rough implementation of geodb docker service
This commit is contained in:
@@ -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
|
||||
@@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use Appwrite\Utopia\Fetch\Client;
|
||||
use Appwrite\Utopia\Request;
|
||||
use Appwrite\Utopia\Response;
|
||||
use MaxMind\Db\Reader;
|
||||
@@ -25,7 +26,8 @@ App::get('/v1/locale')
|
||||
->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');
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Utopia\Fetch;
|
||||
|
||||
use Utopia\Fetch\Client as UtopiaClient;
|
||||
use Utopia\Fetch\Response;
|
||||
|
||||
class Client extends UtopiaClient
|
||||
{
|
||||
protected string $baseUrl = '';
|
||||
|
||||
public function setBaseUrl(string $baseUrl): void
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user