mirror of
https://github.com/meshcore-dev/map.meshcore.dev.git
synced 2026-04-20 22:13:50 +00:00
Add files via upload
Aded proxy file to get rid of CORS problems
This commit is contained in:
parent
50c4e4d022
commit
b578dbafd5
1 changed files with 62 additions and 0 deletions
62
proxy.php
Normal file
62
proxy.php
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
header('Content-Type: application/json');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type');
|
||||
|
||||
// Handle preflight OPTIONS request
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||
http_response_code(200);
|
||||
exit();
|
||||
}
|
||||
|
||||
$apiUrl = 'https://map.meshcore.dev/api/v1/nodes';
|
||||
|
||||
// Initialize cURL
|
||||
$ch = curl_init();
|
||||
|
||||
// Set the URL
|
||||
curl_setopt($ch, CURLOPT_URL, $apiUrl);
|
||||
|
||||
// Set request method
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
||||
|
||||
// Set headers
|
||||
$headers = [
|
||||
'Content-Type: application/json',
|
||||
'User-Agent: MeshCore Map Proxy'
|
||||
];
|
||||
|
||||
// Forward request body for POST requests
|
||||
if ($method === 'POST') {
|
||||
$input = file_get_contents('php://input');
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $input);
|
||||
}
|
||||
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
||||
|
||||
// Execute request
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
|
||||
// Check for cURL errors
|
||||
if (curl_error($ch)) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => 'Proxy error: ' . curl_error($ch)]);
|
||||
curl_close($ch);
|
||||
exit();
|
||||
}
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
// Set the HTTP response code
|
||||
http_response_code($httpCode);
|
||||
|
||||
// Return the response
|
||||
echo $response;
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue