Skip to content

Commit 5bc9143

Browse files
committed
Updated server.php to use constants for JSON data
1 parent 7abbcd2 commit 5bc9143

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

frameworks/workerman/server.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
$http_worker->count = (int) shell_exec('nproc');
1515

1616
// benchmark data
17-
$jsonData = json_decode(file_get_contents('/data/dataset.json'), true);
17+
define('JSON_DATA', json_decode(file_get_contents('/data/dataset.json'), true));
18+
define('LARGE_JSON', largeJson());
1819

1920
function largeJson()
2021
{
@@ -25,16 +26,14 @@ function largeJson()
2526

2627
return json_encode(['items' => $data, 'count' => count($data)]);
2728
}
28-
$largeJson = largeJson();
2929

3030
$http_worker->onWorkerStart = static function () {
3131
DB::Init();
3232
};
3333

3434
// Data received
35-
$http_worker->onMessage = static function ($connection, $request) use ($jsonData, $largeJson) {
36-
$path = $request->path();
37-
switch ($path) {
35+
$http_worker->onMessage = static function ($connection, $request) {
36+
switch ($request->path()) {
3837
case '/pipeline':
3938
$connection->headers = ['Content-Type' => 'text/plain'];
4039
return $connection->send('ok');
@@ -50,7 +49,7 @@ function largeJson()
5049

5150
case '/json':
5251
$total = [];
53-
foreach ($jsonData as $item) {
52+
foreach (JSON_DATA as $item) {
5453
$item['total'] = $item['price'] * $item['quantity'];
5554
$total[] = $item;
5655
}
@@ -68,10 +67,10 @@ function largeJson()
6867
'Content-Type' => 'application/json',
6968
'Content-Encoding' => 'gzip'
7069
];
71-
return $connection->send(gzencode($largeJson, 1));
70+
return $connection->send(gzencode(LARGE_JSON, 1));
7271
}
7372

74-
$resp = new Response(200, ['Content-Type' => 'application/json'], $largeJson);
73+
$resp = new Response(200, ['Content-Type' => 'application/json'], LARGE_JSON);
7574
return $connection->send($resp);
7675

7776
case '/db':
@@ -85,8 +84,8 @@ function largeJson()
8584
}
8685

8786
// Serve static files
88-
if (str_starts_with($path, '/static/')) {
89-
$response = (new Response())->withFile('/data' . $path);
87+
if (str_starts_with($request->path(), '/static/')) {
88+
$response = (new Response())->withFile('/data' . $request->path());
9089
return $connection->send($response);
9190
}
9291

0 commit comments

Comments
 (0)