Add some error and exit handling for the server file.

This commit is contained in:
Dave Smith-Hayes 2024-07-19 21:37:37 -04:00
parent 5c72cdbfd5
commit 1de8da8691
5 changed files with 49 additions and 30 deletions

View File

@ -1,5 +1,6 @@
APP_PORT=8000
DEPLOYMENT_ENV="development"
DATABASE_HOST="localhost"
DATABASE_HOST="db"
DATABASE_USER="slovocast"
DATABASE_PASSWORD="Password01"
DATABASE_SCHEMA="slovocast"

View File

@ -1,17 +1,36 @@
<?php
require_once '../vendor/autoload.php';
require_once __DIR__ . '/../vendor/autoload.php';
use Slovocast\Bootstrap;
use React\Http\HttpServer;
use React\Socket\SocketServer;
use Psr\Http\Message\ServerRequestInterface as Request;
try {
$app = Bootstrap::init();
$http = new HttpServer(fn (Request $request) => $app->handle($request));
$address = "127.0.0.1:8000";
$socket = new SocketServer($address);
$http->listen($socket);
echo "Server running at $address" . PHP_EOL;
$http->on('error', function ($error) use ($socket) {
fprintf(STDERR, $error->getMessage() . "\n");
fprintf(STDERR, "Closing socket connection.\n");
$socket->close();
});
$http->on('exit', function () use ($socket) {
fprintf(STDOUT, "Closing socket connection.\n");
$socket->close();
});
fprintf(STDOUT, "Server running on %s\n", $address);
} catch (\Exception $e) {
fprintf(STDERR, "Error caught bootstrapping the application\n");
fprintf(STDERR, $e->getMessage() . "\n");
return -1;
}

View File

@ -59,12 +59,14 @@ class Bootstrap
{
$config = new Configuration();
$dotenv = \DotenvVault\DotenvVault::createImmutable(APP_ROOT_DIR);
$dotenv->safeLoad();
// set all configuration details
$config->addSchema('site', SiteInformationSchema::getSchema());
$config->addSchema('database', DatabaseConnectionSchema::getSchema());
$config->addSchema('session', SessionSchema::getSchema());
$config->merge([
'site' => [
'name' => "Slovocast",
@ -77,10 +79,11 @@ class Bootstrap
'name' => 'slovocast'
],
'database' => [
'host' => '127.0.0.1',
'database' => 'slovocast',
'username' => 'slovocast',
'password' => 'Password01',
'host' => $_SERVER['DB_HOST'],
'database' => $_SERVER['DB_DATABASE'],
'username' => $_SERVER['DB_USERNAME'],
'password' => $_SERVER['DB_PASSWORD'],
'port' => $_SERVER['DB_PORT'],
]
]);

View File

@ -1,20 +1,16 @@
FROM php:8.3-alpine
# Install PHP extensions
RUN apk add --no-cache zip libzip-dev \
&& docker-php-ext-install pdo_mysql zip
# Install composer
RUN curl -sS https://getcomposer.org/installer | php \
&& mv composer.phar /usr/bin/composer
# Install xdebug
RUN apk add --no-cache $PHPIZE_DEPS
# && pecl install xdebug-2.7.1 \
# && docker-php-ext-enable xdebug \
# && echo 'xdebug.remote_enable=1' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
ARG PHP_VERSION=8.3
FROM chialab/php:${PHP_VERSION}
WORKDIR /app
COPY ./app /app
COPY app .
RUN composer install \
--ignore-platform-reqs \
--no-interaction \
--no-plugins \
--prefer-dist \
--no-progress \
--optimize-autoloader
EXPOSE 8000
CMD [ "php", "server/server.php" ]

View File

@ -15,8 +15,8 @@ services:
app:
build:
dockerfile: ./deploy/php/Dockerfile
volumes:
- ./app:/app
ports:
- "8000:8000"
volumes:
slovocast_db: