text: import 'dotenv/config';
  
  import express from 'express';
  import path from 'path';
  import helmet from 'helmet';
  import redis from 'redis';
  import RateLimit from 'express-rate-limit';
  import RateLimitRedis from 'rate-limit-redis';
  import Youch from 'youch';
  import routes from './routes';
  
  import './database';
  
  class App {
    constructor() {
      this.server = express();
  
      this.middlewares();
      this.routes();
      this.exceptionHandler();
    }
  
    middlewares() {
      this.server.use(helmet());
      this.server.use(express.json());
      this.server.use(
        '/files',
        express.static(path.resolve(__dirname, '..', 'tmp', 'uploads'))
      );
  
      if (process.env.NODE_ENV !== 'development') {
        this.server.use(
          new RateLimit({
            store: new RateLimitRedis({
              client: redis.createClient({
                host: process.env.REDIS_HOST,
                port: process.env.REDIS_PORT,
              }),
            }),
            // We limit a time interval, in this case 15 minutes.
            windowMS: 1000 * 60 * 15,
            // How many requests in this time
            max: 1000,
          })
        );
      }
    }
  
    routes() {
      this.server.use(routes);
    }
  
    exceptionHandler() {
      this.server.use(async (err, req, res, next) => {
        // We will just detail the errors in development mode
        if (process.env.NODE_ENV === 'development') {
          const errors = await new Youch(err, req).toJSON();
  
          return res.status(500).json(errors);
        }
        return res.status(500).json({ error: 'Internal server error' });
      });
    }
  }
  
  export default new App().server;
  