124 lines
4.8 KiB
PHP
124 lines
4.8 KiB
PHP
<?php
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
|
|
$docs['general'][] = array("method"=>'GET', "href"=>"/", "name"=>"Nothing", "body"=>"{}");
|
|
$docs['recipe'][] = array("method"=>'GET', "href"=>"/recipe", "name"=>"Get all available recipes", "body"=>"{}");
|
|
$docs['recipe'][] = array("method"=>'POST', "href"=>"/recipe", "name"=>"Create recipe", "body"=>"{\n \"name\": \"string\",\n \"portions\": \"integer\",\n \"public\": \"boolean\" \n}");
|
|
|
|
|
|
|
|
function capitalizeFirst($input){
|
|
$input[0] = strtoupper($input[0]);
|
|
return $input;
|
|
}
|
|
?><!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset='UTF-8'>
|
|
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
|
|
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
|
|
|
|
<link rel='stylesheet' href='/css/bootstrap.min.css' type='text/css' />
|
|
<link rel='stylesheet' href='/css/index.css' type='text/css' />
|
|
|
|
<script src='/js/jquery-3.5.1.min.js'></script>
|
|
<script src='/js/popper.min.js'></script>
|
|
<script src='/js/bootstrap.min.js'></script>
|
|
<!--<script src='//cdn.jsdelivr.net/npm/marked/marked.min.js'></script>-->
|
|
<script src='/js/hammer.js'></script>
|
|
<script src='/js/hammer.jquery.js'></script>
|
|
<title>PaperBag API Docs</title>
|
|
</head>
|
|
<body>
|
|
|
|
<div class="container-md">
|
|
<h1>PaperBag API Docs</h1>
|
|
|
|
<div class="accordion mb-2" id="apiContents">
|
|
|
|
<?php
|
|
|
|
$count = 0;
|
|
foreach ($docs as $group=>$rows){
|
|
echo "<div><h2>".capitalizeFirst($group)."</h2></div>";
|
|
foreach ($rows as $k => $row){
|
|
$i = $count;
|
|
$count++;
|
|
|
|
switch ($row['method']){
|
|
case 'GET':
|
|
$methodClass = "bg-primary";
|
|
break;
|
|
case 'POST':
|
|
$methodClass = "bg-success";
|
|
break;
|
|
default:
|
|
$methodClass = "bg-warning";
|
|
}
|
|
?>
|
|
<div class="accordion-item">
|
|
<h2 class="accordion-header" id="heading<?=$i;?>">
|
|
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse<?=$i;?>" aria-expanded="true" aria-controls="collapse<?=$i;?>">
|
|
<div class="badge <?=$methodClass;?>" style="margin-right: 5px;"><?=$row['method'];?></div>
|
|
<div style="font-family: var(--bs-font-monospace); margin-right: 5px;"><?=$row['href'];?></div>
|
|
<div><strong><?=$row['name'];?></strong></div>
|
|
</button>
|
|
</h2>
|
|
<div id="collapse<?=$i;?>" class="accordion-collapse collapse" aria-labelledby="heading<?=$i;?>" data-bs-parent="#apiContents">
|
|
<div class="accordion-body">
|
|
<div class="apiBodyHolder" style="display: none;"><?=$row['body'];?></div>
|
|
<form class="apiExec">
|
|
<div class="row row-cols-2">
|
|
<div>
|
|
<input class="form-control" type="hidden" id="url" name="url" value="<?=$row['href'];?>">
|
|
<textarea class="form-control" class="apiBody" name="apiBody"><?=$row['body'];?></textarea>
|
|
<input type="submit">
|
|
</div>
|
|
<div class="apiExecResponse"></div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php }
|
|
} ?>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
$(".apiExec").on('submit', ev=>{
|
|
ev.preventDefault();
|
|
let formElem = $(ev.currentTarget);
|
|
let params = formElem.serializeArray();
|
|
|
|
let url = "."+params[0].value;
|
|
let body = JSON.parse( params[1].value );
|
|
|
|
if(url.length > 0 && typeof body === "object"){
|
|
|
|
$.post(url, body, resp => {
|
|
console.log(resp);
|
|
let response = JSON.stringify(resp, null, 4);
|
|
formElem.find(".apiExecResponse").html("<pre><code>"+response+"</code></pre>");
|
|
});
|
|
}
|
|
});
|
|
|
|
$('.accordion-collapse').on('show.bs.collapse', ev=>{
|
|
let currElem = $(ev.currentTarget);
|
|
let textarea = currElem.find('textarea');
|
|
|
|
textarea.val( currElem.find('.apiBodyHolder').html() );
|
|
|
|
setTimeout(()=> {
|
|
textarea.height( (textarea[0].scrollHeight)-12 );
|
|
}, 10);
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|