diff --git a/app/http.php b/app/http.php index 686baa39ab..c95ce77f4a 100644 --- a/app/http.php +++ b/app/http.php @@ -26,6 +26,7 @@ use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; use Utopia\Database\Query; +use Utopia\DI\Container; use Utopia\Http\Adapter\Swoole\Server; use Utopia\Http\Files; use Utopia\Http\Http; @@ -57,7 +58,7 @@ $container->set('pools', function ($register) { $payloadSize = 12 * (1024 * 1024); // 12MB - adding slight buffer for headers and other data that might be sent with the payload - update later with valid testing $totalWorkers = intval(System::getEnv('_APP_CPU_NUM', swoole_cpu_num())) * intval(System::getEnv('_APP_WORKER_PER_CORE', 6)); -$swooleAdapter = new Server( +$swoole = new Server( host: "0.0.0.0", port: System::getEnv('PORT', 80), settings: [ @@ -72,7 +73,7 @@ $swooleAdapter = new Server( resources: $container, ); -$http = $swooleAdapter->getServer(); +$http = $swoole->getServer(); /** * Assigns HTTP requests to worker threads by analyzing its payload/content. @@ -190,11 +191,11 @@ $http->on(Constant::EVENT_AFTER_RELOAD, function ($server) { Console::success('Reload completed...'); }); -$container->set('bus', fn ($register) => $register->get('bus')->setResolver($swooleAdapter->context()->get(...)), ['register']); +$container->set('bus', fn ($register) => $register->get('bus')->setResolver($swoole->context()->get(...)), ['register']); include __DIR__ . '/controllers/general.php'; -function createDatabase(Http $app, string $resourceKey, string $dbName, array $collections, mixed $pools, ?callable $extraSetup = null): void +function createDatabase(Container $resources, string $resourceKey, string $dbName, array $collections, mixed $pools, ?callable $extraSetup = null): void { $max = 15; $sleep = 2; @@ -203,7 +204,7 @@ function createDatabase(Http $app, string $resourceKey, string $dbName, array $c while (true) { try { $attempts++; - $resource = $app->context()->get($resourceKey); + $resource = $resources->get($resourceKey); /* @var $database Database */ $database = is_callable($resource) ? $resource() : $resource; break; // exit loop on success @@ -286,23 +287,21 @@ function createDatabase(Http $app, string $resourceKey, string $dbName, array $c Span::current()?->finish(); } -$http->on(Constant::EVENT_START, function ($http) use ($payloadSize, $totalWorkers, $swooleAdapter) { - $app = new Http($swooleAdapter, 'UTC'); - +$http->on(Constant::EVENT_START, function ($http) use ($payloadSize, $totalWorkers, $container) { /** @var \Utopia\Pools\Group $pools */ - $pools = $app->context()->get('pools'); + $pools = $container->get('pools'); - go(function () use ($app, $pools) { + go(function () use ($container, $pools) { /** @var array $collections */ $collections = Config::getParam('collections', []); // create logs database first, `getLogsDB` is a callable. - createDatabase($app, 'getLogsDB', 'logs', $collections['logs'], $pools); + createDatabase($container, 'getLogsDB', 'logs', $collections['logs'], $pools); // create appwrite database, `dbForPlatform` is a direct access call. - createDatabase($app, 'dbForPlatform', 'appwrite', $collections['console'], $pools, function (Database $dbForPlatform) use ($collections, $app) { - $authorization = $app->context()->get('authorization'); + createDatabase($container, 'dbForPlatform', 'appwrite', $collections['console'], $pools, function (Database $dbForPlatform) use ($collections, $container) { + $authorization = $container->get('authorization'); if ($dbForPlatform->getCollection(AuditAdapterSQL::COLLECTION)->isEmpty()) { $adapter = new AdapterDatabase($dbForPlatform); @@ -414,7 +413,7 @@ $http->on(Constant::EVENT_START, function ($http) use ($payloadSize, $totalWorke $documentsSharedTables = \explode(',', System::getEnv('_APP_DATABASE_DOCUMENTSDB_SHARED_TABLES', '')); $vectorSharedTables = \explode(',', System::getEnv('_APP_DATABASE_VECTORSDB_SHARED_TABLES', '')); - $cache = $app->context()->get('cache'); + $cache = $container->get('cache'); // All shared tables pools that need project metadata collections $allSharedTables = \array_values(\array_unique(\array_filter([ @@ -500,7 +499,7 @@ $http->on(Constant::EVENT_START, function ($http) use ($payloadSize, $totalWorke }); }); -$swooleAdapter->onRequest(function ($utopiaRequest, $utopiaResponse) use ($files, $swooleAdapter, $setRequestContext) { +$swoole->onRequest(function ($utopiaRequest, $utopiaResponse) use ($files, $swoole, $setRequestContext) { Span::init('http.request'); $request = new Request($utopiaRequest->getSwooleRequest()); @@ -520,8 +519,7 @@ $swooleAdapter->onRequest(function ($utopiaRequest, $utopiaResponse) use ($files return; } - $app = new Http($swooleAdapter, 'UTC'); - $app->context()->set('container', fn () => $app->context()); + $app = new Http($swoole, 'UTC'); $app->context()->set('request', fn () => $request); $app->context()->set('response', fn () => $response); $app->context()->set('utopia', fn () => $app); @@ -638,18 +636,16 @@ $swooleAdapter->onRequest(function ($utopiaRequest, $utopiaResponse) use ($files }); // Fetch domains every `DOMAIN_SYNC_TIMER` seconds and update in the memory -$http->on(Constant::EVENT_TASK, function () use ($swooleAdapter) { +$http->on(Constant::EVENT_TASK, function () use ($container) { $lastSyncUpdate = null; - $app = new Http($swooleAdapter, 'UTC'); - /** @var Utopia\Database\Database $dbForPlatform */ - $dbForPlatform = $app->context()->get('dbForPlatform'); + $dbForPlatform = $container->get('dbForPlatform'); /** @var \Swoole\Table $riskyDomains */ - $riskyDomains = $app->context()->get('riskyDomains'); + $riskyDomains = $container->get('riskyDomains'); - Timer::tick(DOMAIN_SYNC_TIMER * 1000, function () use ($dbForPlatform, $riskyDomains, &$lastSyncUpdate, $app) { + Timer::tick(DOMAIN_SYNC_TIMER * 1000, function () use ($dbForPlatform, $riskyDomains, &$lastSyncUpdate, $container) { try { $time = DateTime::now(); $limit = 1000; @@ -666,7 +662,7 @@ $http->on(Constant::EVENT_TASK, function () use ($swooleAdapter) { } $results = []; try { - $authorization = $app->context()->get('authorization'); + $authorization = $container->get('authorization'); $results = $authorization->skip(fn () => $dbForPlatform->find('rules', $queries)); } catch (Throwable $th) { Console::error('rules ' . $th->getMessage()); @@ -716,4 +712,4 @@ $http->on(Constant::EVENT_TASK, function () use ($swooleAdapter) { }); }); -$swooleAdapter->start(); +$swoole->start(); diff --git a/src/Appwrite/GraphQL/Resolvers.php b/src/Appwrite/GraphQL/Resolvers.php index 41e808b13c..6e8c30b6ec 100644 --- a/src/Appwrite/GraphQL/Resolvers.php +++ b/src/Appwrite/GraphQL/Resolvers.php @@ -6,7 +6,6 @@ use Appwrite\GraphQL\Exception as GQLException; use Appwrite\Promises\Swoole; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; -use Utopia\DI\Container; use Utopia\Http\Exception; use Utopia\Http\Http; use Utopia\Http\Route; @@ -53,22 +52,6 @@ class Resolvers } } - /** - * Get the current request container. - */ - private static function getResolverContainer(Http $utopia): Container - { - $container = $utopia->context()->get('container'); - - if ($container instanceof Container || (\is_object($container) && \method_exists($container, 'get') && \method_exists($container, 'set'))) { - /** @var Container $container */ - return $container; - } - - /** @var callable(): Container $container */ - return $container(); - } - /** * Get the request-scoped lock shared by GraphQL resolver coroutines * for the current HTTP request. @@ -374,9 +357,8 @@ class Resolvers /** @var Response $resolverResponse */ $resolverResponse = clone $utopia->context()->get('response'); - $container = self::getResolverContainer($utopia); - $container->set('request', static fn () => $request); - $container->set('response', static fn () => $resolverResponse); + $utopia->context()->set('request', static fn () => $request); + $utopia->context()->set('response', static fn () => $resolverResponse); $resolverResponse->setContentType(Response::CONTENT_TYPE_NULL); $resolverResponse->setSent(false);