123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- import express from 'express';
- import cors from 'cors';
- import bodyParser from 'body-parser';
- import { createServer } from 'http';
- import pinoHttp from 'pino-http';
- import i18next from 'i18next';
- import i18nextMiddleware from 'i18next-http-middleware';
- import i18nextBackend from 'i18next-fs-backend';
- import path from 'path';
- import fs from 'fs';
- import log from './log.js';
- import { getDirname } from './utils.js';
- import middleware from './middleware.js';
- import {
- PORT,
- SERVER_URL,
- FILE_STORE_TYPE,
- DISK_DESTINATION,
- S3_SECRET_KEY,
- S3_ACCESS_KEY,
- S3_BUCKET,
- S3_ENDPOINT,
- S3_REGION,
- S3_PROXY,
- S3_CDN,
- STORE_BACKEND,
- STORE_PREFIX,
- NEDB_BACKEND_DIRNAME,
- MONGODB_URI,
- MONGODB_DATABASE,
- SECRET,
- DISABLE_CACHE,
- EMAIL_HOST,
- EMAIL_PORT,
- EMAIL_USER,
- EMAIL_PASSWORD,
- SETUP_PATH,
- S3_SIGNED_URL,
- SERVER_NAME,
- EMAIL_FROM,
- SITE_REGISTRATION_ENABLED,
- USE_PINO,
- } from './settings.js';
- const __dirname = getDirname(import.meta.url);
- const startServer = () => {
- i18next
- .use(i18nextMiddleware.LanguageDetector)
- .use(i18nextBackend)
- .init({
- supportedLngs: ['en', 'fr'],
- initImmediate: false,
- fallbackLng: 'en',
- preload: fs
- .readdirSync(path.join(__dirname, '../locales'))
- .filter((fileName) => {
- const joinedPath = path.join(
- path.join(__dirname, '../locales'),
- fileName
- );
- const isDirectory = fs.statSync(joinedPath).isDirectory();
- return isDirectory;
- }),
- backend: {
- loadPath: path.join(__dirname, '../locales/{{lng}}/{{ns}}.json'),
- },
- });
- if (!SECRET) {
- console.log(
- 'You must define "RICOCHET_SECRET" environnement variable (tip: use .env file)'
- );
- process.exit(-1);
- }
- const app = express();
- const httpServer = createServer(app);
- const corsOption = {
- credentials: true,
- origin: (origin, callback) => {
- // Allow ALL origins pls
- return callback(null, true);
- },
- };
- app.use(cors(corsOption));
- if (USE_PINO) {
- app.use(pinoHttp({ logger: log }));
- }
- app.use(
- bodyParser.json({
- limit: '50mb',
- })
- );
- app.use(i18nextMiddleware.handle(i18next));
- app.use(bodyParser.urlencoded({ extended: true }));
- // Static files
- const root = path.join(__dirname, '../public');
- app.use(express.static(root));
- app.use(
- middleware({
- secret: SECRET,
- serverName: SERVER_NAME,
- serverUrl: SERVER_URL,
- siteRegistrationEnabled: SITE_REGISTRATION_ENABLED,
- storeConfig: {
- type: STORE_BACKEND,
- prefix: STORE_PREFIX,
- dirname: NEDB_BACKEND_DIRNAME,
- uri: MONGODB_URI,
- database: MONGODB_DATABASE,
- },
- fileStoreConfig: {
- type: FILE_STORE_TYPE,
- diskDestination: DISK_DESTINATION,
- s3AccessKey: S3_ACCESS_KEY,
- s3Bucket: S3_BUCKET,
- s3Endpoint: S3_ENDPOINT,
- s3SecretKey: S3_SECRET_KEY,
- s3Region: S3_REGION,
- s3Proxy: S3_PROXY,
- s3Cdn: S3_CDN,
- s3SignedUrl: S3_SIGNED_URL,
- },
- disableCache: DISABLE_CACHE,
- setupPath: SETUP_PATH,
- emailConfig: {
- host: EMAIL_HOST,
- port: EMAIL_PORT,
- from: EMAIL_FROM,
- auth: {
- user: EMAIL_USER,
- pass: EMAIL_PASSWORD,
- },
- },
- })
- );
- httpServer.listen(PORT, () => {
- log.info(`Ricochet.js is listening on ${PORT}`);
- });
- return app;
- };
- export default startServer;
|