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
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: 4AEE18F83AFDEB23

View file

@ -29,6 +29,13 @@ const config = (): Config => {
return str
}
const defaultIfUndefined = (str: string | undefined, defaultValue: string): string => {
if (str === undefined) {
return defaultValue
}
return str
}
const validNodeEnvs = (str: string | undefined) => {
const valid = ["development", "production", "test"]
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)
const config: Config = {
port: process.env.PORT ? parseInt(process.env.PORT) : 3000,
jwt_secret: process.env.JWT_SECRET || "myjwtsecret",
drift_home: process.env.DRIFT_HOME || "~/.drift",
is_production: process.env.NODE_ENV === "production",
is_production,
memory_db: stringToBoolean(process.env.MEMORY_DB),
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 || ""
}
return config