From cd327c03a12b4b54b11c29007079e8e009840e68 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 10 Aug 2024 19:19:56 +0800 Subject: [PATCH] refactor: mount config file instead of passing env --- .dockerignore | 2 ++ .gitignore | 1 + docker-compose.yml | 3 +++ prepare.sh | 28 ++++++++++++++++++++++++++++ web/app/.default-config.php | 5 +++-- web/install.sh | 11 ----------- 6 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 prepare.sh diff --git a/.dockerignore b/.dockerignore index 58ecf2c20..35b6354a9 100644 --- a/.dockerignore +++ b/.dockerignore @@ -2,3 +2,5 @@ .git/ uoj_data/ + +.config.local.php diff --git a/.gitignore b/.gitignore index 27e821fc1..b013f6e36 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea uoj_data/ .php-cs-fixer.cache +.config.local.php diff --git a/docker-compose.yml b/docker-compose.yml index 5f85d1a6f..7dbae208b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -52,6 +52,9 @@ services: volumes: - ./uoj_data/web/data:/var/uoj_data - ./uoj_data/web/storage:/opt/uoj/web/app/storage + - type: bind + source: ./.config.local.php + target: /opt/uoj/web/app/.config.php ports: - "80:80" - "3690:3690" diff --git a/prepare.sh b/prepare.sh new file mode 100644 index 000000000..646117307 --- /dev/null +++ b/prepare.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +echo "==> Preparing config file" + +genRandStr(){ + cat /dev/urandom | tr -dc [:alnum:] | head -c $1 +} + +# 复制配置文件 +cp web/app/.default-config.php .config.local.php +echo "==> Copied default config file" + +# 替换配置文件中的 salt +sed -i -e "s/salt0/$(genRandStr 32)/g" \ + -e "s/salt1/$(genRandStr 16)/g" \ + -e "s/salt2/$(genRandStr 16)/g" \ + -e "s/salt3/$(genRandStr 16)/g" \ + .config.local.php +echo "==> Replaced salt in config file" + +# 替换配置文件中的 _httpHost_ +sed -i -e "s/'_httpHost_'/UOJContext::httpHost()/g" .config.local.php +echo "==> Replaced _httpHost_ in config file" + +echo "" +echo "==> Done, config file is '.config.local.php'" +echo "==> Please modify the config file according to your needs" +echo "==> Then run 'docker-compose up -d' to start the service" diff --git a/web/app/.default-config.php b/web/app/.default-config.php index 0981d9d9f..4aa04213f 100644 --- a/web/app/.default-config.php +++ b/web/app/.default-config.php @@ -11,8 +11,8 @@ 'database' => [ 'database' => 'app_uoj233', 'username' => 'root', - 'password' => '_database_password_', - 'host' => '127.0.0.1' + 'password' => 'root', + 'host' => 'uoj-db' ], 'web' => [ 'domain' => null, @@ -51,6 +51,7 @@ ] ], 'switch' => [ + // 请在 page-header.php 中修改统计代码后再启用 'web-analytics' => false, 'blog-domain-mode' => 3 ], diff --git a/web/install.sh b/web/install.sh index 36639347f..1b7705590 100644 --- a/web/install.sh +++ b/web/install.sh @@ -3,14 +3,7 @@ genRandStr(){ cat /dev/urandom | tr -dc [:alnum:] | head -c $1 } #Set some vars -_database_host_="${DATABASE_HOST:-uoj-db}" -_database_password_="${DATABASE_PASSWORD:-root}" _judger_socket_port_="${JUDGER_SOCKET_PORT:-2333}" -_judger_socket_password_="${JUDGER_SOCKET_PASSWORD:-_judger_socket_password_}" -_salt0_="${SALT_0:-salt0}" -_salt1_="${SALT_1:-salt1}" -_salt2_="${SALT_2:-salt2}" -_salt3_="${SALT_3:-salt3}" _uoj_protocol_="${UOJ_PROTOCOL:-http}" getAptPackage(){ @@ -64,8 +57,6 @@ setWebConf(){ # Set web config file php -a <