Added core API router

Added basic PHP script to route and handle exceptions from API calls.
This commit is contained in:
Victor Westerlund 2020-12-21 15:55:13 +01:00
parent af9fbba1e4
commit 790d65be6d
6 changed files with 39 additions and 7 deletions

View file

@ -0,0 +1,4 @@
<?php
header("Header: image/x-icon");
echo file_get_contents("icon.ico");

BIN
api/favicon.ico/icon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View file

4
api/ping/default.php Normal file
View file

@ -0,0 +1,4 @@
<?php
echo "Pong";
header("Content-Type: text/plain");

View file

@ -1,5 +1,29 @@
<?php <?php
http_response_code("503 Service Unavailable"); try {
header("Content-Type: text/plain"); $file = $_SERVER["REQUEST_URI"].".php";
echo "Not available yet";
if(file_exists($file)) {
require $file;
return;
}
$api_root = explode("/",$_SERVER["REQUEST_URI"])[1];
if(is_dir($api_root) && file_exists($api_root."/default.php")) {
require $api_root."/default.php";
return;
}
throw new Exception("Invalid API");
} catch(Exception $error) {
http_response_code("400");
header("Content-Type: application/json");
$output = [
"status" => "error",
"message" => $error->getMessage()
];
echo json_encode($output);
}

View file

@ -1,4 +1,4 @@
# == victorwesterlund.com == # -- victorwesterlund.com --
server { server {
listen 80; listen 80;
@ -37,7 +37,7 @@ server {
} }
} }
# == api.victorwesterlund.com == # -- api.victorwesterlund.com --
server { server {
listen 80; listen 80;
@ -59,8 +59,8 @@ server {
root /var/www/github_victorwesterlund_victorwesterlund.com/api; root /var/www/github_victorwesterlund_victorwesterlund.com/api;
location \* { location / {
try_files /router.php$request_uri =404; try_files /router.php =404;
add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Origin *;
include snippets/fastcgi-php-router.conf; include snippets/fastcgi-php-router.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock; fastcgi_pass unix:/run/php/php7.3-fpm.sock;