Add /servers/surprise endpoint
This commit is contained in:
parent
0e4d5a035f
commit
d0102327c0
|
@ -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
|
||||
|
||||
```
|
||||
|
|
39
src/classes/Controllers/Api/Surprise.php
Normal file
39
src/classes/Controllers/Api/Surprise.php
Normal 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);
|
||||
}
|
||||
}
|
16
src/classes/Routes/Http/Surprise.php
Normal file
16
src/classes/Routes/Http/Surprise.php
Normal 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);
|
||||
}
|
||||
}
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue