forked from xtrime-ru/TelegramApiServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.php
132 lines (119 loc) · 4 KB
/
bootstrap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
use Amp\Future\UnhandledFutureError;
use Amp\SignalException;
use Amp\Sql\SqlException;
use danog\MadelineProto\SecurityException;
use Revolt\EventLoop;
use TelegramApiServer\Logger;
use TelegramApiServer\Migrations\EnvUpgrade;
$root = __DIR__;
const ENV_VERSION='1';
//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';
}
//Config init
{
if (!getenv('SERVER_ADDRESS')) {
$envFile = $options['env'];
if (empty($envFile)) {
throw new InvalidArgumentException('Env file not defined');
}
$envPath = ROOT_DIR . '/' . $envFile;
$envPathExample = $envPath . '.example';
if (!is_file($envPath) || filesize($envPath) === 0) {
if (!is_file($envPathExample) || filesize($envPathExample) === 0) {
throw new InvalidArgumentException("Env files not found or empty: {$envPath}, {$envPathExample}");
}
//Dont use copy because of docker symlinks
$envContent = file_get_contents($envPathExample);
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}");
}
}
Dotenv\Dotenv::createImmutable(ROOT_DIR, $envFile)->load();
if (getenv('VERSION') !== ENV_VERSION) {
Logger::getInstance()->critical("Env version mismatch. Update {$envPath} from {$envPathExample}", [
'VERSION in .env' => getenv('VERSION'),
'required' => ENV_VERSION
]);
throw new RuntimeException('.env version mismatch');
}
}
}
$setMemLimit = function() {
if ($memoryLimit = getenv('MEMORY_LIMIT')) {
ini_set('memory_limit', $memoryLimit);
}
};
$setMemLimit();
EventLoop::repeat(60.0, $setMemLimit);
if ($timezone = getenv('TIMEZONE')) {
date_default_timezone_set($timezone);
}
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);
}
}
EventLoop::setErrorHandler(function (\Throwable $e) {
if ($e instanceof UnhandledFutureError) {
$e = $e->getPrevious();
}
if ($e instanceof SecurityException || $e instanceof SignalException || $e instanceof SqlException) {
throw $e;
}
if (str_starts_with($e->getMessage(), 'Could not connect to DC ')) {
throw $e;
}
emergency((string) $e);
});