shlink_in_docker.local.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. declare(strict_types=1);
  3. namespace Shlinkio\Shlink;
  4. use Monolog\Handler\StreamHandler;
  5. use Monolog\Logger;
  6. use function explode;
  7. use function file_exists;
  8. use function file_get_contents;
  9. use function file_put_contents;
  10. use function implode;
  11. use function Shlinkio\Shlink\Common\env;
  12. use function sprintf;
  13. use function str_shuffle;
  14. use function substr;
  15. use function sys_get_temp_dir;
  16. $helper = new class {
  17. private const BASE62 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  18. private const DB_DRIVERS_MAP = [
  19. 'mysql' => 'pdo_mysql',
  20. 'postgres' => 'pdo_pgsql',
  21. ];
  22. private const DB_PORTS_MAP = [
  23. 'mysql' => '3306',
  24. 'postgres' => '5432',
  25. ];
  26. /** @var string */
  27. private $charset;
  28. /** @var string */
  29. private $secretKey;
  30. public function __construct()
  31. {
  32. [$this->charset, $this->secretKey] = $this->initShlinkKeys();
  33. }
  34. private function initShlinkKeys(): array
  35. {
  36. $keysFile = sprintf('%s/shlink.keys', sys_get_temp_dir());
  37. if (file_exists($keysFile)) {
  38. return explode(',', file_get_contents($keysFile));
  39. }
  40. $keys = [
  41. env('SHORTCODE_CHARS', $this->generateShortcodeChars()),
  42. env('SECRET_KEY', $this->generateSecretKey()),
  43. ];
  44. file_put_contents($keysFile, implode(',', $keys));
  45. return $keys;
  46. }
  47. private function generateShortcodeChars(): string
  48. {
  49. return str_shuffle(self::BASE62);
  50. }
  51. private function generateSecretKey(): string
  52. {
  53. return substr(str_shuffle(self::BASE62), 0, 32);
  54. }
  55. public function getShortcodeChars(): string
  56. {
  57. return $this->charset;
  58. }
  59. public function getSecretKey(): string
  60. {
  61. return $this->secretKey;
  62. }
  63. public function getDbConfig(): array
  64. {
  65. $driver = env('DB_DRIVER');
  66. if ($driver === null || $driver === 'sqlite') {
  67. return [
  68. 'driver' => 'pdo_sqlite',
  69. 'path' => 'data/database.sqlite',
  70. ];
  71. }
  72. $driverOptions = $driver !== 'mysql' ? [] : [
  73. // PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
  74. 1002 => 'SET NAMES utf8',
  75. ];
  76. return [
  77. 'driver' => self::DB_DRIVERS_MAP[$driver],
  78. 'dbname' => env('DB_NAME', 'shlink'),
  79. 'user' => env('DB_USER'),
  80. 'password' => env('DB_PASSWORD'),
  81. 'host' => env('DB_HOST'),
  82. 'port' => env('DB_PORT', self::DB_PORTS_MAP[$driver]),
  83. 'driverOptions' => $driverOptions,
  84. ];
  85. }
  86. public function getNotFoundConfig(): array
  87. {
  88. $notFoundRedirectTo = env('NOT_FOUND_REDIRECT_TO');
  89. return [
  90. 'enable_redirection' => $notFoundRedirectTo !== null,
  91. 'redirect_to' => $notFoundRedirectTo,
  92. ];
  93. }
  94. };
  95. return [
  96. 'config_cache_enabled' => false,
  97. 'app_options' => [
  98. 'secret_key' => $helper->getSecretKey(),
  99. 'disable_track_param' => env('DISABLE_TRACK_PARAM'),
  100. ],
  101. 'delete_short_urls' => [
  102. 'check_visits_threshold' => true,
  103. 'visits_threshold' => (int) env('DELETE_SHORT_URL_THRESHOLD', 15),
  104. ],
  105. 'translator' => [
  106. 'locale' => env('LOCALE', 'en'),
  107. ],
  108. 'entity_manager' => [
  109. 'connection' => $helper->getDbConfig(),
  110. ],
  111. 'url_shortener' => [
  112. 'domain' => [
  113. 'schema' => env('SHORT_DOMAIN_SCHEMA', 'http'),
  114. 'hostname' => env('SHORT_DOMAIN_HOST', ''),
  115. ],
  116. 'shortcode_chars' => $helper->getShortcodeChars(),
  117. 'validate_url' => (bool) env('VALIDATE_URLS', true),
  118. 'not_found_short_url' => $helper->getNotFoundConfig(),
  119. ],
  120. 'logger' => [
  121. 'handlers' => [
  122. 'shlink_rotating_handler' => [
  123. 'level' => Logger::EMERGENCY, // This basically disables regular file logs
  124. ],
  125. 'shlink_stdout_handler' => [
  126. 'class' => StreamHandler::class,
  127. 'level' => Logger::INFO,
  128. 'stream' => 'php://stdout',
  129. 'formatter' => 'dashed',
  130. ],
  131. ],
  132. 'loggers' => [
  133. 'Shlink' => [
  134. 'handlers' => ['shlink_stdout_handler'],
  135. ],
  136. ],
  137. ],
  138. 'dependencies' => [
  139. 'aliases' => env('REDIS_SERVERS') === null ? [] : [
  140. 'lock_store' => 'redis_lock_store',
  141. ],
  142. ],
  143. 'redis' => [
  144. 'servers' => env('REDIS_SERVERS'),
  145. ],
  146. ];