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

249 lines
9.4 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-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;
2024-04-15 17:29:32 +12:00
use Utopia\Cache\Cache;
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-04-15 17:29:32 +12:00
use Utopia\Database\Adapter\MariaDB;
use Utopia\Database\Adapter\MySQL;
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;
2024-04-14 20:58:05 +12:00
use Utopia\System\System;
2022-09-30 23:32:58 +13:00
2024-04-15 15:59:02 +12:00
//require_once __DIR__ . '/init.php';
require_once __DIR__ . '/init2.php';
require_once __DIR__ . '/controllers/general.php';
2024-04-04 00:43:20 +13:00
2024-04-14 20:58:05 +12:00
$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,
2024-04-15 08:42:31 +12:00
'package_max_length' => $payloadSize,
'buffer_output_size' => $payloadSize,
2024-04-14 20:58:05 +12:00
// 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-15 15:59:02 +12:00
global $global, $container;
2024-04-14 20:58:05 +12:00
http::onStart()
->inject('authorization')
2024-04-15 17:29:32 +12:00
->inject('cache')
->inject('pools')
2024-04-14 20:58:05 +12:00
->inject('connections')
2024-04-15 17:29:32 +12:00
->action(function (Authorization $authorization, Cache $cache, array $pools, Connections $connections) {
try {
// wait for database to be ready
$attempts = 0;
$max = 15;
$sleep = 2;
do {
try {
$attempts++;
$pool = $pools['pools-console-main']['pool'];
$dsn = $pools['pools-console-main']['dsn'];
$connection = $pool->get();
$connections->add($connection, $pool);
$adapter = match ($dsn->getScheme()) {
'mariadb' => new MariaDB($connection),
'mysql' => new MySQL($connection),
default => null
};
$adapter->setDatabase($dsn->getPath());
$dbForConsole = new Database($adapter, $cache);
$dbForConsole->setAuthorization($authorization);
$dbForConsole
->setNamespace('_console')
->setMetadata('host', \gethostname())
->setMetadata('project', 'console')
->setTimeout(APP_DATABASE_TIMEOUT_MILLISECONDS);
$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);
Console::success('[Setup] - Server database init started...');
2024-04-14 20:58:05 +12:00
try {
2024-04-15 17:29:32 +12:00
Console::success('[Setup] - Creating database: appwrite...');
$dbForConsole->create();
2024-04-14 20:58:05 +12:00
} catch (\Throwable $e) {
2024-04-15 17:29:32 +12:00
Console::success('[Setup] - Skip: metadata table already exists');
return true;
2024-04-14 20:58:05 +12:00
}
2021-07-05 03:14:39 +12:00
2024-04-15 17:29:32 +12:00
if ($dbForConsole->getCollection(Audit::COLLECTION)->isEmpty()) {
$audit = new Audit($dbForConsole, $authorization);
$audit->setup();
}
2022-09-13 02:09:28 +12:00
2024-04-15 17:29:32 +12:00
if ($dbForConsole->getCollection(TimeLimit::COLLECTION)->isEmpty()) {
$abuse = new TimeLimit("", 0, 1, $dbForConsole, $authorization);
$abuse->setup();
}
2021-07-05 00:05:46 +12:00
2024-04-15 17:29:32 +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-01-10 19:31:33 +13:00
2024-04-15 17:29:32 +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'] ?? ''
]);
}
2024-04-15 17:29:32 +12: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'],
]);
}
2022-05-24 02:54:50 +12:00
2024-04-15 17:29:32 +12:00
$dbForConsole->createCollection($key, $attributes, $indexes);
2024-04-14 20:58:05 +12:00
}
2024-04-15 17:29:32 +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.');
}
2024-04-15 17:29:32 +12: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-07-10 01:41:14 +12:00
2024-04-15 17:29:32 +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-04-14 20:58:05 +12:00
2024-04-15 17:29:32 +12:00
$dbForConsole->createCollection('bucket_' . $bucket->getInternalId(), $attributes, $indexes);
2024-04-14 20:58:05 +12:00
}
2024-03-09 01:57:20 +13:00
2024-04-15 17:29:32 +12:00
$connections->reclaim();
2024-03-09 01:57:20 +13:00
2024-04-15 17:29:32 +12:00
Console::success('[Setup] - Server database init completed...');
Console::success('Server started successfully');
} catch (\Throwable $e) {
Console::warning('Database not ready: ' . $e->getMessage());
exit(1);
2024-04-14 20:58:05 +12:00
}
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();