Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

107 lignes
3.0 KiB

  1. const package = require('../../package.json');
  2. const fs = require('fs-extra');
  3. const debug = require('debug')('server-connect:setup:config');
  4. const { toSystemPath } = require('../core/path');
  5. const { mergeDeep } = require('../core/util');
  6. const Parser = require('../core/parser');
  7. const Scope = require('../core/scope');
  8. const config = {
  9. port: process.env.PORT || 3000,
  10. debug: false,
  11. secret: 'Need to be set',
  12. tmpFolder: '/tmp',
  13. abortOnDisconnect: false,
  14. createApiRoutes: true,
  15. compression: true,
  16. redis: false,
  17. cron: true,
  18. static: {
  19. index: false
  20. },
  21. session: {
  22. name: package.name + '.sid',
  23. resave: false,
  24. saveUninitialized: false,
  25. store: { $type: 'memory', ttl: 86400000 }
  26. },
  27. cors: { // see https://github.com/expressjs/cors
  28. origin: false,
  29. methods: 'GET,POST',
  30. allowedHeaders: '*',
  31. credentials: true
  32. },
  33. globals: {},
  34. mail: {},
  35. auth: {},
  36. oauth: {},
  37. db: {},
  38. s3: {},
  39. jwt: {},
  40. stripe: {},
  41. env: {}
  42. };
  43. if (fs.existsSync('app/config/config.json')) {
  44. mergeDeep(config, fs.readJSONSync('app/config/config.json'))
  45. }
  46. if (fs.existsSync('app/config/user_config.json')) {
  47. mergeDeep(config, fs.readJSONSync('app/config/user_config.json'));
  48. }
  49. // folders are site relative
  50. config.tmpFolder = toSystemPath(config.tmpFolder);
  51. if (config.env) {
  52. for (let key in config.env) {
  53. if (!Object.prototype.hasOwnProperty.call(process.env, key)) {
  54. process.env[key] = config.env[key];
  55. } else if (config.debug) {
  56. debug(`"${key}" is already defined in \`process.env\` and will not be overwritten`);
  57. }
  58. }
  59. }
  60. Parser.parseValue(config, new Scope({
  61. $_ENV: process.env
  62. }));
  63. // we change the cors config a bit, * will become true
  64. // and we split string on comma for multiple origins
  65. if (typeof config.cors?.origin == 'string') {
  66. if (config.cors.origin === '*') {
  67. config.cors.origin = true;
  68. } else if (config.cors.origin.includes(',')) {
  69. config.cors.origin = config.cors.origin.split(/\s*,\s*/);
  70. }
  71. }
  72. if (config.debug) {
  73. require('debug').enable(typeof config.debug == 'string' ? config.debug : 'server-connect:*');
  74. }
  75. if (config.redis) {
  76. const { createClient } = require('redis');
  77. const client = createClient(typeof config.redis == 'object' ? config.redis : {
  78. url: config.redis === true ? 'redis://redis' : config.redis,
  79. socket: { connectTimeout: 60000 },
  80. pingInterval: 10000,
  81. });
  82. client.on('error', err => console.error(err));
  83. client.on('connect', () => debug('Redis Connected'));
  84. client.on('reconnecting', () => debug('Redis Reconnecting'));
  85. client.on('ready', () => debug('Redis Ready'));
  86. client.connect().catch(err => {
  87. // we don't want to crash the server if redis is not available
  88. });
  89. global.redisClient = client;
  90. }
  91. debug(config);
  92. module.exports = config;