diff --git a/README.md b/README.md index 00b8234..06d57a6 100644 --- a/README.md +++ b/README.md @@ -38,8 +38,8 @@ KevaChat is distributed chat platform for open, uncensored and privacy respectab ## Start -* `php src/server.php` +* `php src/server.php 127.0.0.1` ## See also -* [web implementation](https://github.com/kevachat/webapp) \ No newline at end of file +* [KevaChat Web Application](https://github.com/kevachat/webapp) \ No newline at end of file diff --git a/src/server.php b/src/server.php index 99e6a3d..51b9a2a 100644 --- a/src/server.php +++ b/src/server.php @@ -3,286 +3,282 @@ // Load dependencies require_once __DIR__ . '/../vendor/autoload.php'; -// Scan host -foreach ((array) scandir(__DIR__ . '/../host') as $host) +// Check arguments +if (empty($argv[1])) { - // Skip meta - 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 - ); + exit(_('Configured hostname required as argument!') . PHP_EOL); +} - continue; - } - - // Check cert exists - if (!file_exists(__DIR__ . '/../host/' . $host . '/cert.pem')) - { - echo sprintf( +// Check cert exists +if (!file_exists(__DIR__ . '/../host/' . $argv[1] . '/cert.pem')) +{ + exit( + sprintf( _('Certificate for host "%s" not found!') . PHP_EOL, - $host - ); - - continue; - } + $argv[1] + ) + ); +} - // Check key exists - if (!file_exists(__DIR__ . '/../host/' . $host . '/key.rsa')) - { - echo sprintf( +// Check key exists +if (!file_exists(__DIR__ . '/../host/' . $argv[1] . '/key.rsa')) +{ + exit( + sprintf( _('Key for host "%s" not found!') . PHP_EOL, - $host - ); - - continue; - } - - // Init config - $config = json_decode( - file_get_contents( - __DIR__ . '/../host/' . $host . '/config.json' + $argv[1] ) ); +} - // Init memory - $memory = new \Yggverse\Cache\Memory( - $config->memcached->server->host, - $config->memcached->server->port, - $config->memcached->server->namespace, - $config->memcached->server->timeout +// Check host configured +if (!file_exists(__DIR__ . '/../host/' . $argv[1] . '/config.json')) +{ + exit( + sprintf( + _('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 - $server = new \Yggverse\TitanII\Server(); + $response = new \Yggverse\TitanII\Response(); - $server->setCert( - __DIR__ . '/../host/' . $host . '/cert.pem' - ); + $response->setCode( + 20 + ); - $server->setKey( - __DIR__ . '/../host/' . $host . '/key.rsa' - ); + $response->setMeta( + 'text/gemini' + ); - $server->setHandler( - function (\Yggverse\TitanII\Request $request): \Yggverse\TitanII\Response + // Route begin + switch ($request->getPath()) { - global $memory; - global $config; - - $response = new \Yggverse\TitanII\Response(); - - $response->setCode( - 20 - ); - - $response->setMeta( - 'text/gemini' - ); - - // Route begin - switch ($request->getPath()) - { - // Home page - case null: - case '/': - - // Get rooms list - include_once __DIR__ . '/controller/room.php'; - - $room = new \Kevachat\Geminiapp\Controller\Room( - $memory, - $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()) - { - $response->setContent( - $list - ); + return $response; + } - return $response; - } - - // Dynamical requests - default: + // Dynamical requests + default: - // room|raw request - if (preg_match('/^\/([A-z]+)\/(N[A-z0-9]{33})$/', $request->getPath(), $matches)) + // room|raw request + 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( + $memory, + $config + ); + + if ($posts = $room->posts($matches[2])) + { + $response->setContent( + $posts ); - if ($posts = $room->posts($matches[2])) - { - $response->setContent( - $posts - ); + return $response; + } - 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( - $config + if ($data = $media->raw($matches[2], $mime)) + { + $response->setMeta( + $mime ); - if ($data = $media->raw($matches[2], $mime)) - { - $response->setMeta( - $mime - ); - - $response->setContent( - $data - ); + $response->setContent( + $data + ); - return $response; - } + return $response; + } - break; - } + break; } } + } - // New publication request - else if (preg_match('/^\/room\/(N[A-z0-9]{33})\/([\d]+)\/post$/', $request->getPath(), $matches)) + // New publication request + 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 - if (empty($request->getQuery())) - { - $response->setMeta( - 'text/plain' - ); + $response->setMeta( + 'text/plain' + ); - $response->setCode( - 10 - ); + $response->setCode( + 10 + ); - return $response; - } + return $response; + } - // Message sent, save to blockchain - else - { - include_once __DIR__ . '/controller/room.php'; + // Message sent, save to blockchain + else + { + include_once __DIR__ . '/controller/room.php'; - $room = new \Kevachat\Geminiapp\Controller\Room( - $memory, - $config - ); + $room = new \Kevachat\Geminiapp\Controller\Room( + $memory, + $config + ); - // Success, redirect to this room page - if ($txid = $room->post($matches[1], null, $matches[2], $request->getQuery())) + // Success, redirect to this room page + 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 - ); - - return $response; - } + $response->setContent( + $result + ); + + return $response; } } } } + } - // New post reply request - else if (preg_match('/^\/room\/(N[A-z0-9]{33})\/([A-z0-9]{64})\/([\d]+)\/reply$/', $request->getPath(), $matches)) + // New post reply request + 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 - if (empty($request->getQuery())) - { - $response->setMeta( - 'text/plain' - ); + $response->setMeta( + 'text/plain' + ); - $response->setCode( - 10 - ); + $response->setCode( + 10 + ); - return $response; - } + return $response; + } - // Message sent, save to blockchain - else - { - include_once __DIR__ . '/controller/room.php'; + // Message sent, save to blockchain + else + { + include_once __DIR__ . '/controller/room.php'; - $room = new \Kevachat\Geminiapp\Controller\Room( - $memory, - $config - ); + $room = new \Kevachat\Geminiapp\Controller\Room( + $memory, + $config + ); - // Success, redirect to this room page - if ($txid = $room->post($matches[1], $matches[2], $matches[3], $request->getQuery())) + // Success, redirect to this room page + 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 - ); - - return $response; - } + $response->setContent( + $result + ); + + return $response; } } } } - } - - // Set default response - include_once __DIR__ . '/controller/error.php'; - - $error = new \Kevachat\Geminiapp\Controller\Error( - $config - ); + } + } - $response->setContent( - $error->oops() - ); + // Set default response + include_once __DIR__ . '/controller/error.php'; - return $response; - } - ); + $error = new \Kevachat\Geminiapp\Controller\Error( + $config + ); - // Start server - echo sprintf( - _('Server "%s" started on %s:%d') . PHP_EOL, - $host, - $config->gemini->server->host, - $config->gemini->server->port - ); + $response->setContent( + $error->oops() + ); - $server->start( - $config->gemini->server->host, - $config->gemini->server->port - ); -} \ No newline at end of file + return $response; + } +); + +// 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 +); \ No newline at end of file