2020-06-26 21:54:37 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once __DIR__.'/../vendor/autoload.php';
|
|
|
|
|
2020-10-30 02:07:56 +13:00
|
|
|
use Appwrite\Utopia\Response;
|
2020-06-27 00:27:58 +12:00
|
|
|
use Swoole\Process;
|
|
|
|
use Swoole\Http\Server;
|
2020-06-26 21:54:37 +12:00
|
|
|
use Swoole\Http\Request as SwooleRequest;
|
|
|
|
use Swoole\Http\Response as SwooleResponse;
|
2020-06-27 00:27:58 +12:00
|
|
|
use Utopia\App;
|
|
|
|
use Utopia\CLI\Console;
|
2021-05-04 22:24:08 +12:00
|
|
|
use Utopia\Config\Config;
|
2021-07-26 02:51:04 +12:00
|
|
|
use Utopia\Database\Validator\Authorization;
|
2021-06-09 21:28:12 +12:00
|
|
|
use Utopia\Audit\Audit;
|
|
|
|
use Utopia\Abuse\Adapters\TimeLimit;
|
2021-07-05 00:05:46 +12:00
|
|
|
use Utopia\Database\Document;
|
2021-06-13 06:39:59 +12:00
|
|
|
use Utopia\Swoole\Files;
|
|
|
|
use Utopia\Swoole\Request;
|
2020-06-27 00:27:58 +12:00
|
|
|
|
2021-01-21 00:49:36 +13:00
|
|
|
$http = new Server("0.0.0.0", App::getEnv('PORT', 80));
|
2020-07-01 20:55:14 +12:00
|
|
|
|
2020-10-28 08:56:37 +13:00
|
|
|
$payloadSize = max(4000000 /* 4mb */, App::getEnv('_APP_STORAGE_LIMIT', 10000000 /* 10mb */));
|
2020-07-20 03:17:57 +12:00
|
|
|
|
2020-06-27 00:27:58 +12:00
|
|
|
$http
|
2020-06-26 21:54:37 +12:00
|
|
|
->set([
|
|
|
|
'open_http2_protocol' => true,
|
2020-07-07 07:29:13 +12:00
|
|
|
// 'document_root' => __DIR__.'/../public',
|
|
|
|
// 'enable_static_handler' => true,
|
2020-07-02 18:44:16 +12:00
|
|
|
'http_compression' => true,
|
|
|
|
'http_compression_level' => 6,
|
2020-07-20 03:17:57 +12:00
|
|
|
'package_max_length' => $payloadSize,
|
2021-05-25 22:19:49 +12:00
|
|
|
'buffer_output_size' => $payloadSize,
|
2020-06-26 21:54:37 +12:00
|
|
|
])
|
|
|
|
;
|
|
|
|
|
2020-06-27 00:27:58 +12:00
|
|
|
$http->on('WorkerStart', function($serv, $workerId) {
|
2020-07-08 05:59:42 +12:00
|
|
|
Console::success('Worker '.++$workerId.' started succefully');
|
2020-06-26 21:54:37 +12:00
|
|
|
});
|
|
|
|
|
2020-06-27 00:27:58 +12:00
|
|
|
$http->on('BeforeReload', function($serv, $workerId) {
|
2020-06-26 21:54:37 +12:00
|
|
|
Console::success('Starting reload...');
|
|
|
|
});
|
|
|
|
|
2020-06-27 00:27:58 +12:00
|
|
|
$http->on('AfterReload', function($serv, $workerId) {
|
2020-06-26 21:54:37 +12:00
|
|
|
Console::success('Reload completed...');
|
|
|
|
});
|
|
|
|
|
2021-05-07 09:35:05 +12:00
|
|
|
Files::load(__DIR__ . '/../public');
|
|
|
|
|
|
|
|
include __DIR__ . '/controllers/general.php';
|
2020-12-30 07:42:03 +13:00
|
|
|
|
2021-05-07 09:35:05 +12:00
|
|
|
$http->on('start', function (Server $http) use ($payloadSize, $register) {
|
2021-05-04 22:24:08 +12:00
|
|
|
$app = new App('UTC');
|
2021-06-17 08:32:10 +12:00
|
|
|
|
2021-07-02 08:35:09 +12:00
|
|
|
go(function() use ($register, $app) {
|
2021-07-21 06:59:58 +12:00
|
|
|
// wait for database to be ready
|
|
|
|
$attempts = 0;
|
2021-07-21 07:11:54 +12:00
|
|
|
$max = 10;
|
|
|
|
$sleep = 1;
|
|
|
|
|
2021-07-21 06:59:58 +12:00
|
|
|
do {
|
|
|
|
try {
|
|
|
|
$attempts++;
|
|
|
|
$db = $register->get('dbPool')->get();
|
|
|
|
$redis = $register->get('redisPool')->get();
|
|
|
|
break; // leave the do-while if successful
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
Console::warning("Database not ready. Retrying connection ({$attempts})...");
|
2021-07-21 07:11:54 +12:00
|
|
|
if ($attempts >= $max) {
|
2021-07-21 06:59:58 +12:00
|
|
|
throw new \Exception('Failed to connect to database: '. $e->getMessage());
|
|
|
|
}
|
2021-07-21 07:11:54 +12:00
|
|
|
sleep($sleep);
|
2021-07-21 06:59:58 +12:00
|
|
|
}
|
2021-07-21 07:11:54 +12:00
|
|
|
} while ($attempts < $max);
|
2021-07-02 08:35:09 +12:00
|
|
|
|
|
|
|
App::setResource('db', function () use (&$db) {
|
|
|
|
return $db;
|
|
|
|
});
|
|
|
|
|
|
|
|
App::setResource('cache', function () use (&$redis) {
|
|
|
|
return $redis;
|
|
|
|
});
|
|
|
|
|
|
|
|
App::setResource('app', function() use (&$app) {
|
|
|
|
return $app;
|
|
|
|
});
|
|
|
|
|
2021-07-21 06:59:58 +12:00
|
|
|
$dbForConsole = $app->getResource('dbForConsole'); /** @var Utopia\Database\Database $dbForConsole */
|
2021-05-04 22:24:08 +12:00
|
|
|
|
2021-07-02 08:35:09 +12:00
|
|
|
if(!$dbForConsole->exists()) {
|
|
|
|
Console::success('[Setup] - Server database init started...');
|
2021-07-05 03:14:39 +12:00
|
|
|
|
2021-07-02 08:35:09 +12:00
|
|
|
$collections = Config::getParam('collections2', []); /** @var array $collections */
|
|
|
|
|
|
|
|
$redis->flushAll();
|
|
|
|
|
|
|
|
$dbForConsole->create();
|
2021-07-05 03:14:39 +12:00
|
|
|
|
|
|
|
$audit = new Audit($dbForConsole);
|
2021-07-02 08:35:09 +12:00
|
|
|
$audit->setup();
|
|
|
|
|
|
|
|
$adapter = new TimeLimit("", 0, 1, $dbForConsole);
|
|
|
|
$adapter->setup();
|
|
|
|
|
|
|
|
foreach ($collections as $key => $collection) {
|
|
|
|
Console::success('[Setup] - Creating collection: ' . $collection['$id'] . '...');
|
|
|
|
|
2021-07-05 03:14:39 +12:00
|
|
|
$attributes = [];
|
|
|
|
$indexes = [];
|
|
|
|
|
|
|
|
foreach ($collection['attributes'] as $attribute) {
|
|
|
|
$attributes[] = new Document([
|
|
|
|
'$id' => $attribute['$id'],
|
|
|
|
'type' => $attribute['type'],
|
|
|
|
'size' => $attribute['size'],
|
|
|
|
'required' => $attribute['required'],
|
|
|
|
'signed' => $attribute['signed'],
|
|
|
|
'array' => $attribute['array'],
|
|
|
|
'filters' => $attribute['filters'],
|
|
|
|
]);
|
2021-07-02 08:35:09 +12:00
|
|
|
}
|
|
|
|
|
2021-07-05 03:14:39 +12:00
|
|
|
foreach ($collection['indexes'] as $index) {
|
|
|
|
$indexes[] = new Document([
|
|
|
|
'$id' => $index['$id'],
|
|
|
|
'type' => $index['type'],
|
|
|
|
'attributes' => $index['attributes'],
|
|
|
|
'lengths' => $index['lengths'],
|
|
|
|
'orders' => $index['orders'],
|
|
|
|
]);
|
2021-07-02 08:35:09 +12:00
|
|
|
}
|
2021-07-05 03:14:39 +12:00
|
|
|
|
|
|
|
$dbForConsole->createCollection($key, $attributes, $indexes);
|
|
|
|
|
2021-05-04 22:24:08 +12:00
|
|
|
}
|
|
|
|
|
2021-07-02 08:35:09 +12:00
|
|
|
Console::success('[Setup] - Server database init completed...');
|
|
|
|
}
|
|
|
|
});
|
2021-05-04 22:24:08 +12:00
|
|
|
|
2020-10-28 08:56:37 +13:00
|
|
|
Console::success('Server started succefully (max payload is '.number_format($payloadSize).' bytes)');
|
2020-07-09 03:08:14 +12:00
|
|
|
|
|
|
|
Console::info("Master pid {$http->master_pid}, manager pid {$http->manager_pid}");
|
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();
|
|
|
|
});
|
2020-06-26 21:54:37 +12:00
|
|
|
});
|
|
|
|
|
2021-06-12 04:09:46 +12:00
|
|
|
$http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) use ($register) {
|
2020-07-01 20:55:14 +12:00
|
|
|
$request = new Request($swooleRequest);
|
|
|
|
$response = new Response($swooleResponse);
|
|
|
|
|
2020-07-07 07:29:13 +12:00
|
|
|
if(Files::isFileLoaded($request->getURI())) {
|
2020-07-11 16:16:24 +12:00
|
|
|
$time = (60 * 60 * 24 * 365 * 2); // 45 days cache
|
2020-07-07 07:29:13 +12:00
|
|
|
|
|
|
|
$response
|
|
|
|
->setContentType(Files::getFileMimeType($request->getURI()))
|
|
|
|
->addHeader('Cache-Control', 'public, max-age='.$time)
|
|
|
|
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + $time).' GMT') // 45 days cache
|
|
|
|
->send(Files::getFileContents($request->getURI()))
|
|
|
|
;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-28 19:19:33 +12:00
|
|
|
$app = new App('UTC');
|
|
|
|
|
2021-06-12 04:09:46 +12:00
|
|
|
$db = $register->get('dbPool')->get();
|
|
|
|
$redis = $register->get('redisPool')->get();
|
|
|
|
|
2021-06-28 19:19:33 +12:00
|
|
|
App::setResource('db', function () use (&$db) {
|
2021-06-12 04:09:46 +12:00
|
|
|
return $db;
|
|
|
|
});
|
|
|
|
|
2021-06-28 19:19:33 +12:00
|
|
|
App::setResource('cache', function () use (&$redis) {
|
2021-06-12 04:09:46 +12:00
|
|
|
return $redis;
|
|
|
|
});
|
2020-07-01 20:55:14 +12:00
|
|
|
|
2020-06-27 00:27:58 +12:00
|
|
|
try {
|
2020-11-19 19:56:14 +13:00
|
|
|
Authorization::cleanRoles();
|
2021-06-12 06:23:16 +12:00
|
|
|
Authorization::setRole('role:all');
|
2020-11-19 19:56:14 +13:00
|
|
|
|
2020-07-01 20:55:14 +12:00
|
|
|
$app->run($request, $response);
|
2020-06-27 00:27:58 +12:00
|
|
|
} catch (\Throwable $th) {
|
2020-10-26 02:48:04 +13:00
|
|
|
Console::error('[Error] Type: '.get_class($th));
|
|
|
|
Console::error('[Error] Message: '.$th->getMessage());
|
|
|
|
Console::error('[Error] File: '.$th->getFile());
|
|
|
|
Console::error('[Error] Line: '.$th->getLine());
|
|
|
|
|
2021-07-12 22:04:00 +12:00
|
|
|
/**
|
|
|
|
* Reset Database connection if PDOException was thrown.
|
|
|
|
*/
|
2021-07-13 04:15:21 +12:00
|
|
|
if ($th instanceof PDOException) {
|
2021-07-12 22:04:00 +12:00
|
|
|
$db = null;
|
|
|
|
}
|
|
|
|
|
2020-07-02 10:34:05 +12:00
|
|
|
if(App::isDevelopment()) {
|
|
|
|
$swooleResponse->end('error: '.$th->getMessage());
|
|
|
|
}
|
2020-12-23 08:53:49 +13:00
|
|
|
else {
|
|
|
|
$swooleResponse->end('500: Server Error');
|
|
|
|
}
|
2021-06-12 04:09:46 +12:00
|
|
|
} finally {
|
|
|
|
/** @var PDOPool $dbPool */
|
|
|
|
$dbPool = $register->get('dbPool');
|
|
|
|
$dbPool->put($db);
|
|
|
|
|
|
|
|
/** @var RedisPool $redisPool */
|
|
|
|
$redisPool = $register->get('redisPool');
|
|
|
|
$redisPool->put($redis);
|
2020-06-27 00:27:58 +12:00
|
|
|
}
|
2020-06-26 21:54:37 +12:00
|
|
|
});
|
|
|
|
|
2020-10-20 05:54:21 +13:00
|
|
|
$http->start();
|