Add /servers/surprise endpoint

This commit is contained in:
Hypolite Petovan 2019-02-03 09:30:31 -05:00
parent 0e4d5a035f
commit d0102327c0
4 changed files with 64 additions and 0 deletions

View file

@ -1,5 +1,13 @@
# Friendica Directory Protocol
## Surprise
```
GET /servers/surprise
```
Redirects to the base URL of a random server with open registration policy and above 75 health.
## Search
```

View file

@ -0,0 +1,39 @@
<?php
namespace Friendica\Directory\Controllers\Api;
use Friendica\Directory\Content\Pager;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
/**
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
class Surprise
{
/**
* @var \Atlas\Pdo\Connection
*/
private $atlas;
public function __construct(
\Atlas\Pdo\Connection $atlas
)
{
$this->atlas = $atlas;
}
public function render(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args): \Slim\Http\Response
{
$redirectUrl = '';
$sql = 'SELECT base_url FROM server WHERE reg_policy = "REGISTER_OPEN" AND health_score > 75 ORDER BY health_score DESC, RAND()';
$server = $this->atlas->fetchOne($sql);
if ($server) {
$redirectUrl = $server['base_url'];
}
return $response->withRedirect($redirectUrl);
}
}

View file

@ -0,0 +1,16 @@
<?php
namespace Friendica\Directory\Routes\Http;
/**
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
class Surprise extends BaseRoute
{
public function __invoke(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args): \Slim\Http\Response
{
return (new \Friendica\Directory\Controllers\Api\Surprise(
$this->container->atlas
))->render($request, $response, $args);
}
}

View file

@ -9,6 +9,7 @@ use Slim\Http\Response;
* @var $app \Slim\App
*/
$app->get('/servers/surprise', \Friendica\Directory\Routes\Http\Surprise::class);
$app->get('/servers', \Friendica\Directory\Routes\Web\Servers::class);
$app->get('/search[/{account_type}]', function (Request $request, Response $response, $args) {