-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.php
96 lines (83 loc) · 2.17 KB
/
server.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
<?php
require_once 'vendor/autoload.php';
use Mark\App;
use Workerman\Protocols\Http\Response;
$SIDECAR_ADDRESS = getenv('SIDECAR_ADDRESS', true) ? getenv('SIDECAR_ADDRESS') : 'http://localhost:5103';
$api = new App('http://0.0.0.0:5001');
$api->count = 4; // process count
// Action handler response sender
function sendResponse($content) {
return new Response(200, ['Content-Type' => 'application/json'], json_encode([
'response' => $content
]));
}
// Action handler
$api->post(
'/actions/hello',
function ($request) {
echo("Hello action called.\n");
return sendResponse('Hello from PHP!');
}
);
// Action handler
$api->post(
'/actions/welcome',
function ($request) {
echo("Welcome action called. Body:\n" . $request->rawBody());
$body = json_decode($request->rawBody(), true);
$params = $body['params'];
$meta = $body['meta'];
return sendResponse('Hello ' . $params['name'] . ' from PHP!');
}
);
// Event handler
$api->post(
'/events/sample.event',
function ($request) {
echo("Sample event happened.\n");
return new Response(200, [], "");
}
);
// Make a HTTP request
function httpPost($url, $data)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
if ($response == false) {
echo "Sidecar is not available. Exiting...\n";
exit(1);
}
return $response;
}
function registerServiceSchema() {
global $SIDECAR_ADDRESS;
echo("Registering services to Sidecar (" . $SIDECAR_ADDRESS . ")...\n");
$regRes = httpPost($SIDECAR_ADDRESS . '/v1/registry/services', '{
"name": "php-demo",
"settings": {
"baseUrl": "http://php-demo:5001"
},
"actions": {
"hello": "/actions/hello",
"welcome": {
"params": {
"name": "string|no-empty|trim"
},
"handler": "/actions/welcome"
}
},
"events": {
"sample.event": "/events/sample.event"
}
}');
echo("Response: " . $regRes . "\n");
}
// Register schema
registerServiceSchema();
// Start nano
$api->start();