Skip to content

Instantly share code, notes, and snippets.

@jszobody
Last active January 4, 2016 09:49
Show Gist options
  • Save jszobody/8604922 to your computer and use it in GitHub Desktop.
Save jszobody/8604922 to your computer and use it in GitHub Desktop.
<?php
function __autoload($class_name) {
$path = str_replace("\\","/",$class_name);
include "lib/" . $path . '.php';
}
$i = 0;
$path = false;
//file_put_contents("server.log",""); // I want a clean log file for this run
$app = function ($request, $response) use (&$i, &$path) {
$i++;
switch($request->getPath()) {
case '/healthcheck':
$json = '{"success": "true"}';
break;
case '/isIndexed':
if($path) $json = '{"success": "true"}';
else $json = '{"success": "false"}';
break;
case '/index':
$path = $request->getQuery()['path'];
//$json = json_encode("I need to index: $path");
$json = '{"success": "true"}';
break;
case '/':
$query = $request->getQuery()['q'];
//file_put_contents("server.log","QUERY: $query\n",FILE_APPEND);
//$json = json_encode("I need to search for: $query in $path");
$sed = str_replace("/",".",$path);
$cmd = "grep -rn $query $path|sed 's/$sed.//' |cut -d\":\" -f1-2";
exec($cmd,$output);
$result = array("success" => true);
foreach($output AS $found) $result['results'][] = $found;
//file_put_contents("server.log",print_r($result,true),FILE_APPEND);
$json = json_encode($result);
break;
default:
$json = json_encode("invalid request");
break;
}
$headers = array('Content-Type' => 'application/json');
$response->writeHead(200, $headers);
$response->end($json);
};
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket);
$http->on('request', $app);
$socket->listen(9090);
$loop->run();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment