Skip to content

Commit

Permalink
refactor: mount config file instead of passing env
Browse files Browse the repository at this point in the history
  • Loading branch information
renbaoshuo committed Aug 10, 2024
1 parent d5e6b4b commit cd327c0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
.git/

uoj_data/

.config.local.php
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
uoj_data/
.php-cs-fixer.cache
.config.local.php
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
28 changes: 28 additions & 0 deletions prepare.sh
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 3 additions & 2 deletions web/app/.default-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -51,6 +51,7 @@
]
],
'switch' => [
// 请在 page-header.php 中修改统计代码后再启用
'web-analytics' => false,
'blog-domain-mode' => 3
],
Expand Down
11 changes: 0 additions & 11 deletions web/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down Expand Up @@ -64,8 +57,6 @@ setWebConf(){
# Set web config file
php -a <<UOJEOF
\$config = include '/var/www/uoj/app/.default-config.php';
\$config['database']['host']='$_database_host_';
\$config['database']['password']='$_database_password_';
\$config['judger']['socket']['port']='$_judger_socket_port_';
file_put_contents('/var/www/uoj/app/.config.php', "<?php\nreturn ".str_replace('\'_httpHost_\'','UOJContext::httpHost()',var_export(\$config, true)).";\n");
UOJEOF
Expand All @@ -85,8 +76,6 @@ initProgress(){
#Set uoj_data path
mkdir -p /var/uoj_data/upload
chown -R www-data:www-data /var/uoj_data
#Replace password placeholders
sed -i -e "s/salt0/$_salt0_/g" -e "s/salt1/$_salt1_/g" -e "s/salt2/$_salt2_/g" -e "s/salt3/$_salt3_/g" -e "s/_judger_socket_password_/$_judger_socket_password_/g" /var/www/uoj/app/.config.php
#Using cli upgrade to latest
php /var/www/uoj/app/cli.php upgrade:latest
#Start services
Expand Down

0 comments on commit cd327c0

Please sign in to comment.