Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

106 строки
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. credentials: true
  31. },
  32. globals: {},
  33. mail: {},
  34. auth: {},
  35. oauth: {},
  36. db: {},
  37. s3: {},
  38. jwt: {},
  39. stripe: {},
  40. env: {}
  41. };
  42. if (fs.existsSync('app/config/config.json')) {
  43. mergeDeep(config, fs.readJSONSync('app/config/config.json'))
  44. }
  45. if (fs.existsSync('app/config/user_config.json')) {
  46. mergeDeep(config, fs.readJSONSync('app/config/user_config.json'));
  47. }
  48. // folders are site relative
  49. config.tmpFolder = toSystemPath(config.tmpFolder);
  50. if (config.env) {
  51. for (let key in config.env) {
  52. if (!Object.prototype.hasOwnProperty.call(process.env, key)) {
  53. process.env[key] = config.env[key];
  54. } else if (config.debug) {
  55. debug(`"${key}" is already defined in \`process.env\` and will not be overwritten`);
  56. }
  57. }
  58. }
  59. Parser.parseValue(config, new Scope({
  60. $_ENV: process.env
  61. }));
  62. // we change the cors config a bit, * will become true
  63. // and we split string on comma for multiple origins
  64. if (typeof config.cors?.origin == 'string') {
  65. if (config.cors.origin === '*') {
  66. config.cors.origin = true;
  67. } else if (config.cors.origin.includes(',')) {
  68. config.cors.origin = config.cors.origin.split(/\s*,\s*/);
  69. }
  70. }
  71. if (config.debug) {
  72. require('debug').enable(typeof config.debug == 'string' ? config.debug : 'server-connect:*');
  73. }
  74. if (config.redis) {
  75. const { createClient } = require('redis');
  76. const client = createClient(typeof config.redis == 'object' ? config.redis : {
  77. url: config.redis === true ? 'redis://redis' : config.redis,
  78. socket: { connectTimeout: 60000 },
  79. pingInterval: 10000,
  80. });
  81. client.on('error', err => console.error(err));
  82. client.on('connect', () => debug('Redis Connected'));
  83. client.on('reconnecting', () => debug('Redis Reconnecting'));
  84. client.on('ready', () => debug('Redis Ready'));
  85. client.connect().catch(err => {
  86. // we don't want to crash the server if redis is not available
  87. });
  88. global.redisClient = client;
  89. }
  90. debug(config);
  91. module.exports = config;