config.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import dotenv from 'dotenv'
  2. const isDev = process.env.NODE_ENV === 'development'
  3. export const ADMIN_ROLE = 'ADMIN'
  4. type LoadEnvVarOpts = {
  5. defaultValue?: string
  6. devDefaultValue?: string
  7. }
  8. const loadEnvVar = (name: string, { defaultValue, devDefaultValue }: LoadEnvVarOpts = {}): string => {
  9. const value = process.env[name]
  10. if (value) {
  11. return value
  12. }
  13. if (isDev && devDefaultValue) {
  14. return devDefaultValue
  15. }
  16. if (defaultValue) {
  17. return defaultValue
  18. }
  19. throw new Error(`Required env variable "${name}" is missing from the environment`)
  20. }
  21. export class Config {
  22. private _port: number
  23. private _mongoDBUri: string
  24. private _featuredContentSecret: string
  25. private _adminSecret: string
  26. private _queryNodeUrl: string
  27. private _isDebugging: boolean
  28. get port(): number {
  29. return this._port
  30. }
  31. get mongoDBUri(): string {
  32. return this._mongoDBUri
  33. }
  34. get featuredContentSecret(): string {
  35. return this._featuredContentSecret
  36. }
  37. get adminSecret(): string {
  38. return this._adminSecret
  39. }
  40. get queryNodeUrl(): string {
  41. return this._queryNodeUrl
  42. }
  43. get isDebugging(): boolean {
  44. return this._isDebugging
  45. }
  46. loadConfig() {
  47. dotenv.config()
  48. const rawPort = loadEnvVar('ORION_PORT', { defaultValue: '6116' })
  49. this._port = parseInt(rawPort)
  50. const mongoHostname = loadEnvVar('ORION_MONGO_HOSTNAME', { devDefaultValue: 'localhost' })
  51. const rawMongoPort = loadEnvVar('ORION_MONGO_PORT', { defaultValue: '27017' })
  52. const mongoDatabase = loadEnvVar('ORION_MONGO_DATABASE', { defaultValue: 'orion' })
  53. this._mongoDBUri = `mongodb://${mongoHostname}:${rawMongoPort}/${mongoDatabase}`
  54. this._featuredContentSecret = loadEnvVar('ORION_FEATURED_CONTENT_SECRET')
  55. this._adminSecret = loadEnvVar('ORION_ADMIN_SECRET')
  56. this._queryNodeUrl = loadEnvVar('ORION_QUERY_NODE_URL')
  57. this._isDebugging = loadEnvVar('ORION_DEBUGGING', { defaultValue: 'false' }) === 'true'
  58. }
  59. }
  60. const config = new Config()
  61. export default config