server.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import express from 'express';
  2. import cors from 'cors';
  3. import bodyParser from 'body-parser';
  4. import { createServer } from 'http';
  5. import pinoHttp from 'pino-http';
  6. import i18next from 'i18next';
  7. import i18nextMiddleware from 'i18next-http-middleware';
  8. import i18nextBackend from 'i18next-fs-backend';
  9. import path from 'path';
  10. import fs from 'fs';
  11. import log from './log.js';
  12. import { getDirname } from './utils.js';
  13. import middleware from './middleware.js';
  14. import {
  15. PORT,
  16. SERVER_URL,
  17. FILE_STORE_TYPE,
  18. DISK_DESTINATION,
  19. S3_SECRET_KEY,
  20. S3_ACCESS_KEY,
  21. S3_BUCKET,
  22. S3_ENDPOINT,
  23. S3_REGION,
  24. S3_PROXY,
  25. S3_CDN,
  26. STORE_BACKEND,
  27. STORE_PREFIX,
  28. NEDB_BACKEND_DIRNAME,
  29. MONGODB_URI,
  30. MONGODB_DATABASE,
  31. SECRET,
  32. DISABLE_CACHE,
  33. EMAIL_HOST,
  34. EMAIL_PORT,
  35. EMAIL_USER,
  36. EMAIL_PASSWORD,
  37. SETUP_PATH,
  38. S3_SIGNED_URL,
  39. SERVER_NAME,
  40. EMAIL_FROM,
  41. SITE_REGISTRATION_ENABLED,
  42. USE_PINO,
  43. } from './settings.js';
  44. const __dirname = getDirname(import.meta.url);
  45. const startServer = () => {
  46. i18next
  47. .use(i18nextMiddleware.LanguageDetector)
  48. .use(i18nextBackend)
  49. .init({
  50. supportedLngs: ['en', 'fr'],
  51. initImmediate: false,
  52. fallbackLng: 'en',
  53. preload: fs
  54. .readdirSync(path.join(__dirname, '../locales'))
  55. .filter((fileName) => {
  56. const joinedPath = path.join(
  57. path.join(__dirname, '../locales'),
  58. fileName
  59. );
  60. const isDirectory = fs.statSync(joinedPath).isDirectory();
  61. return isDirectory;
  62. }),
  63. backend: {
  64. loadPath: path.join(__dirname, '../locales/{{lng}}/{{ns}}.json'),
  65. },
  66. });
  67. if (!SECRET) {
  68. console.log(
  69. 'You must define "RICOCHET_SECRET" environnement variable (tip: use .env file)'
  70. );
  71. process.exit(-1);
  72. }
  73. const app = express();
  74. const httpServer = createServer(app);
  75. const corsOption = {
  76. credentials: true,
  77. origin: (origin, callback) => {
  78. // Allow ALL origins pls
  79. return callback(null, true);
  80. },
  81. };
  82. app.use(cors(corsOption));
  83. if (USE_PINO) {
  84. app.use(pinoHttp({ logger: log }));
  85. }
  86. app.use(
  87. bodyParser.json({
  88. limit: '50mb',
  89. })
  90. );
  91. app.use(i18nextMiddleware.handle(i18next));
  92. app.use(bodyParser.urlencoded({ extended: true }));
  93. // Static files
  94. const root = path.join(__dirname, '../public');
  95. app.use(express.static(root));
  96. app.use(
  97. middleware({
  98. secret: SECRET,
  99. serverName: SERVER_NAME,
  100. serverUrl: SERVER_URL,
  101. siteRegistrationEnabled: SITE_REGISTRATION_ENABLED,
  102. storeConfig: {
  103. type: STORE_BACKEND,
  104. prefix: STORE_PREFIX,
  105. dirname: NEDB_BACKEND_DIRNAME,
  106. uri: MONGODB_URI,
  107. database: MONGODB_DATABASE,
  108. },
  109. fileStoreConfig: {
  110. type: FILE_STORE_TYPE,
  111. diskDestination: DISK_DESTINATION,
  112. s3AccessKey: S3_ACCESS_KEY,
  113. s3Bucket: S3_BUCKET,
  114. s3Endpoint: S3_ENDPOINT,
  115. s3SecretKey: S3_SECRET_KEY,
  116. s3Region: S3_REGION,
  117. s3Proxy: S3_PROXY,
  118. s3Cdn: S3_CDN,
  119. s3SignedUrl: S3_SIGNED_URL,
  120. },
  121. disableCache: DISABLE_CACHE,
  122. setupPath: SETUP_PATH,
  123. emailConfig: {
  124. host: EMAIL_HOST,
  125. port: EMAIL_PORT,
  126. from: EMAIL_FROM,
  127. auth: {
  128. user: EMAIL_USER,
  129. pass: EMAIL_PASSWORD,
  130. },
  131. },
  132. })
  133. );
  134. httpServer.listen(PORT, () => {
  135. log.info(`Ricochet.js is listening on ${PORT}`);
  136. });
  137. return app;
  138. };
  139. export default startServer;