TelegramApiServer/bootstrap.php

132 lines
4.0 KiB
PHP
Raw Normal View History

<?php
2020-03-11 00:20:14 +01:00
2024-06-30 16:12:30 +02:00
use Amp\Future\UnhandledFutureError;
use Amp\SignalException;
2024-06-30 16:15:51 +02:00
use Amp\Sql\SqlException;
2024-06-30 16:12:30 +02:00
use danog\MadelineProto\SecurityException;
2023-05-23 23:40:47 +02:00
use Revolt\EventLoop;
2020-02-10 03:26:11 +01:00
use TelegramApiServer\Logger;
2020-10-03 01:50:56 +02:00
use TelegramApiServer\Migrations\EnvUpgrade;
2020-02-10 03:26:11 +01:00
$root = __DIR__;
2020-05-17 22:19:21 +02:00
const ENV_VERSION='1';
2020-03-11 00:20:14 +01:00
//Composer init
{
if (!file_exists($root . '/vendor/autoload.php')) {
if (file_exists(__DIR__ . '/../../..' . '/vendor/autoload.php')) {
$root = __DIR__ . '/../../..';
} else {
system('composer install -o --no-dev');
}
}
define('ROOT_DIR', $root);
chdir(ROOT_DIR);
require_once ROOT_DIR . '/vendor/autoload.php';
}
2020-03-11 00:20:14 +01:00
//Config init
{
if (!getenv('SERVER_ADDRESS')) {
2020-10-03 01:50:56 +02:00
2020-06-25 00:33:18 +02:00
$envFile = $options['env'];
if (empty($envFile)) {
throw new InvalidArgumentException('Env file not defined');
2020-03-11 00:20:14 +01:00
}
2020-06-07 21:03:19 +02:00
$envPath = ROOT_DIR . '/' . $envFile;
$envPathExample = $envPath . '.example';
if (!is_file($envPath) || filesize($envPath) === 0) {
2020-06-25 00:33:18 +02:00
if (!is_file($envPathExample) || filesize($envPathExample) === 0) {
throw new InvalidArgumentException("Env files not found or empty: {$envPath}, {$envPathExample}");
}
2020-06-07 21:03:19 +02:00
//Dont use copy because of docker symlinks
$envContent = file_get_contents($envPathExample);
2021-11-22 23:29:41 +01:00
if (strlen($envContent) > 0) {
$result = file_put_contents($envPath, $envContent);
if (!$result) {
throw new RuntimeException("Cant write file: {$envPath}");
}
} else {
throw new RuntimeException("Cant read file: {$envPathExample}");
}
2020-06-07 21:03:19 +02:00
}
Dotenv\Dotenv::createImmutable(ROOT_DIR, $envFile)->load();
2020-05-17 22:19:21 +02:00
if (getenv('VERSION') !== ENV_VERSION) {
2020-06-25 00:33:18 +02:00
Logger::getInstance()->critical("Env version mismatch. Update {$envPath} from {$envPathExample}", [
2020-05-17 22:19:21 +02:00
'VERSION in .env' => getenv('VERSION'),
'required' => ENV_VERSION
]);
throw new RuntimeException('.env version mismatch');
}
2020-03-11 00:20:14 +01:00
}
}
2020-02-10 03:26:11 +01:00
2023-05-23 23:40:47 +02:00
$setMemLimit = function() {
if ($memoryLimit = getenv('MEMORY_LIMIT')) {
ini_set('memory_limit', $memoryLimit);
}
};
$setMemLimit();
EventLoop::repeat(60.0, $setMemLimit);
2020-05-08 00:16:35 +02:00
2020-06-13 00:44:52 +02:00
if ($timezone = getenv('TIMEZONE')) {
date_default_timezone_set($timezone);
}
2020-02-10 03:26:11 +01:00
if (!function_exists('debug')) {
function debug(string $message, array $context) {
Logger::getInstance()->debug($message, $context);
}
}
if (!function_exists('info')) {
function info(string $message, array $context = []) {
Logger::getInstance()->info($message, $context);
}
}
if (!function_exists('notice')) {
function notice($message, array $context = []) {
Logger::getInstance()->notice($message, $context);
}
}
if (!function_exists('warning')) {
function warning(string $message, array $context = []) {
Logger::getInstance()->warning($message, $context);
}
}
if (!function_exists('error')) {
function error(string $message, array $context = []) {
Logger::getInstance()->error($message, $context);
}
}
if (!function_exists('critical')) {
function critical(string $message, array $context = []) {
Logger::getInstance()->critical($message, $context);
}
}
if (!function_exists('alert')) {
function alert(string $message, array $context = []) {
Logger::getInstance()->alert($message, $context);
}
}
if (!function_exists('emergency')) {
function emergency(string $message, array $context = []) {
Logger::getInstance()->emergency($message, $context);
}
}
2024-06-30 16:12:30 +02:00
EventLoop::setErrorHandler(function (\Throwable $e) {
if ($e instanceof UnhandledFutureError) {
$e = $e->getPrevious();
}
2024-06-30 16:15:51 +02:00
if ($e instanceof SecurityException || $e instanceof SignalException || $e instanceof SqlException) {
2024-06-30 16:12:30 +02:00
throw $e;
}
if (str_starts_with($e->getMessage(), 'Could not connect to DC ')) {
throw $e;
}
emergency((string) $e);
});