Browse Source

fix multi server launch

main
ghost 10 months ago
parent
commit
6e1211fc22
  1. 4
      README.md
  2. 432
      src/server.php

4
README.md

@ -38,8 +38,8 @@ KevaChat is distributed chat platform for open, uncensored and privacy respectab
## Start ## Start
* `php src/server.php` * `php src/server.php 127.0.0.1`
## See also ## See also
* [web implementation](https://github.com/kevachat/webapp) * [KevaChat Web Application](https://github.com/kevachat/webapp)

432
src/server.php

@ -3,286 +3,282 @@
// Load dependencies // Load dependencies
require_once __DIR__ . '/../vendor/autoload.php'; require_once __DIR__ . '/../vendor/autoload.php';
// Scan host // Check arguments
foreach ((array) scandir(__DIR__ . '/../host') as $host) if (empty($argv[1]))
{ {
// Skip meta exit(_('Configured hostname required as argument!') . PHP_EOL);
if ($host == '.' || $host == '..' || is_file($host)) }
{
continue;
}
// Check host configured
if (!file_exists(__DIR__ . '/../host/' . $host . '/config.json'))
{
echo sprintf(
_('Host "%s" not configured!') . PHP_EOL,
$host
);
continue; // Check cert exists
} if (!file_exists(__DIR__ . '/../host/' . $argv[1] . '/cert.pem'))
{
// Check cert exists exit(
if (!file_exists(__DIR__ . '/../host/' . $host . '/cert.pem')) sprintf(
{
echo sprintf(
_('Certificate for host "%s" not found!') . PHP_EOL, _('Certificate for host "%s" not found!') . PHP_EOL,
$host $argv[1]
); )
);
continue; }
}
// Check key exists // Check key exists
if (!file_exists(__DIR__ . '/../host/' . $host . '/key.rsa')) if (!file_exists(__DIR__ . '/../host/' . $argv[1] . '/key.rsa'))
{ {
echo sprintf( exit(
sprintf(
_('Key for host "%s" not found!') . PHP_EOL, _('Key for host "%s" not found!') . PHP_EOL,
$host $argv[1]
);
continue;
}
// Init config
$config = json_decode(
file_get_contents(
__DIR__ . '/../host/' . $host . '/config.json'
) )
); );
}
// Init memory // Check host configured
$memory = new \Yggverse\Cache\Memory( if (!file_exists(__DIR__ . '/../host/' . $argv[1] . '/config.json'))
$config->memcached->server->host, {
$config->memcached->server->port, exit(
$config->memcached->server->namespace, sprintf(
$config->memcached->server->timeout _('Host "%s" not configured!') . PHP_EOL,
$argv[1]
)
); );
}
// Init config
$config = json_decode(
file_get_contents(
__DIR__ . '/../host/' . $argv[1] . '/config.json'
)
);
// Init memory
$memory = new \Yggverse\Cache\Memory(
$config->memcached->server->host,
$config->memcached->server->port,
$config->memcached->server->namespace,
$config->memcached->server->timeout
);
// Init server
$server = new \Yggverse\TitanII\Server();
$server->setCert(
__DIR__ . '/../host/' . $argv[1] . '/cert.pem'
);
$server->setKey(
__DIR__ . '/../host/' . $argv[1] . '/key.rsa'
);
$server->setHandler(
function (\Yggverse\TitanII\Request $request): \Yggverse\TitanII\Response
{
global $memory;
global $config;
// Init server $response = new \Yggverse\TitanII\Response();
$server = new \Yggverse\TitanII\Server();
$server->setCert( $response->setCode(
__DIR__ . '/../host/' . $host . '/cert.pem' 20
); );
$server->setKey( $response->setMeta(
__DIR__ . '/../host/' . $host . '/key.rsa' 'text/gemini'
); );
$server->setHandler( // Route begin
function (\Yggverse\TitanII\Request $request): \Yggverse\TitanII\Response switch ($request->getPath())
{ {
global $memory; // Home page
global $config; case null:
case '/':
$response = new \Yggverse\TitanII\Response();
// Get rooms list
$response->setCode( include_once __DIR__ . '/controller/room.php';
20
); $room = new \Kevachat\Geminiapp\Controller\Room(
$memory,
$response->setMeta( $config
'text/gemini' );
);
if ($list = $room->list())
// Route begin {
switch ($request->getPath()) $response->setContent(
{ $list
// Home page
case null:
case '/':
// Get rooms list
include_once __DIR__ . '/controller/room.php';
$room = new \Kevachat\Geminiapp\Controller\Room(
$memory,
$config
); );
if ($list = $room->list()) return $response;
{ }
$response->setContent(
$list
);
return $response; // Dynamical requests
} default:
// Dynamical requests
default:
// room|raw request // room|raw request
if (preg_match('/^\/([A-z]+)\/(N[A-z0-9]{33})$/', $request->getPath(), $matches)) if (preg_match('/^\/([A-z]+)\/(N[A-z0-9]{33})$/', $request->getPath(), $matches))
{
if (!empty($matches[1]) && !empty($matches[2]))
{ {
if (!empty($matches[1]) && !empty($matches[2])) switch ($matches[1])
{ {
switch ($matches[1]) case 'room':
{
case 'room':
include_once __DIR__ . '/controller/room.php'; include_once __DIR__ . '/controller/room.php';
$room = new \Kevachat\Geminiapp\Controller\Room( $room = new \Kevachat\Geminiapp\Controller\Room(
$memory, $memory,
$config $config
);
if ($posts = $room->posts($matches[2]))
{
$response->setContent(
$posts
); );
if ($posts = $room->posts($matches[2])) return $response;
{ }
$response->setContent(
$posts
);
return $response; break;
}
break; case 'raw':
case 'raw': include_once __DIR__ . '/controller/media.php';
include_once __DIR__ . '/controller/media.php'; $media = new \Kevachat\Geminiapp\Controller\Media(
$config
);
$media = new \Kevachat\Geminiapp\Controller\Media( if ($data = $media->raw($matches[2], $mime))
$config {
$response->setMeta(
$mime
); );
if ($data = $media->raw($matches[2], $mime)) $response->setContent(
{ $data
$response->setMeta( );
$mime
);
$response->setContent(
$data
);
return $response; return $response;
} }
break; break;
}
} }
} }
}
// New publication request // New publication request
else if (preg_match('/^\/room\/(N[A-z0-9]{33})\/([\d]+)\/post$/', $request->getPath(), $matches)) else if (preg_match('/^\/room\/(N[A-z0-9]{33})\/([\d]+)\/post$/', $request->getPath(), $matches))
{
if (!empty($matches[1]))
{ {
if (!empty($matches[1])) // Request post message
if (empty($request->getQuery()))
{ {
// Request post message $response->setMeta(
if (empty($request->getQuery())) 'text/plain'
{ );
$response->setMeta(
'text/plain'
);
$response->setCode( $response->setCode(
10 10
); );
return $response; return $response;
} }
// Message sent, save to blockchain // Message sent, save to blockchain
else else
{ {
include_once __DIR__ . '/controller/room.php'; include_once __DIR__ . '/controller/room.php';
$room = new \Kevachat\Geminiapp\Controller\Room( $room = new \Kevachat\Geminiapp\Controller\Room(
$memory, $memory,
$config $config
); );
// Success, redirect to this room page // Success, redirect to this room page
if ($txid = $room->post($matches[1], null, $matches[2], $request->getQuery())) if ($txid = $room->post($matches[1], null, $matches[2], $request->getQuery()))
{
if ($result = $room->sent($matches[1], $txid))
{ {
if ($result = $room->sent($matches[1], $txid)) $response->setContent(
{ $result
$response->setContent( );
$result
); return $response;
return $response;
}
} }
} }
} }
} }
}
// New post reply request // New post reply request
else if (preg_match('/^\/room\/(N[A-z0-9]{33})\/([A-z0-9]{64})\/([\d]+)\/reply$/', $request->getPath(), $matches)) else if (preg_match('/^\/room\/(N[A-z0-9]{33})\/([A-z0-9]{64})\/([\d]+)\/reply$/', $request->getPath(), $matches))
{
if (!empty($matches[1]) && !empty($matches[2]) && !empty($matches[3]))
{ {
if (!empty($matches[1]) && !empty($matches[2]) && !empty($matches[3])) // Request post message
if (empty($request->getQuery()))
{ {
// Request post message $response->setMeta(
if (empty($request->getQuery())) 'text/plain'
{ );
$response->setMeta(
'text/plain'
);
$response->setCode( $response->setCode(
10 10
); );
return $response; return $response;
} }
// Message sent, save to blockchain // Message sent, save to blockchain
else else
{ {
include_once __DIR__ . '/controller/room.php'; include_once __DIR__ . '/controller/room.php';
$room = new \Kevachat\Geminiapp\Controller\Room( $room = new \Kevachat\Geminiapp\Controller\Room(
$memory, $memory,
$config $config
); );
// Success, redirect to this room page // Success, redirect to this room page
if ($txid = $room->post($matches[1], $matches[2], $matches[3], $request->getQuery())) if ($txid = $room->post($matches[1], $matches[2], $matches[3], $request->getQuery()))
{
if ($result = $room->sent($matches[1], $txid))
{ {
if ($result = $room->sent($matches[1], $txid)) $response->setContent(
{ $result
$response->setContent( );
$result
); return $response;
return $response;
}
} }
} }
} }
} }
} }
}
// Set default response
include_once __DIR__ . '/controller/error.php';
$error = new \Kevachat\Geminiapp\Controller\Error(
$config
);
$response->setContent( // Set default response
$error->oops() include_once __DIR__ . '/controller/error.php';
);
return $response; $error = new \Kevachat\Geminiapp\Controller\Error(
} $config
); );
// Start server $response->setContent(
echo sprintf( $error->oops()
_('Server "%s" started on %s:%d') . PHP_EOL, );
$host,
$config->gemini->server->host,
$config->gemini->server->port
);
$server->start( return $response;
$config->gemini->server->host, }
$config->gemini->server->port );
);
} // Start server
echo sprintf(
_('Server "%s" started on %s:%d') . PHP_EOL,
$argv[1],
$config->gemini->server->host,
$config->gemini->server->port
);
$server->start(
$config->gemini->server->host,
$config->gemini->server->port
);
Loading…
Cancel
Save