mirror of
https://codeberg.org/vlw/victorwesterlund.com.git
synced 2025-09-14 11:33:41 +02:00
Added core API router
Added basic PHP script to route and handle exceptions from API calls.
This commit is contained in:
parent
af9fbba1e4
commit
790d65be6d
6 changed files with 39 additions and 7 deletions
4
api/favicon.ico/default.php
Normal file
4
api/favicon.ico/default.php
Normal 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
BIN
api/favicon.ico/icon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
4
api/ping/default.php
Normal file
4
api/ping/default.php
Normal file
|
@ -0,0 +1,4 @@
|
|||
<?php
|
||||
|
||||
echo "Pong";
|
||||
header("Content-Type: text/plain");
|
|
@ -1,5 +1,29 @@
|
|||
<?php
|
||||
|
||||
http_response_code("503 Service Unavailable");
|
||||
header("Content-Type: text/plain");
|
||||
echo "Not available yet";
|
||||
try {
|
||||
$file = $_SERVER["REQUEST_URI"].".php";
|
||||
|
||||
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);
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
# == victorwesterlund.com ==
|
||||
# -- victorwesterlund.com --
|
||||
|
||||
server {
|
||||
listen 80;
|
||||
|
@ -37,7 +37,7 @@ server {
|
|||
}
|
||||
}
|
||||
|
||||
# == api.victorwesterlund.com ==
|
||||
# -- api.victorwesterlund.com --
|
||||
|
||||
server {
|
||||
listen 80;
|
||||
|
@ -59,8 +59,8 @@ server {
|
|||
|
||||
root /var/www/github_victorwesterlund_victorwesterlund.com/api;
|
||||
|
||||
location \* {
|
||||
try_files /router.php$request_uri =404;
|
||||
location / {
|
||||
try_files /router.php =404;
|
||||
add_header Access-Control-Allow-Origin *;
|
||||
include snippets/fastcgi-php-router.conf;
|
||||
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
|
||||
|
|
Loading…
Add table
Reference in a new issue