index.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import crypto from 'crypto'
  2. import dotenv from 'dotenv'
  3. import main from './main'
  4. dotenv.config()
  5. /**
  6. * Some configuration may be dynamically generated at startup.
  7. * This private object allows it to be preserved during the application's runtime.
  8. */
  9. const dynamicConfig: Record<string, unknown> = {}
  10. /** String truthy values. */
  11. const TRUE = ['1', 't', 'y', 'on', 'yes', 'true']
  12. // Run the app
  13. main({
  14. api: {
  15. prefix: process.env.API_PREFIX || '/api',
  16. },
  17. auth: {
  18. jwt: {
  19. expiresIn: parseInt(process.env.AUTH_JWT_EXPIRES_IN || '86400'),
  20. get secret() {
  21. if (process.env.AUTH_JWT_SECRET) return process.env.AUTH_JWT_SECRET
  22. if (!dynamicConfig.authJwtSecret) {
  23. const secret = crypto.randomBytes(64).toString('hex')
  24. console.warn('AUTH_JWT_SECRET not set. Generated a JWT secret for this run only:', secret)
  25. dynamicConfig.authJwtSecret = secret
  26. }
  27. return dynamicConfig.authJwtSecret as string
  28. },
  29. },
  30. },
  31. http: {
  32. host: process.env.HTTP_HOST || '',
  33. port: parseInt(process.env.HTTP_PORT || '5001'),
  34. },
  35. log: {
  36. level: process.env.LOG_LEVEL || 'info',
  37. },
  38. mongo: {
  39. db: process.env.MONGO_DB || 'herda',
  40. uri: process.env.MONGO_URI || 'mongodb://root:root@localhost:27017',
  41. useTransactions: TRUE.includes(process.env.MONGO_USE_TRANSACTIONS || 'false'),
  42. },
  43. shutdownTimeout: parseInt(process.env.SHUTDOWN_TIMEOUT || '60000'),
  44. }).catch(err => {
  45. if (err) console.error(err)
  46. process.exit(1)
  47. })