Repository navigation
Expand file tree
/
Copy pathindex.php
More file actions
118 lines (90 loc) · 4.18 KB
/
Copy pathindex.php
File metadata and controls
118 lines (90 loc) · 4.18 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
<?php
// --------------------------------------------------------------------------------
// [ INIT ] ****************************
// CORS
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
header('Access-Control-Allow-Origin: http://localhost:5173');
header('Access-Control-Allow-Methods: PUT, DELETE'); // Specify allowed methods
header('Access-Control-Allow-Credentials: true');
// Check if the request includes additional headers and include them in the response
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
header('Access-Control-Allow-Headers: ' . $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
}
exit; // Stop further execution
}
// For regular requests
header('Access-Control-Allow-Origin: http://localhost:5173');
header('Access-Control-Allow-Headers: *');
header('Access-Control-Allow-Methods: *');
header('Access-Control-Allow-Credentials: true');
header('Content-Type: application/json; charset=UTF-8');
// Error Handling
error_reporting(-1);
ini_set('display_errors', 1);
// Initial files
require __DIR__ . '/vendor/autoload.php'; // Classes Autoloader.
require __DIR__ . '/app/config/config.php'; // Initial configs.
// Use of initial libraries
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use RequestHandlerInterface as Handler;
use Slim\Factory\AppFactory;
use Slim\Routing\RouteCollectorProxy;
use Model\Utilities\Log;
// Instantiating of the App
$app = AppFactory::create();
// Error middleware
$errorMiddleware = function ($request, $exception, $displayErrorDetails) use ($app)
{
$statusCode = 500;
$errorMessage = $exception->getMessage();
$requestedUrl = $request->getUri()->getPath();
$httpMethod = $request->getMethod();
$response = $app->getResponseFactory()->createResponse($statusCode);
$response->getBody()->write(json_encode(['error' => $errorMessage]));
$trace = $exception->getTrace();
$errorFile = $trace[0]['file'] ?? 'unknown';
$errorLine = $trace[0]['line'] ?? 'unknown';
$logMessage = "Error: " . $errorMessage . "\n" . " (METHOD: " . $httpMethod . ", URL: " . $requestedUrl . ", FILE: " . $errorFile . ", LINE: " . $errorLine . ")";
Log::WriteLog('index_error.txt', $logMessage);
return $response->withHeader('Content-Type', 'application/json');
};
// Add error middleware
$app->addErrorMiddleware(true, true, true)->setDefaultErrorHandler($errorMiddleware);
// Add parse body
$app->addBodyParsingMiddleware();
// --------------------------------------------------------------------------------
// [ SERVER ] ****************************
/////////////////////////////////////////////////////////////
#region - - - TEST ROUTES - - -
$app->get('/test', function (Request $request, Response $response)
{
Log::WriteLog('test.txt', 'a');
$payload = json_encode(array('method' => 'GET', 'msg' => "GET /test working (.htacces file is present). - - - RPG PROJECT"));
$response->getBody()->write($payload);
return $response->withHeader('Content-Type', 'application/json');
});
$app->get('/test_browser', function (Request $request, Response $response)
{
$payload = json_encode($_SERVER['HTTP_USER_AGENT']);
$response->getBody()->write($payload);
return $response->withHeader('Content-Type', 'application/json');
});
#endregion
/////////////////////////////////////////////////////////////
#region - - - PUBLIC ROUTES - - -
// Returns your APP's name, which is set at the config.php file. By default the APP has no name and it's up to you to define one.
$app->get('/app_name', \Model\Services\Manager::class . '::ReturnAppName');
$app->get('[/]', \Model\Services\Manager::class . '::ReturnToFront');
$app->get('/get_random_coords', \Model\Services\MapManager::class . '::ReturnRandomCoords'); // CURRENTLY UNUSED
$app->get('/get_clip_by_coords', \Model\Services\MapManager::class . '::ReturnClip');
$app->get('/next_turn_pic', function($request, $response)
{
return \Model\Services\MapManager::ReturnArrival($request, $response, true);
});
$app->get('/next_turn_coords', function($request, $response)
{
return \Model\Services\MapManager::ReturnArrival($request, $response, false);
});
#endregion
$app->run();