diff --git a/Dockerfile b/Dockerfile index 4251b3e4..3a1d5bcc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -61,7 +61,6 @@ EXPOSE 8080 # Copy config specific for the image COPY docker/docker-entrypoint.sh docker-entrypoint.sh -COPY docker/config/shlink_in_docker.local.php config/autoload/shlink_in_docker.local.php COPY docker/config/php.ini ${PHP_INI_DIR}/conf.d/ USER ${USER_ID} diff --git a/config/autoload/.gitignore b/config/autoload/.gitignore deleted file mode 100644 index 1a83fda6..00000000 --- a/config/autoload/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -local.php -*.local.php diff --git a/config/autoload/app_options.global.php b/config/autoload/app_options.global.php index 0b7ec937..9590e544 100644 --- a/config/autoload/app_options.global.php +++ b/config/autoload/app_options.global.php @@ -2,11 +2,13 @@ declare(strict_types=1); +use Shlinkio\Shlink\Core\Config\EnvVars; + return [ 'app_options' => [ 'name' => 'Shlink', - 'version' => '%SHLINK_VERSION%', + 'version' => EnvVars::isDevEnv() ? 'latest' : '%SHLINK_VERSION%', ], ]; diff --git a/config/autoload/app_options.local.php.dist b/config/autoload/app_options.local.php.dist deleted file mode 100644 index 14633a61..00000000 --- a/config/autoload/app_options.local.php.dist +++ /dev/null @@ -1,11 +0,0 @@ - [ - 'version' => 'latest', - ], - -]; diff --git a/config/autoload/common.global.php b/config/autoload/common.global.php index c7db57f1..0ae55ce3 100644 --- a/config/autoload/common.global.php +++ b/config/autoload/common.global.php @@ -3,13 +3,18 @@ declare(strict_types=1); use Laminas\ConfigAggregator\ConfigAggregator; +use Shlinkio\Shlink\Core\Config\EnvVars; -return [ +return (function () { + $isDev = EnvVars::isDevEnv(); - 'debug' => false, + return [ - // Disabling config cache for cli, ensures it's never used for RoadRunner, and also that console - // commands don't generate a cache file that's then used by php-fpm web executions - ConfigAggregator::ENABLE_CACHE => PHP_SAPI !== 'cli', + 'debug' => $isDev, -]; + // Disabling config cache for cli, ensures it's never used for RoadRunner, and also that console + // commands don't generate a cache file that's then used by php-fpm web executions + ConfigAggregator::ENABLE_CACHE => ! $isDev && PHP_SAPI !== 'cli', + + ]; +})(); diff --git a/config/autoload/common.local.php.dist b/config/autoload/common.local.php.dist deleted file mode 100644 index f29c74b0..00000000 --- a/config/autoload/common.local.php.dist +++ /dev/null @@ -1,12 +0,0 @@ - true, - ConfigAggregator::ENABLE_CACHE => false, - -]; diff --git a/config/autoload/dependencies.global.php b/config/autoload/dependencies.global.php index 469171ca..0a99d323 100644 --- a/config/autoload/dependencies.global.php +++ b/config/autoload/dependencies.global.php @@ -11,6 +11,7 @@ use Psr\Http\Client\ClientInterface; use Psr\Http\Message\ServerRequestFactoryInterface; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\UploadedFileFactoryInterface; +use Shlinkio\Shlink\Core\Config\EnvVars; use Spiral\RoadRunner\Http\PSR7Worker; use Spiral\RoadRunner\WorkerInterface; use Symfony\Component\Filesystem\Filesystem; @@ -36,7 +37,7 @@ return [ 'lazy_services' => [ 'proxies_target_dir' => 'data/proxies', 'proxies_namespace' => 'ShlinkProxy', - 'write_proxy_files' => true, + 'write_proxy_files' => EnvVars::isProdEnv(), ], ], diff --git a/config/autoload/dependencies.local.php.dist b/config/autoload/dependencies.local.php.dist deleted file mode 100644 index d9569029..00000000 --- a/config/autoload/dependencies.local.php.dist +++ /dev/null @@ -1,24 +0,0 @@ - [ - 'lazy_services' => [ - 'write_proxy_files' => false, - ], - - 'initializers' => [ - function (ContainerInterface $container, $instance): void { - if ($instance instanceof Log\LoggerAwareInterface) { - $instance->setLogger($container->get(Log\LoggerInterface::class)); - } - }, - ], - ], - -]; diff --git a/config/autoload/logger.global.php b/config/autoload/logger.global.php index c7d7d757..56ba42bb 100644 --- a/config/autoload/logger.global.php +++ b/config/autoload/logger.global.php @@ -14,23 +14,33 @@ use Shlinkio\Shlink\Common\Logger\LoggerFactory; use Shlinkio\Shlink\Common\Logger\LoggerType; use Shlinkio\Shlink\Common\Middleware\AccessLogMiddleware; use Shlinkio\Shlink\Common\Middleware\RequestIdMiddleware; +use Shlinkio\Shlink\Core\Config\EnvVars; use Shlinkio\Shlink\Core\EventDispatcher\Helper\RequestIdProvider; use Shlinkio\Shlink\EventDispatcher\Util\RequestIdProviderInterface; +use function Shlinkio\Shlink\Config\env; use function Shlinkio\Shlink\Config\runningInRoadRunner; return (static function (): array { + $isDev = EnvVars::isDevEnv(); $common = [ - 'level' => Level::Info->value, + 'level' => $isDev ? Level::Debug->value : Level::Info->value, 'processors' => [RequestIdMiddleware::class], 'line_format' => '[%datetime%] [%extra.' . RequestIdMiddleware::ATTRIBUTE . '%] %channel%.%level_name% - %message%', ]; + // In dev env or the docker container, stream Shlink logs to stderr, otherwise send them to a file + $useStreamForShlinkLogger = $isDev || env('SHLINK_RUNTIME') !== null; + return [ 'logger' => [ - 'Shlink' => [ + 'Shlink' => $useStreamForShlinkLogger ? [ + 'type' => LoggerType::STREAM->value, + 'destination' => 'php://stderr', + ...$common, + ] : [ 'type' => LoggerType::FILE->value, ...$common, ], diff --git a/config/autoload/logger.local.php.dist b/config/autoload/logger.local.php.dist deleted file mode 100644 index fe2c8c54..00000000 --- a/config/autoload/logger.local.php.dist +++ /dev/null @@ -1,18 +0,0 @@ - [ - 'Shlink' => [ - 'type' => LoggerType::STREAM->value, - 'destination' => 'php://stderr', - 'level' => Level::Debug->value, - ], - ], - -]; diff --git a/config/autoload/router.global.php b/config/autoload/router.global.php index db389f3a..0464ca83 100644 --- a/config/autoload/router.global.php +++ b/config/autoload/router.global.php @@ -13,7 +13,7 @@ return [ 'fastroute' => [ // Disabling config cache for cli, ensures it's never used for RoadRunner, and also that console // commands don't generate a cache file that's then used by php-fpm web executions - FastRouteRouter::CONFIG_CACHE_ENABLED => PHP_SAPI !== 'cli', + FastRouteRouter::CONFIG_CACHE_ENABLED => EnvVars::isProdEnv() && PHP_SAPI !== 'cli', FastRouteRouter::CONFIG_CACHE_FILE => 'data/cache/fastroute_cached_routes.php', ], ], diff --git a/config/autoload/router.local.php.dist b/config/autoload/router.local.php.dist deleted file mode 100644 index dc67b587..00000000 --- a/config/autoload/router.local.php.dist +++ /dev/null @@ -1,15 +0,0 @@ - [ - 'fastroute' => [ - FastRouteRouter::CONFIG_CACHE_ENABLED => false, - ], - ], - -]; diff --git a/config/config.php b/config/config.php index 40f88ea3..92c69ba0 100644 --- a/config/config.php +++ b/config/config.php @@ -8,10 +8,7 @@ use Laminas\ConfigAggregator; use Laminas\Diactoros; use Mezzio; use Mezzio\ProblemDetails; - -use function Shlinkio\Shlink\Config\env; - -$isTestEnv = env('APP_ENV') === 'test'; +use Shlinkio\Shlink\Core\Config\EnvVars; return (new ConfigAggregator\ConfigAggregator( providers: [ @@ -29,10 +26,10 @@ return (new ConfigAggregator\ConfigAggregator( CLI\ConfigProvider::class, Rest\ConfigProvider::class, new ConfigAggregator\PhpFileProvider('config/autoload/{,*.}global.php'), - // Local config should not be loaded during tests, whereas test config should be loaded ONLY during tests - new ConfigAggregator\PhpFileProvider( - $isTestEnv ? 'config/test/*.global.php' : 'config/autoload/{,*.}local.php', - ), + // Test config should be loaded ONLY during tests + EnvVars::isTestEnv() + ? new ConfigAggregator\PhpFileProvider('config/test/*.global.php') + : new ConfigAggregator\ArrayProvider([]), // Routes have to be loaded last new ConfigAggregator\PhpFileProvider('config/autoload/routes.config.php'), ], diff --git a/docker/config/shlink_in_docker.local.php b/docker/config/shlink_in_docker.local.php deleted file mode 100644 index 2d5d6a06..00000000 --- a/docker/config/shlink_in_docker.local.php +++ /dev/null @@ -1,18 +0,0 @@ - [ - 'Shlink' => [ - 'type' => LoggerType::STREAM->value, - 'destination' => 'php://stderr', - ], - ], - -]; diff --git a/module/Core/src/Config/EnvVars.php b/module/Core/src/Config/EnvVars.php index 2fe5dfd9..6cdf6297 100644 --- a/module/Core/src/Config/EnvVars.php +++ b/module/Core/src/Config/EnvVars.php @@ -27,6 +27,7 @@ use const Shlinkio\Shlink\DEFAULT_SHORT_CODES_LENGTH; enum EnvVars: string { + case APP_ENV = 'APP_ENV'; case DELETE_SHORT_URL_THRESHOLD = 'DELETE_SHORT_URL_THRESHOLD'; case DB_DRIVER = 'DB_DRIVER'; case DB_NAME = 'DB_NAME'; @@ -117,6 +118,7 @@ enum EnvVars: string private function defaultValue(): string|int|bool|null { return match ($this) { + self::APP_ENV => 'prod', self::MEMORY_LIMIT => '512M', self::TIMEZONE => date_default_timezone_get(), @@ -174,4 +176,19 @@ enum EnvVars: string { return $this->loadFromEnv() !== null; } + + public static function isProdEnv(): bool + { + return self::APP_ENV->loadFromEnv() === 'prod'; + } + + public static function isDevEnv(): bool + { + return self::APP_ENV->loadFromEnv() === 'dev'; + } + + public static function isTestEnv(): bool + { + return self::APP_ENV->loadFromEnv() === 'test'; + } } diff --git a/shlink-dev.env.dist b/shlink-dev.env.dist index 1bd58636..ee6173f2 100644 --- a/shlink-dev.env.dist +++ b/shlink-dev.env.dist @@ -1,5 +1,5 @@ LC_ALL=C -#APP_ENV=dev +APP_ENV=dev #GEOLITE_LICENSE_KEY= # URL shortener