Browse Source

fix multi server launch

main
ghost 11 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 // Check cert exists
if (!file_exists(__DIR__ . '/../host/' . $host . '/cert.pem')) if (!file_exists(__DIR__ . '/../host/' . $argv[1] . '/cert.pem'))
{ {
echo sprintf( exit(
sprintf(
_('Certificate for host "%s" not found!') . PHP_EOL, _('Certificate for host "%s" not found!') . PHP_EOL,
$host $argv[1]
);
continue;
}
// Check key exists
if (!file_exists(__DIR__ . '/../host/' . $host . '/key.rsa'))
{
echo sprintf(
_('Key for host "%s" not found!') . PHP_EOL,
$host
);
continue;
}
// Init config
$config = json_decode(
file_get_contents(
__DIR__ . '/../host/' . $host . '/config.json'
) )
); );
}
// Init memory // Check key exists
$memory = new \Yggverse\Cache\Memory( if (!file_exists(__DIR__ . '/../host/' . $argv[1] . '/key.rsa'))
$config->memcached->server->host, {
$config->memcached->server->port, exit(
$config->memcached->server->namespace, sprintf(
$config->memcached->server->timeout _('Key for host "%s" not found!') . PHP_EOL,
); $argv[1]
)
// Init server
$server = new \Yggverse\TitanII\Server();
$server->setCert(
__DIR__ . '/../host/' . $host . '/cert.pem'
); );
}
$server->setKey( // Check host configured
__DIR__ . '/../host/' . $host . '/key.rsa' if (!file_exists(__DIR__ . '/../host/' . $argv[1] . '/config.json'))
{
exit(
sprintf(
_('Host "%s" not configured!') . PHP_EOL,
$argv[1]
)
); );
}
$server->setHandler( // Init config
function (\Yggverse\TitanII\Request $request): \Yggverse\TitanII\Response $config = json_decode(
{ file_get_contents(
global $memory; __DIR__ . '/../host/' . $argv[1] . '/config.json'
global $config; )
);
$response = new \Yggverse\TitanII\Response();
// Init memory
$response->setCode( $memory = new \Yggverse\Cache\Memory(
20 $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;
$response->setMeta( $response = new \Yggverse\TitanII\Response();
'text/gemini'
);
// Route begin $response->setCode(
switch ($request->getPath()) 20
{ );
// Home page
case null:
case '/':
// Get rooms list $response->setMeta(
include_once __DIR__ . '/controller/room.php'; 'text/gemini'
);
$room = new \Kevachat\Geminiapp\Controller\Room( // Route begin
$memory, switch ($request->getPath())
$config {
// 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())
{
$response->setContent(
$list
); );
if ($list = $room->list()) return $response;
{ }
$response->setContent(
$list
);
return $response;
}
// Dynamical requests // Dynamical requests
default: 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(
$memory,
$config
);
$room = new \Kevachat\Geminiapp\Controller\Room( if ($posts = $room->posts($matches[2]))
$memory, {
$config $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