Skip to content

Commit a6f6424

Browse files
Merge pull request #83 from walters-labs/migrate-towards-REST-style-API
create `api/v1/index.php` router
2 parents 50dfb09 + b04fc9b commit a6f6424

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

api/v1/index.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
header('Content-Type: application/json');
3+
4+
$method = $_SERVER['REQUEST_METHOD'];
5+
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
6+
7+
// Strip /api/v1 prefix
8+
$path = preg_replace('#^/api/v1#', '', $path);
9+
10+
// Only allow POST for certain endpoints, GET for health
11+
if ($method === 'POST') {
12+
switch ($path) {
13+
case '/keys':
14+
include __DIR__ . '/../keygen.php';
15+
break;
16+
17+
case '/encrypt':
18+
include __DIR__ . '/../encrypt.php';
19+
break;
20+
21+
case '/decrypt':
22+
include __DIR__ . '/../decrypt.php';
23+
break;
24+
25+
default:
26+
http_response_code(404);
27+
echo json_encode(['error' => 'Not Found']);
28+
}
29+
} elseif ($method === 'GET' && $path === '/health') {
30+
echo json_encode(['status' => 'ok']);
31+
} else {
32+
http_response_code(405);
33+
echo json_encode(['error' => 'Method Not Allowed']);
34+
}

0 commit comments

Comments
 (0)