server: throw if secretKey not set in production, set default in development (#59)

This commit is contained in:
Joaquin "Florius" Azcarate 2022-04-03 21:50:04 +02:00 committed by GitHub
parent ef005ef0b2
commit 76a2b50c6b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -29,6 +29,13 @@ const config = (): Config => {
return str return str
} }
const defaultIfUndefined = (str: string | undefined, defaultValue: string): string => {
if (str === undefined) {
return defaultValue
}
return str
}
const validNodeEnvs = (str: string | undefined) => { const validNodeEnvs = (str: string | undefined) => {
const valid = ["development", "production", "test"] const valid = ["development", "production", "test"]
if (str && !valid.includes(str)) { if (str && !valid.includes(str)) {
@ -40,16 +47,23 @@ const config = (): Config => {
} }
} }
const is_production = process.env.NODE_ENV === "production";
const developmentDefault = (str: string | undefined, name: string, defaultValue: string): string => {
if (is_production) return throwIfUndefined(str, name);
return defaultIfUndefined(str, defaultValue);
}
validNodeEnvs(process.env.NODE_ENV) validNodeEnvs(process.env.NODE_ENV)
const config: Config = { const config: Config = {
port: process.env.PORT ? parseInt(process.env.PORT) : 3000, port: process.env.PORT ? parseInt(process.env.PORT) : 3000,
jwt_secret: process.env.JWT_SECRET || "myjwtsecret", jwt_secret: process.env.JWT_SECRET || "myjwtsecret",
drift_home: process.env.DRIFT_HOME || "~/.drift", drift_home: process.env.DRIFT_HOME || "~/.drift",
is_production: process.env.NODE_ENV === "production", is_production,
memory_db: stringToBoolean(process.env.MEMORY_DB), memory_db: stringToBoolean(process.env.MEMORY_DB),
enable_admin: stringToBoolean(process.env.ENABLE_ADMIN), enable_admin: stringToBoolean(process.env.ENABLE_ADMIN),
secret_key: throwIfUndefined(process.env.SECRET_KEY, "SECRET_KEY"), secret_key: developmentDefault(process.env.SECRET_KEY, "SECRET_KEY", "secret"),
registration_password: process.env.REGISTRATION_PASSWORD || "" registration_password: process.env.REGISTRATION_PASSWORD || ""
} }
return config return config