/* eslint-disable no-console,global-require,import/no-extraneous-dependencies */
const express = require('express');
const path = require('path');

const PORT = process.env.PORT || 3001;
const DIST_DIR = path.resolve(__dirname, 'public');

const app = express();

app.use(express.static(DIST_DIR));

const server = app.listen(PORT, err => {
  if (err) {
    console.log(err);
  }

  console.log(`Listening at http://localhost:${server.address().port} 👂`);
});

process.on('SIGTERM', () => {
  console.log('Shutting down server');
  if (server) {
    server.close(err => {
      if (err) {
        console.log('Failed to shut down server');
        process.exit(1);
      }

      console.log('Shut down server');
      process.exit(0);
    });
  } else {
    process.exit(0);
  }
});

module.exports = server;
