Created
January 30, 2019 14:48
-
-
Save asherkin/58c5fada57972e4d98b44e331c2e60c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require __dir__.'/../vendor/autoload.php'; | |
function uuid() { | |
$raw = openssl_random_pseudo_bytes(16); | |
$raw[6] = chr(ord($raw[6]) & 0x0f | 0x40); | |
$raw[8] = chr(ord($raw[8]) & 0x3f | 0x80); | |
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($raw), 4)); | |
} | |
$server_protocol = null; | |
if (isset($_SERVER['SERVER_PROTOCOL'])) { | |
$server_protocol = $_SERVER['SERVER_PROTOCOL']; | |
} | |
$selected = null; | |
if (isset($_GET['selected'])) { | |
$selected = $_GET['selected']; | |
} | |
$client = new Predis\Client(); | |
$id = uuid(); | |
$request = [ | |
'id' => $id, | |
]; | |
if ($selected) { | |
$request['selected'] = $selected; | |
} | |
$encoded = json_encode($request); | |
$client->lpush('scc.locator.request', $encoded); | |
$data = $client->brpop('scc.locator.response.'.$id, 10); | |
if ($data === null) { | |
$client->lrem('scc.locator.request', 0, $encoded); | |
if ($server_protocol !== null) | |
header($server_protocol.' 503 Service Unavailable'); | |
exit(); | |
} | |
header('Content-type: application/json'); | |
print($data[1].PHP_EOL); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment