build: add access token default value from env

When entering the setting interface, Api and Code will now get default values ​​from env
This commit is contained in:
Prajna 2023-04-06 22:52:59 +08:00
parent bf8d136936
commit 1a53ba2da5
4 changed files with 49 additions and 4 deletions

2
.env.production Normal file
View File

@ -0,0 +1,2 @@
NEXT_PUBLIC_OPENAI_API_KEY=APP_NEXT_PUBLIC_OPENAI_API_KEY
NEXT_PUBLIC_CODE=APP_NEXT_PUBLIC_CODE

View File

@ -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

View File

@ -16,8 +16,8 @@ export const ACCESS_KEY = "access-control";
export const useAccessStore = create<AccessControlStore>()(
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<AccessControlStore>()(
{
name: ACCESS_KEY,
version: 1,
}
)
},
),
);

35
entrypoint.sh Normal file
View File

@ -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 "$@"