-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_app.php
More file actions
245 lines (216 loc) · 7.26 KB
/
Copy pathexample_app.php
File metadata and controls
245 lines (216 loc) · 7.26 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
// Example IndexPHP Application
// This demonstrates the unique features of IndexPHP
// Global _index.php - handles all routing
$uri = $_SERVER['REQUEST_URI'];
$method = $_SERVER['REQUEST_METHOD'];
// API endpoints
if (strpos($uri, '/api/') === 0) {
header('Content-Type: application/json');
// QUIC connection info endpoint
if ($uri === '/api/quic/info') {
echo json_encode(quic_get_connection_info());
exit;
}
// Streaming endpoint
if ($uri === '/api/stream') {
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
// Stream data using QUIC
for ($i = 0; $i < 100; $i++) {
$data = [
'id' => $i,
'time' => microtime(true),
'value' => sin($i / 10) * 100
];
// Broadcast to all QUIC streams
quic_broadcast(json_encode($data));
echo "data: " . json_encode($data) . "\n\n";
ob_flush();
flush();
usleep(50000); // 50ms
}
exit;
}
// REST API example
if (preg_match('/^\/api\/items\/(\d+)$/', $uri, $matches)) {
$id = $matches[1];
switch ($method) {
case 'GET':
echo json_encode(['id' => $id, 'name' => "Item $id"]);
break;
case 'PUT':
$input = json_decode(file_get_contents('php://input'), true);
echo json_encode(['id' => $id, 'updated' => true, 'data' => $input]);
break;
case 'DELETE':
echo json_encode(['id' => $id, 'deleted' => true]);
break;
default:
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);
}
exit;
}
}
// Static file serving is handled automatically by IndexPHP
// PHP files are executed, others are served directly
// For the root path, show a demo page
if ($uri === '/' || $uri === '/index.php'):
?>
<!DOCTYPE html>
<html>
<head>
<title>IndexPHP Demo</title>
<style>
body {
font-family: -apple-system, system-ui, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background: #f5f5f5;
}
.card {
background: white;
border-radius: 8px;
padding: 20px;
margin-bottom: 20px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
h1 { color: #333; }
h2 { color: #666; }
pre {
background: #f0f0f0;
padding: 15px;
border-radius: 4px;
overflow-x: auto;
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
}
button {
background: #007AFF;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
}
button:hover { background: #0051D5; }
#stream-output {
height: 200px;
overflow-y: auto;
background: #f9f9f9;
padding: 10px;
border-radius: 4px;
font-family: monospace;
font-size: 12px;
}
</style>
</head>
<body>
<h1>🚀 IndexPHP Demo</h1>
<p>High-performance QUIC-enabled PHP server</p>
<div class="grid">
<div class="card">
<h2>Server Information</h2>
<pre><?php
$info = [
'Server' => $_SERVER['SERVER_SOFTWARE'] ?? 'IndexPHP/1.0',
'Protocol' => 'HTTP/3 (QUIC)',
'PHP Version' => PHP_VERSION,
'Request Method' => $_SERVER['REQUEST_METHOD'],
'Request URI' => $_SERVER['REQUEST_URI'],
'Server Time' => date('Y-m-d H:i:s')
];
foreach ($info as $key => $value) {
echo htmlspecialchars("$key: $value\n");
}
?></pre>
</div>
<div class="card">
<h2>QUIC Connection Info</h2>
<pre id="quic-info">Loading...</pre>
<button onclick="updateQuicInfo()">Refresh</button>
</div>
</div>
<div class="card">
<h2>Real-time Streaming Demo</h2>
<p>Uses QUIC multiplexed streams for efficient data transmission</p>
<button onclick="startStream()">Start Stream</button>
<button onclick="stopStream()">Stop Stream</button>
<div id="stream-output"></div>
</div>
<div class="card">
<h2>REST API Example</h2>
<p>Test the REST endpoints:</p>
<pre>
GET /api/items/123
PUT /api/items/123
DELETE /api/items/123
</pre>
<button onclick="testAPI()">Test API</button>
<pre id="api-output"></pre>
</div>
<script>
async function updateQuicInfo() {
const response = await fetch('/api/quic/info');
const info = await response.json();
document.getElementById('quic-info').textContent = JSON.stringify(info, null, 2);
}
let eventSource = null;
function startStream() {
if (eventSource) return;
const output = document.getElementById('stream-output');
output.innerHTML = '';
eventSource = new EventSource('/api/stream');
eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
const line = `[${data.id}] t=${data.time.toFixed(3)} value=${data.value.toFixed(2)}\n`;
output.textContent += line;
output.scrollTop = output.scrollHeight;
};
eventSource.onerror = () => {
output.textContent += '[Stream ended]\n';
eventSource.close();
eventSource = null;
};
}
function stopStream() {
if (eventSource) {
eventSource.close();
eventSource = null;
}
}
async function testAPI() {
const output = document.getElementById('api-output');
output.textContent = 'Testing API endpoints...\n\n';
// Test GET
const getResp = await fetch('/api/items/123');
output.textContent += 'GET /api/items/123:\n';
output.textContent += await getResp.text() + '\n\n';
// Test PUT
const putResp = await fetch('/api/items/123', {
method: 'PUT',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({name: 'Updated Item'})
});
output.textContent += 'PUT /api/items/123:\n';
output.textContent += await putResp.text() + '\n\n';
// Test DELETE
const deleteResp = await fetch('/api/items/123', {
method: 'DELETE'
});
output.textContent += 'DELETE /api/items/123:\n';
output.textContent += await deleteResp.text() + '\n';
}
// Initial load
updateQuicInfo();
</script>
</body>
</html>
<?php
endif;
?>