forked from maxpoulin64/protondb-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.php
51 lines (43 loc) · 1.34 KB
/
router.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
<?php
require_once __DIR__.'/core.php';
// can't be bothered to set up an autoloader for such a tiny project
require_once __DIR__.'/includes/Router.php';
/**
* Redirects the user to a file on the filesystem
*/
function nginx($url = null) {
$url = $url ?: '/__internal'.$_SERVER['REQUEST_URI'];
header('X-Accel-Redirect: '.$url);
header('Content-Type:'); // otherwise nginx won't override it
}
/**
* Returns a JSON response
*/
function api_return_json($data, $status = 200) {
header('Content-Type: application/json');
http_response_code($status);
echo json_encode($data);
}
// Process API requests
$R = new Router();
$R->get('^/games/$', function() {
$db = connectDB();
$query = $db->query('SELECT `appId`, `title` FROM `games` ORDER BY `appId` ASC');
$query->execute();
api_return_json($query->fetchAll(PDO::FETCH_ASSOC));
});
$R->get('^/games/([0-9]+)/reports/$', function($appId) {
$db = connectDB();
// no point setting up the whole prepared queries for a one-off at the moment
$appId = (int)$appId;
$query = $db->query("SELECT * FROM `reports` WHERE `appId`=$appId ORDER BY `timestamp` DESC");
$query->execute();
api_return_json($query->fetchAll(PDO::FETCH_ASSOC));
});
// Dispatch request
if(!$R->dispatch(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH),
$_SERVER['REQUEST_METHOD'] )
) {
nginx(); // return hand to nginx if no match
}