diff --git a/app/.env.sample b/app/.env.sample index 3e1971f..befd066 100644 --- a/app/.env.sample +++ b/app/.env.sample @@ -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" diff --git a/app/server/server.php b/app/server/server.php index 5d909cf..b24ed1a 100644 --- a/app/server/server.php +++ b/app/server/server.php @@ -1,17 +1,36 @@ $app->handle($request)); + $address = "127.0.0.1:8000"; + $socket = new SocketServer($address); + $http->listen($socket); + + $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; +} + -$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; diff --git a/app/src/Bootstrap.php b/app/src/Bootstrap.php index 6ed4df5..2214b77 100644 --- a/app/src/Bootstrap.php +++ b/app/src/Bootstrap.php @@ -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'], ] ]); diff --git a/deploy/php/Dockerfile b/deploy/php/Dockerfile index b704a5b..ab51ed8 100644 --- a/deploy/php/Dockerfile +++ b/deploy/php/Dockerfile @@ -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" ] diff --git a/docker-compose.yml b/docker-compose.yml index c2d9d32..addea43 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,8 +15,8 @@ services: app: build: dockerfile: ./deploy/php/Dockerfile - volumes: - - ./app:/app + ports: + - "8000:8000" volumes: slovocast_db: