-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
68 lines (54 loc) · 1.93 KB
/
index.php
File metadata and controls
68 lines (54 loc) · 1.93 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
<?php
declare(strict_types=1);
use Backend\Domain\Response;
use Backend\Envs;
use Backend\Petitions;
use Backend\Router;
const ROOT_DIR = __DIR__;
require(ROOT_DIR . '/vendor/autoload.php');
try {
# Load environment variables
new Envs(ROOT_DIR);
# If DEV_MODE is true, allow showing errors for debugging
if ($_ENV['DEV_MODE']) {
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
}
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: " . $_ENV['CORS_ORIGIN']);
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Authorization, Accept, Access-Control-Request-Method");
header("Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, OPTIONS");
header("Allow: GET, POST, PUT, PATCH, DELETE, OPTIONS");
header('Content-Type: application/json');
# Handle preflight requests
if (($_SERVER['REQUEST_METHOD'] ?? '') === 'OPTIONS') {
http_response_code(200);
exit;
}
# Instance of the response
$response = new Response();
# Get request details
$method = Petitions::getHttpMethod();
$service = Petitions::getService();
# Default route
if (empty($service)) {
$response->setSuccess("API is running");
echo json_encode($response->getResponse());
exit;
}
$parameters = Petitions::getParameters();
# Initialize Router, validate the route and handle authentication
$router = new Router($method, $service);
$router->validateRoute();
$router->handleAuthentication();
# Execute the controller function
$result = $router->executeController($parameters);
# Return the response
echo json_encode($result);
exit;
} catch (Exception $e) {
$response->setError($e->getMessage(), (int) $e->getCode() ?: 500);
echo json_encode($response->getResponse());
exit;
}