forked from phalcon/rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server
172 lines (147 loc) · 5.13 KB
/
server
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env php
<?php
// Copied from <https://github.com/limingxinleo>
define('ROOT_PATH', __DIR__);
define('APP_PATH', ROOT_PATH);
define('IS_CLI', false);
define('ENGINE', 'SWOOLE');
use Phalcon\Di\FactoryDefault;
use Canvas\Bootstrap\Swoole;
use Canvas\Http\SwooleResponse;
use Canvas\Exception\ServerErrorHttpException;
$script = $argv[0];
if (empty($argv[1])) {
echo './server [start|restart|reload|stop|status]' . PHP_EOL;
exit;
}
$action = $argv[1];
//auto load
require_once __DIR__ . '/vendor/autoload.php';
/**
* Get config service for use in inline setup below.
*/
$pidDir = ROOT_PATH;
$logDir = ROOT_PATH . '/logs';
//move to config
$host = '0.0.0.0';
$port = 8081;
$pidFile = $pidDir . 'swoole_http_server.pid';
$logFile = $logDir . 'swoole_http_server.log';
$pid = 0;
if (file_exists($pidFile)) {
$pid = intval(file_get_contents($pidFile));
if (!swoole_process::kill($pid, 0)) {
$pid = 0;
}
}
switch ($action) {
case 'restart':
if ($pid > 0) {
swoole_process::kill($pid);
while (swoole_process::kill($pid, 0)) {
}
}
// no break
case 'start':
$http = new swoole_http_server($host, $port);
/**
* @todo move to .env or another config file
*/
$http->set([
'dispatch_mode' => 2,
'worker_num' => 10,
'max_request' => 10000,
'log_file' => $logFile,
'log_level' => 5,
'pid_file' => $pidFile,
'open_tcp_nodelay' => 1,
'daemonize' => 0,
]);
$http->on('workerStart', function () {
/** @var FactoryDefault $di */
require __DIR__ . '/library/Core/autoload.php';
});
$http->on('request', function ($request, $response) {
try {
//OPTION Request
if ($request->server['request_method'] == 'OPTIONS') {
$response->status(200);
$response->end();
return;
};
$bootstrap = new Swoole();
$bootstrap->setup();
$SRequest = $bootstrap->getContainer()->get('request');
$SResponse = $bootstrap->getContainer()->get('response');
$SResponse->init($response);
$SRequest->init($request);
$bootstrap->run();
} catch (Throwable $e) {
//Server errores FATAL
$httpCode = (method_exists($e, 'getHttpCode')) ? $e->getHttpCode() : 400;
$httpMessage = (method_exists($e, 'getHttpMessage')) ? $e->getHttpMessage() : 'Bad Request';
$data = (method_exists($e, 'getData')) ? $e->getData() : [];
//if we get a exception before response
if (!is_object($SResponse)) {
$SResponse = new SwooleResponse();
$SResponse->init($response);
}
$SResponse->setHeader('Access-Control-Allow-Origin', '*'); //@todo check why this fails on nginx
$SResponse->setStatusCode($httpCode, $httpMessage);
$SResponse->setContentType('application/json');
$content = '';
$timestamp = date('c');
$hash = sha1($timestamp . $content);
$eTag = sha1($content);
$jsonapi = [
'jsonapi' => [
'version' => '1.0',
],
];
$meta = [
'meta' => [
'timestamp' => $timestamp,
'hash' => $hash,
]
];
$content = [
'errors' => [
'type' => $httpMessage,
'message' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
'data' => $data,
],
];
$data = $jsonapi + $content + $meta;
$SResponse->setJsonContent($data);
if ($e instanceof ServerErrorHttpException && getenv('PRODUCTION')) {
$bootstrap->getContainer()->getLog()->error($e->getTraceAsString());
}
$SResponse->send();
}
});
echo 'swoole http server start.' . PHP_EOL;
$http->start();
break;
case 'reload':
if ($pid > 0 && swoole_process::kill($pid, SIGUSR1)) {
echo 'swoole http server reload successed.' . PHP_EOL;
} else {
echo 'swoole http server is not running' . PHP_EOL;
}
break;
case 'stop':
if ($pid > 0 && swoole_process::kill($pid)) {
echo 'swoole http server stop successed.' . PHP_EOL;
} else {
echo 'swoole http server is not running' . PHP_EOL;
}
break;
case 'status':
if ($pid > 0) {
echo 'swoole http server is running. master pid is ' . $pid . PHP_EOL;
} else {
echo 'swoole http server is not running' . PHP_EOL;
}
break;
}