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

231 lines
8.2 KiB
PHP
Raw Normal View History

2020-06-26 21:54:37 +12:00
<?php
2024-04-14 20:58:05 +12:00
ini_set('memory_limit', '512M');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('default_socket_timeout', -1);
error_reporting(E_ALL);
2020-06-26 21:54:37 +12:00
2022-05-24 02:54:50 +12:00
require_once __DIR__ . '/../vendor/autoload.php';
2020-06-26 21:54:37 +12:00
2024-04-14 20:58:05 +12:00
use Appwrite\Utopia\Queue\Connections;
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 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-04-14 20:58:05 +12:00
use Utopia\DI\Container;
2024-03-09 01:57:20 +13:00
use Utopia\Http\Adapter\Swoole\Server;
use Utopia\Http\Http;
2024-04-14 20:58:05 +12:00
use Utopia\System\System;
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');
2024-04-14 20:58:05 +12:00
$container = new Container();
$workerNumber = swoole_cpu_num() * intval(System::getEnv('_APP_WORKER_PER_CORE', 6));
$payloadSize = 6 * (1024 * 1024); // 6MB
2020-06-26 21:54:37 +12:00
2024-04-14 20:58:05 +12:00
$server = new Server('0.0.0.0', '80', [
2024-03-09 01:57:20 +13:00
'open_http2_protocol' => true,
2024-04-14 20:58:05 +12:00
// 'http_compression' => true,
// 'http_compression_level' => 6,
// Server
// 'log_level' => 0,
'dispatch_mode' => 2,
'worker_num' => $workerNumber,
'reactor_num' => swoole_cpu_num() * 2,
// 'task_worker_num' => $workerNumber,
'open_cpu_affinity' => true,
// Coroutine
'enable_coroutine' => true,
'max_coroutine' => 10000,
]);
2020-06-26 21:54:37 +12:00
2024-04-14 20:58:05 +12:00
$http = new Http($server, $container, 'UTC');
// $http->loadFiles(__DIR__ . '/../console');
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-04-14 21:22:53 +12:00
//require_once __DIR__ . '/init.php';
require_once __DIR__ . '/init/constants.php';
require_once __DIR__ . '/init/config.php';
require_once __DIR__ . '/init/locale.php';
require_once __DIR__ . '/init/database/filters.php';
require_once __DIR__ . '/init/database/formats.php';
2024-04-14 20:58:05 +12:00
require_once __DIR__ . '/init2.php';
2024-04-14 21:22:53 +12:00
require_once __DIR__ . '/controllers/general.php';
2024-04-14 20:58:05 +12:00
global $global;
http::onStart()
->inject('authorization')
->inject('dbForConsole')
->inject('connections')
->action(function (Authorization $authorization, Database $dbForConsole, Connections $connections) {
// wait for database to be ready
$attempts = 0;
$max = 10;
$sleep = 1;
do {
try {
$attempts++;
$dbForConsole->ping();
break; // leave the do-while if successful
} catch (\Throwable $e) {
Console::warning("Database not ready. Retrying connection ({$attempts})...");
if ($attempts >= $max) {
throw new \Exception('Failed to connect to database: ' . $e->getMessage());
}
sleep($sleep);
}
} while ($attempts < $max);
2021-07-05 03:14:39 +12:00
2024-04-14 20:58:05 +12:00
Console::success('[Setup] - Server database init started...');
2022-09-13 02:09:28 +12:00
2022-10-16 18:58:25 +13:00
try {
2024-04-14 20:58:05 +12:00
Console::success('[Setup] - Creating database: appwrite...');
$dbForConsole->create();
} catch (\Throwable $e) {
2024-04-14 20:58:05 +12:00
Console::success('[Setup] - Skip: metadata table already exists');
return true;
2024-03-09 01:57:20 +13:00
}
2021-07-05 00:05:46 +12:00
2024-04-14 20:58:05 +12:00
if ($dbForConsole->getCollection(Audit::COLLECTION)->isEmpty()) {
$audit = new Audit($dbForConsole, $authorization);
$audit->setup();
2022-01-10 19:31:33 +13:00
}
2024-04-14 20:58:05 +12:00
if ($dbForConsole->getCollection(TimeLimit::COLLECTION)->isEmpty()) {
$abuse = new TimeLimit("", 0, 1, $dbForConsole, $authorization);
$abuse->setup();
2024-03-09 01:57:20 +13:00
}
2024-04-14 20:58:05 +12: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;
}
2022-05-24 02:54:50 +12:00
2024-04-14 20:58:05 +12: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'] ?? ''
]);
}
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-04-14 20:58:05 +12:00
$dbForConsole->createCollection($key, $attributes, $indexes);
2024-03-09 01:57:20 +13:00
}
2022-07-10 01:41:14 +12:00
2024-04-14 20:58:05 +12: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) System::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.');
}
$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'] ?? ''
]);
}
2024-03-09 01:57:20 +13:00
2024-04-14 20:58:05 +12: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'],
]);
}
2024-03-09 01:57:20 +13:00
2024-04-14 20:58:05 +12:00
$dbForConsole->createCollection('bucket_' . $bucket->getInternalId(), $attributes, $indexes);
}
2021-05-04 22:24:08 +12:00
2024-04-14 20:58:05 +12:00
$connections->reclaim();
2020-06-27 00:27:58 +12:00
2024-04-14 20:58:05 +12:00
Console::success('[Setup] - Server database init completed...');
Console::success('Server started successfully');
2020-06-27 00:27:58 +12:00
});
2021-12-22 04:01:40 +13:00
2024-04-14 20:58:05 +12:00
Http::init()
->inject('authorization')
->action(function (Authorization $authorization) {
$authorization->cleanRoles();
$authorization->addRole(Role::any()->toString());
});
2021-12-22 04:01:40 +13:00
2024-04-14 20:58:05 +12:00
$http->start();