Skip to content

Commit

Permalink
feat: Add env to prevent duplicate runtime loading
Browse files Browse the repository at this point in the history
  • Loading branch information
nezort11 committed Dec 21, 2024
1 parent a2a6353 commit 5cf0d64
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
45 changes: 45 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,51 @@
# Copypaste this files as .env and fill blanks

# used to ensure this env files is loaded exactly one
ENV_FILE_LOADED=true

PORT=3012

# https://yandex.cloud/ru/docs/serverless-containers/quickstart/container#cli_1
REGISTRY_NAME="nezort11-registry"
CONTAINER_NAME="serverless-curl-impersonate"
SERVICE_ACCOUNT_ID="..."

# CONTAINER_NUMBER=""

# # https://github.com/FOSWLY/vot-cli/blob/main/src/config/config.js
# # EXPIRES AFTER SOME TIME
# YANDEX_TRANSLATE_HMAC_SHA254_SECRET=""

# BOT_TOKEN_PROD=""
# BOT_TOKEN_DEV=""
# BOT_PUBLIC_USERNAME=""
# OWNER_USERNAME=""

# APP_ID=""
# APP_HASH=""
# # npx tglogin
# SESSION_PROD=""
# SESSION_DEV=""

# NOTIFICATION_BOT_TOKEN=""
# NOTIFICATION_USER_ID=""
# STORAGE_CHANNEL_CHAT_ID=""

# SENTRY_DSN=""

# IMAGE_TRANSLATE_URL=""

# LOGGING_CHANNEL_ID=""

# # REGISTRY_NAME=""
# # CONTAINER_NAME=""
# # IMAGE_NAME=""
# # SERVICE_ACCOUNT_ID=""
# # CONTAINER_DOMAIN=""

# # UPLOADER_URL_PROD="http://telegram_upload:3000"
# # UPLOADER_URL_DEV="http://127.0.0.1:3000"

# # SERVER_PASSWORD=""


10 changes: 6 additions & 4 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import dotenv from "dotenv";

export const DEBUG_ENV = process.env.DEBUG_ENV;
console.log("DEBUG_ENV", DEBUG_ENV);
export const NODE_ENV = process.env.NODE_ENV;
console.log("NODE_ENV", NODE_ENV);
export const IS_PRODUCTION = process.env.NODE_ENV !== "development";

// otherwise loaded directly by docker env/env_file
if (DEBUG_ENV) {
// ensure env is loaded once (prevent duplicate loading)
// otherwise loaded directly by docker env/env_file or serverless environment/etc.
if (!process.env.ENV_FILE_LOADED) {
// const dotenv = await import("dotenv/lib/main"); // this produces ERROR cuz of dotenv package.json exports field
// const dotenv = await import("dotenv"); // required ESNext module which breaks a lot
dotenv.config({ path: "./.env" }); // mutates process.env from .env
}

export const DEBUG_ENV = process.env.DEBUG_ENV === "true";
console.log("DEBUG_ENV", DEBUG_ENV);

export const PORT = process.env.PORT;
5 changes: 4 additions & 1 deletion src/types/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ export {};
declare global {
namespace NodeJS {
interface ProcessEnv {
ENV_FILE_LOADED: "true" | undefined;

NODE_ENV: "development" | "production";
DEBUG: "true" | "false";
DEBUG: "*";
DEBUG_ENV: "true" | undefined;
}
}
}

0 comments on commit 5cf0d64

Please sign in to comment.