1
0
Fork 0
mirror of synced 2024-06-01 18:39:57 +12:00
appwrite/app/http.php

113 lines
3.2 KiB
PHP
Raw Normal View History

2020-06-26 21:54:37 +12:00
<?php
require_once __DIR__.'/../vendor/autoload.php';
2020-07-07 17:01:27 +12:00
use Appwrite\Swoole\Files;
use Appwrite\Swoole\Request;
use Appwrite\Swoole\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;
2020-07-05 10:24:09 +12:00
// xdebug_start_trace('/tmp/trace');
2020-07-12 20:38:35 +12:00
ini_set('memory_limit','512M');
2020-06-27 00:27:58 +12:00
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
2020-06-26 21:54:37 +12:00
2020-07-18 18:16:22 +12:00
sleep(2);
2020-07-04 03:14:06 +12:00
2020-07-03 18:08:46 +12:00
$http = new Server("0.0.0.0", 80);
2020-07-01 20:55:14 +12:00
2020-07-20 03:17:57 +12:00
$payloadSize = max(4000000 /* 4mb */, App::getEnv('_APP_STORAGE_LIMIT', 100000000));
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,
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...');
});
2020-07-20 03:17:57 +12:00
$http->on('start', function (Server $http) use ($payloadSize) {
Console::success('Server started succefully (max payload is '.$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
});
2020-07-07 07:29:13 +12:00
Files::load(__DIR__ . '/../public');
2020-07-29 19:29:34 +12:00
include __DIR__ . '/controllers/general.php';
2020-07-01 20:55:14 +12:00
2020-07-08 05:59:42 +12:00
$domain = App::getEnv('_APP_DOMAIN', '');
Console::info('Issuing a TLS certificate for the master domain ('.$domain.') in 30 seconds.
Make sure your domain points to your server IP or restart your Appwrite server to try again.'); // TODO move this to installation script
ResqueScheduler::enqueueAt(\time() + 30, 'v1-certificates', 'CertificatesV1', [
'document' => [],
'domain' => $domain,
'validateTarget' => false,
'validateCNAME' => false,
]);
2020-07-02 10:34:05 +12:00
$http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) {
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;
}
2020-07-01 20:55:14 +12:00
$app = new App('Asia/Tel_Aviv');
2020-06-27 00:27:58 +12:00
try {
2020-07-01 20:55:14 +12:00
$app->run($request, $response);
2020-06-27 00:27:58 +12:00
} catch (\Throwable $th) {
2020-07-02 10:34:05 +12:00
if(App::isDevelopment()) {
2020-07-03 00:24:39 +12:00
var_dump(get_class($th));
2020-07-02 10:34:05 +12:00
var_dump($th->getMessage());
var_dump($th->getFile());
var_dump($th->getLine());
$swooleResponse->end('error: '.$th->getMessage());
}
$swooleResponse->end('500: Server Error');
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();