From 1a53ba2da5c6ee9d3d42c64b131fb109d1fba6e0 Mon Sep 17 00:00:00 2001 From: Prajna <44605072+hqwuzhaoyi@users.noreply.github.com> Date: Thu, 6 Apr 2023 22:52:59 +0800 Subject: [PATCH] build: add access token default value from env MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When entering the setting interface, Api and Code will now get default values ​​from env --- .env.production | 2 ++ Dockerfile | 8 ++++++++ app/store/access.ts | 8 ++++---- entrypoint.sh | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 .env.production create mode 100644 entrypoint.sh diff --git a/.env.production b/.env.production new file mode 100644 index 000000000..7cc266257 --- /dev/null +++ b/.env.production @@ -0,0 +1,2 @@ +NEXT_PUBLIC_OPENAI_API_KEY=APP_NEXT_PUBLIC_OPENAI_API_KEY +NEXT_PUBLIC_CODE=APP_NEXT_PUBLIC_CODE diff --git a/Dockerfile b/Dockerfile index 6f7547b21..016fbe51d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -34,6 +34,14 @@ COPY --from=builder /app/public ./public COPY --from=builder /app/.next/standalone ./ COPY --from=builder /app/.next/static ./.next/static COPY --from=builder /app/.next/server ./.next/server +COPY --from=builder /app/next.config.js ./ + +COPY entrypoint.sh . +COPY .env.production . +# Execute script +RUN apk add --no-cache --upgrade bash +RUN ["chmod", "+x", "./entrypoint.sh"] +ENTRYPOINT ["./entrypoint.sh"] EXPOSE 3000 diff --git a/app/store/access.ts b/app/store/access.ts index 9c61dfa01..716d980c1 100644 --- a/app/store/access.ts +++ b/app/store/access.ts @@ -16,8 +16,8 @@ export const ACCESS_KEY = "access-control"; export const useAccessStore = create()( persist( (set, get) => ({ - token: "", - accessCode: "", + token: process.env.NEXT_PUBLIC_OPENAI_API_KEY ?? "", + accessCode: process.env.NEXT_PUBLIC_CODE ?? "", enabledAccessControl() { return queryMeta("access") === "enabled"; }, @@ -31,6 +31,6 @@ export const useAccessStore = create()( { name: ACCESS_KEY, version: 1, - } - ) + }, + ), ); diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 000000000..49db4eca7 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,35 @@ +#!/bin/bash +# no verbose +set +x +# config +envFilename='.env.production' +nextFolder='./.next/' +function apply_path { + # read all config file + while read line; do + # no comment or not empty + if [ "${line:0:1}" == "#" ] || [ "${line}" == "" ]; then + continue + fi + + # split + configName="$(cut -d'=' -f1 <<<"$line")" + configValue="$(cut -d'=' -f2 <<<"$line")" + prefix="NEXT_PUBLIC_" + if [[ $configName == $prefix* ]]; then + configName="${configName#$prefix}" + fi + # get system env + envValue=$(env | grep "^$configName=" | grep -oe '[^=]*$') + echo "Config: ${configName}=${configValue} Env: ${envValue}" + # if config found + if [ -n "$configValue" ] && [ -n "$envValue" ]; then + # replace all + echo "Replace: ${configValue} with: ${envValue}" + find $nextFolder \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#$configValue#$envValue#g" + fi + done <$envFilename +} +apply_path +echo "Starting Nextjs" +exec "$@"