Skip to content

Commit

Permalink
small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyMathys committed Feb 5, 2015
1 parent 1db915d commit 094d32c
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions server.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
<?php
if(isset($_SERVER['argv'][0]) && $_SERVER['argv'][0] === 'server.php') {
$scriptInvokedFromCli =
isset($_SERVER['argv'][0]) && $_SERVER['argv'][0] === 'server.php';

if($scriptInvokedFromCli) {
echo 'server listening on port 3000';
exec('php -S localhost:3000 -t public server.php');
} else {
return routeRequest();
}

$comments = file_get_contents('_comments.json');
switch($_SERVER["REQUEST_URI"]) {
case '/':
echo file_get_contents('./public/index.html');
break;
case '/comments.json':
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$commentsDecoded = json_decode($comments, true);
$commentsDecoded[] = ['author' => $_POST['author'],
'text' => $_POST['text']];
function routeRequest()
{
$comments = file_get_contents('_comments.json');
switch($_SERVER["REQUEST_URI"]) {
case '/':
echo file_get_contents('./public/index.html');
break;
case '/comments.json':
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$commentsDecoded = json_decode($comments, true);
$commentsDecoded[] = ['author' => $_POST['author'],
'text' => $_POST['text']];

$comments = json_encode($commentsDecoded);
file_put_contents('_comments.json', $comments);
}
header('Content-Type: application/json');
echo $comments;
break;
default:
return false;
$comments = json_encode($commentsDecoded);
file_put_contents('_comments.json', $comments);
}
header('Content-Type: application/json');
echo $comments;
break;
default:
return false;
}
}

0 comments on commit 094d32c

Please sign in to comment.