-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move constants & env to central config (#107)
* Move constants & process.env to central config * Update jest test mocking to match config change
- Loading branch information
1 parent
5e09100
commit a9d49e3
Showing
25 changed files
with
213 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* This file defines all config used anywhere in the api. These values need to be defined on import. | ||
* | ||
* By moving all env variable usage to one place, we also make managing their usage much easier, and | ||
* can error if they are not defined. | ||
*/ | ||
|
||
import env from "./env.js"; | ||
|
||
export enum Device { | ||
ADMIN = "admin", | ||
DEV = "dev", | ||
WEB = "web", | ||
IOS = "ios", | ||
ANDROID = "android", | ||
} | ||
|
||
function requireEnv(name: string): string { | ||
const value = env[name]; | ||
|
||
if (value === undefined) { | ||
throw new Error(`Env variable ${name} is not defined!`); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
const Config = { | ||
/* Jest */ | ||
TEST: false, // False by default, will be mocked over | ||
|
||
/* URLs */ | ||
PORT: env.PORT ? parseInt(env.PORT) : 3000, | ||
|
||
DEFAULT_DEVICE: Device.WEB, | ||
|
||
REDIRECT_URLS: new Map([ | ||
[Device.ADMIN, "https://admin.hackillinois.org/auth/"], | ||
[Device.DEV, "https://adonix.hackillinois.org/auth/dev/"], | ||
[Device.WEB, "https://www.hackillinois.org/auth/"], | ||
[Device.IOS, "hackillinois://login/"], | ||
[Device.ANDROID, "hackillinois://login/"], | ||
]) as Map<string, string>, | ||
|
||
CALLBACK_URLS: { | ||
GITHUB: "https://adonix.hackillinois.org/auth/github/callback/", | ||
// GITHUB: "http://localhost:3000/auth/github/callback/", | ||
GOOGLE: "https://adonix.hackillinois.org/auth/google/callback/", | ||
// GOOGLE: "http://127.0.0.1:3000/auth/google/callback/", | ||
}, | ||
|
||
METADATA_URL: "https://hackillinois.github.io/adonix-metadata/config.json", | ||
|
||
/* OAuth, Keys, & Permissions */ | ||
DB_URL: `mongodb+srv://${requireEnv("DB_USERNAME")}:${requireEnv("DB_PASSWORD")}@${requireEnv("DB_SERVER")}/`, | ||
|
||
GITHUB_OAUTH_ID: requireEnv("GITHUB_OAUTH_ID"), | ||
GITHUB_OAUTH_SECRET: requireEnv("GITHUB_OAUTH_SECRET"), | ||
|
||
GOOGLE_OAUTH_ID: requireEnv("GOOGLE_OAUTH_ID"), | ||
GOOGLE_OAUTH_SECRET: requireEnv("GOOGLE_OAUTH_SECRET"), | ||
|
||
JWT_SECRET: requireEnv("JWT_SECRET"), | ||
|
||
NEWSLETTER_CORS: { | ||
PROD_REGEX: requireEnv("PROD_REGEX"), | ||
DEPLOY_REGEX: requireEnv("DEPLOY_REGEX"), | ||
}, | ||
|
||
SYSTEM_ADMIN_LIST: requireEnv("SYSTEM_ADMINS").split(","), | ||
|
||
/* Timings */ | ||
MILLISECONDS_PER_SECOND: 1000, | ||
DEFAULT_JWT_EXPIRY_TIME: "24h", | ||
QR_EXPIRY_TIME: "20s", | ||
|
||
/* Defaults */ | ||
DEFAULT_POINT_VALUE: 0, | ||
DEFAULT_FOOD_WAVE: 0, | ||
|
||
/* Limits */ | ||
LEADERBOARD_QUERY_LIMIT: 25, | ||
|
||
/* Misc */ | ||
EVENT_ID_LENGTH: 32, | ||
EVENT_BYTES_GEN: 16, | ||
}; | ||
|
||
export default Config; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,27 @@ | ||
import { configDotenv } from "dotenv"; | ||
/* | ||
* This file loads the env variables we will use at runtime. Instead of relying on system envs dynamically, | ||
* we instead parse the .env file, and overwrite any existing variables with system variables. | ||
* Basically, .env file vars can be overwritten by system level env vars. | ||
* | ||
* The .env is also optional so that env vars can be entirely defined with system vars if needed, like for vercel. | ||
*/ | ||
|
||
export const TEST = false; | ||
import dotenv from "dotenv"; | ||
import { existsSync, readFileSync } from "fs"; | ||
import path from "path"; | ||
|
||
configDotenv(); | ||
const envFilePath = path.join(process.cwd(), ".env"); | ||
const rawEnv = existsSync(envFilePath) ? readFileSync(envFilePath) : ""; | ||
const env = dotenv.parse(rawEnv); | ||
|
||
for (const key in process.env) { | ||
const value = process.env[key]; | ||
|
||
if (value === undefined) { | ||
continue; | ||
} | ||
|
||
env[key] = value; | ||
} | ||
|
||
export default env; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.