This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
serve.php
77 lines (64 loc) · 2.22 KB
/
serve.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
<?php
// Copyright 2021 Peter Beverloo. All rights reserved.
// Use of this source code is governed by the MIT license, a copy of which can
// be found in the LICENSE file.
require __DIR__ . '/../vendor/autoload.php';
Header('Access-Control-Allow-Origin: *');
Header('Content-Type: application/json');
\Anime\ErrorHandler::Install();
$requestUri = $_SERVER['REQUEST_URI'];
$parameters = [];
if (str_contains($requestUri, '?')) {
[ $requestUri, $parameterString ] = explode('?', $requestUri, 2);
// Parse the |$parameterString| into |$parameters| as an associative array.
parse_str($parameterString, $parameters);
}
$api = new \Anime\Api($_SERVER['HTTP_HOST']);
$endpoint = null;
switch ($requestUri) {
case '/api/application':
$endpoint = new \Anime\Endpoints\ApplicationEndpoint;
break;
case '/api/auth':
$endpoint = new \Anime\Endpoints\AuthEndpoint;
break;
case '/api/avatar':
$endpoint = new \Anime\Endpoints\AvatarEndpoint;
break;
case '/api/content':
$endpoint = new \Anime\Endpoints\ContentEndpoint;
break;
case '/api/display':
$endpoint = new \Anime\Endpoints\DisplayEndpoint;
break;
case '/api/environment':
$endpoint = new \Anime\Endpoints\EnvironmentEndpoint;
break;
case '/api/event':
$endpoint = new \Anime\Endpoints\EventEndpoint;
break;
case '/api/nardo':
$endpoint = new \Anime\Endpoints\NardoEndpoint;
break;
case '/api/notes':
$endpoint = new \Anime\Endpoints\NotesEndpoint;
break;
case '/api/user':
$endpoint = new \Anime\Endpoints\UserEndpoint;
break;
}
if (!$endpoint) {
echo json_encode([ 'error' => 'Unknown API endpoint: ' . $requestUri ]);
exit;
}
$validationResult = $endpoint->validateInput($parameters, $_POST);
if ($validationResult !== true) {
$message = 'A validation error occurred while calling this API endpoint.';
if (is_string($validationResult))
$message = $validationResult;
echo json_encode([ 'error' => $message ]);
exit;
}
$flags = array_key_exists('pretty', $parameters) ? JSON_PRETTY_PRINT : 0;
echo json_encode($endpoint->execute($api, $parameters, $_POST), $flags);
exit;