From 732cf0231750b8fded533aeb7138b4a030084643 Mon Sep 17 00:00:00 2001 From: hackerESQ Date: Thu, 6 Mar 2025 16:55:54 -0600 Subject: [PATCH] refactor: storage directory scaffoling and save generated app key to file --- config/app.php | 2 +- docker/entrypoint.sh | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/config/app.php b/config/app.php index b213b42..f2a557f 100644 --- a/config/app.php +++ b/config/app.php @@ -100,7 +100,7 @@ return [ 'cipher' => 'AES-256-CBC', - 'key' => env('APP_KEY'), + 'key' => env('APP_KEY') ?: when(file_exists('storage/app/.key'), fn () => trim(file_get_contents('storage/app/.key'))), 'previous_keys' => [ ...array_filter( diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 8b2d2b0..7ca0c12 100644 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -8,7 +8,11 @@ echo "CiAgKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKi echo -e "\n====================== Validating environment... ====================== " # Ensure app storage directory is scaffolded -mkdir -p storage/{{framework/cache,framework/sessions,framework/views},app,logs} +mkdir -p storage/framework/cache \ + storage/framework/sessions \ + storage/framework/views \ + storage/app \ + storage/logs # Ensure storage directory is permissioned for www-data chmod -R 775 storage @@ -16,9 +20,9 @@ chown -R www-data:www-data storage echo -e "\n > Storage directory scaffolding is OK... " -# Ensure app key is generated -if [[ -z "$APP_KEY" ]]; then - echo -e "\n > Oops! The required APP_KEY configuration is missing in your environment! " +# Ensure app key exists / generate if required +KEY_FILE="storage/app/.key" +if [ -z "$APP_KEY" ] && [ ! -s "$KEY_FILE" ]; then draw_box() { local text="$1" @@ -30,7 +34,12 @@ if [[ -z "$APP_KEY" ]]; then echo "$border" } - export APP_KEY=$(php artisan key:generate --show) + export APP_KEY="$(php artisan key:generate --show)" + + echo -e "\n > Oops! The required APP_KEY configuration is missing! Generated app key and saved in $KEY_FILE ." + + echo "$APP_KEY" > "$KEY_FILE" + draw_box $APP_KEY else echo -e "\n > APP_KEY is OK... "