1
0
Fork 0
mirror of synced 2024-09-29 08:51:28 +13:00
appwrite/app/http.php

216 lines
7.2 KiB
PHP
Raw Normal View History

2020-06-26 21:54:37 +12:00
<?php
2022-05-24 02:54:50 +12:00
require_once __DIR__ . '/../vendor/autoload.php';
2020-06-26 21:54:37 +12:00
2024-03-07 06:34:21 +13:00
use Appwrite\Utopia\Request;
use Appwrite\Utopia\Response;
2024-03-07 06:34:21 +13:00
use Swoole\Process;
2024-04-04 00:43:20 +13:00
use Swoole\Runtime;
2024-03-07 06:34:21 +13:00
use Utopia\Abuse\Adapters\TimeLimit;
use Utopia\Audit\Audit;
2020-06-27 00:27:58 +12:00
use Utopia\CLI\Console;
2021-05-04 22:24:08 +12:00
use Utopia\Config\Config;
2024-03-07 06:34:21 +13:00
use Utopia\Database\Database;
use Utopia\Database\Document;
2022-12-15 04:42:25 +13:00
use Utopia\Database\Helpers\ID;
2022-12-15 05:04:06 +13:00
use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role;
2021-07-26 02:51:04 +12:00
use Utopia\Database\Validator\Authorization;
2024-03-09 01:57:20 +13:00
use Utopia\Http\Adapter\Swoole\Server;
use Utopia\Http\Http;
2022-10-16 03:14:17 +13:00
use Utopia\Pools\Group;
2020-07-01 20:55:14 +12:00
2021-09-30 21:37:03 +13:00
$payloadSize = 6 * (1024 * 1024); // 6MB
2024-03-08 03:29:42 +13:00
$workerNumber = swoole_cpu_num() * intval(Http::getEnv('_APP_WORKER_PER_CORE', 6));
2022-09-30 23:32:58 +13:00
2024-04-04 00:43:20 +13:00
// Unlimited memory limit to handle as many coroutines/requests as possible
ini_set('memory_limit', '-1');
Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL);
2024-03-09 01:57:20 +13:00
include __DIR__ . '/controllers/general.php';
2020-06-26 21:54:37 +12:00
2024-03-09 01:57:20 +13:00
$http = new Http(new Server('0.0.0.0', Http::getEnv('PORT', 80), [
'open_http2_protocol' => true,
'http_compression' => true,
'http_compression_level' => 6,
'package_max_length' => $payloadSize,
'buffer_output_size' => $payloadSize,
]), 'UTC');
2020-06-26 21:54:37 +12:00
2024-03-09 01:57:20 +13:00
$http->setRequestClass(Request::class);
$http->setResponseClass(Response::class);
2021-05-07 09:35:05 +12:00
2024-03-09 01:57:20 +13:00
$http->loadFiles(__DIR__ . '/../console');
2024-03-09 01:57:20 +13:00
go(function () use ($register, $http, $payloadSize) {
$pools = $register->get('pools');
/** @var Group $pools */
Http::setResource('pools', fn () => $pools);
$auth = new Authorization();
2021-07-05 03:14:39 +12:00
2024-03-09 01:57:20 +13:00
// wait for database to be ready
$attempts = 0;
$max = 10;
$sleep = 1;
2022-09-13 02:09:28 +12:00
2024-03-09 01:57:20 +13:00
do {
2022-10-16 18:58:25 +13:00
try {
2024-03-09 01:57:20 +13:00
$attempts++;
$dbForConsole = $http->getResource('dbForConsole');
$dbForConsole->ping();
/** @var Utopia\Database\Database $dbForConsole */
break; // leave the do-while if successful
} catch (\Throwable $e) {
2024-03-09 01:57:20 +13:00
Console::warning("Database not ready. Retrying connection ({$attempts})...");
if ($attempts >= $max) {
throw new \Exception('Failed to connect to database: ' . $e->getMessage());
}
sleep($sleep);
}
2024-03-09 01:57:20 +13:00
} while ($attempts < $max);
2024-03-09 01:57:20 +13:00
Console::success('[Setup] - Server database init started...');
2021-07-05 03:14:39 +12:00
2024-03-09 01:57:20 +13:00
try {
Console::success('[Setup] - Creating database: appwrite...');
$dbForConsole->create();
} catch (\Throwable $e) {
Console::success('[Setup] - Skip: metadata table already exists');
}
2021-07-05 03:14:39 +12:00
2024-03-09 01:57:20 +13:00
if ($dbForConsole->getCollection(Audit::COLLECTION)->isEmpty()) {
$audit = new Audit($dbForConsole, $auth);
$audit->setup();
}
2022-06-23 09:11:42 +12:00
2024-03-09 01:57:20 +13:00
if ($dbForConsole->getCollection(TimeLimit::COLLECTION)->isEmpty()) {
$adapter = new TimeLimit("", 0, 1, $dbForConsole, $auth);
$adapter->setup();
}
2021-07-05 03:14:39 +12:00
2024-03-09 01:57:20 +13:00
/** @var array $collections */
$collections = Config::getParam('collections', []);
$consoleCollections = $collections['console'];
foreach ($consoleCollections as $key => $collection) {
if (($collection['$collection'] ?? '') !== Database::METADATA) {
continue;
}
if (!$dbForConsole->getCollection($key)->isEmpty()) {
continue;
}
2021-07-05 00:05:46 +12:00
2024-03-09 01:57:20 +13:00
Console::success('[Setup] - Creating collection: ' . $collection['$id'] . '...');
$attributes = [];
$indexes = [];
foreach ($collection['attributes'] as $attribute) {
$attributes[] = new Document([
'$id' => ID::custom($attribute['$id']),
'type' => $attribute['type'],
'size' => $attribute['size'],
'required' => $attribute['required'],
'signed' => $attribute['signed'],
'array' => $attribute['array'],
'filters' => $attribute['filters'],
'default' => $attribute['default'] ?? null,
'format' => $attribute['format'] ?? ''
]);
2022-01-10 19:31:33 +13:00
}
2024-03-09 01:57:20 +13:00
foreach ($collection['indexes'] as $index) {
$indexes[] = new Document([
'$id' => ID::custom($index['$id']),
'type' => $index['type'],
'attributes' => $index['attributes'],
'lengths' => $index['lengths'],
'orders' => $index['orders'],
]);
}
2024-03-09 01:57:20 +13:00
$dbForConsole->createCollection($key, $attributes, $indexes);
}
2022-05-24 02:54:50 +12:00
2024-03-09 01:57:20 +13:00
if ($dbForConsole->getDocument('buckets', 'default')->isEmpty() && !$dbForConsole->exists($dbForConsole->getDatabase(), 'bucket_1')) {
Console::success('[Setup] - Creating default bucket...');
$dbForConsole->createDocument('buckets', new Document([
'$id' => ID::custom('default'),
'$collection' => ID::custom('buckets'),
'name' => 'Default',
'maximumFileSize' => (int) Http::getEnv('_APP_STORAGE_LIMIT', 0), // 10MB
'allowedFileExtensions' => [],
'enabled' => true,
'compression' => 'gzip',
'encryption' => true,
'antivirus' => true,
'fileSecurity' => true,
'$permissions' => [
Permission::create(Role::any()),
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
'search' => 'buckets Default',
]));
$bucket = $dbForConsole->getDocument('buckets', 'default');
Console::success('[Setup] - Creating files collection for default bucket...');
$files = $collections['buckets']['files'] ?? [];
if (empty($files)) {
throw new Exception('Files collection is not configured.');
}
2022-05-24 02:54:50 +12:00
2024-03-09 01:57:20 +13:00
$attributes = [];
$indexes = [];
foreach ($files['attributes'] as $attribute) {
$attributes[] = new Document([
'$id' => ID::custom($attribute['$id']),
'type' => $attribute['type'],
'size' => $attribute['size'],
'required' => $attribute['required'],
'signed' => $attribute['signed'],
'array' => $attribute['array'],
'filters' => $attribute['filters'],
'default' => $attribute['default'] ?? null,
'format' => $attribute['format'] ?? ''
]);
2022-01-10 19:31:33 +13:00
}
2024-03-09 01:57:20 +13:00
foreach ($files['indexes'] as $index) {
$indexes[] = new Document([
'$id' => ID::custom($index['$id']),
'type' => $index['type'],
'attributes' => $index['attributes'],
'lengths' => $index['lengths'],
'orders' => $index['orders'],
]);
}
2022-07-10 01:41:14 +12:00
2024-03-09 01:57:20 +13:00
$dbForConsole->createCollection('bucket_' . $bucket->getInternalId(), $attributes, $indexes);
}
$pools->reclaim();
Console::success('[Setup] - Server database init completed...');
2021-05-04 22:24:08 +12:00
2022-05-24 02:54:50 +12:00
Console::success('Server started successfully (max payload is ' . number_format($payloadSize) . ' bytes)');
2020-06-27 00:27:58 +12:00
// listen ctrl + c
Process::signal(2, function () use ($http) {
2020-07-09 03:08:14 +12:00
Console::log('Stop by Ctrl+C');
2020-06-27 00:27:58 +12:00
$http->shutdown();
});
2021-12-22 04:01:40 +13:00
2024-03-09 01:57:20 +13:00
Http::init()
->inject('auth')
->action(function (Authorization $auth) {
$auth->cleanRoles();
$auth->addRole(Role::any()->toString());
});
2021-12-22 04:01:40 +13:00
2024-03-09 01:57:20 +13:00
$http->start();
2020-06-26 21:54:37 +12:00
});