<commit_before>const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
require('dotenv').config();
const R = require('ramda');
const htmlParser = require('./modules/html-parser.js');
const dbConnection = require('./modules/database-connection.js');

const app = express();

const corsOptions = {
  origin: 'https://quest-stat.netlify.com',
  methods: ['GET', 'POST'],
  optionsSuccessStatus: 200,
  preflightContinue: true,
  credentials: true
};

// app.use(allowCrossDomain);
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.options('*', cors(corsOptions));
app.post('/games/', cors(corsOptions), (req, res) => {
  const gameId = R.path(['body', 'id'], req);
  const domain = R.path(['body', 'domain'], req);

  if (R.isNil(gameId) || R.isNil(domain)) {
    res.status(400).send('Bad Request');
  }

  const gameData = {
    info: null,
    stat: null
  };

  dbConnection.getGameInfoFromDatabase(gameId)
    .then((results) => {
      if (R.isNil(results) || R.isEmpty(results)) {
        return htmlParser.getGameInfo(domain, gameId)
          .then((parsedGameData) => dbConnection.saveGameInfoToDatabase(parsedGameData));
      }
      return R.head(results);
    })
    .then((data) => {
      gameData.info = data;
      return dbConnection.getLevelFromDatabase(gameId);
    })
    .then((levels) => {
      if (R.isNil(levels) || R.isEmpty(levels)) {
        return htmlParser.getGameStat(domain, gameId, gameData.info)
          .then((gameStat) => dbConnection.saveGameDataToDatabase(gameData.info, gameStat));
      }
      return levels;
    })
    .then((stat) => {
      gameData.stat = stat;
      res.send(gameData);
    })
    .catch((error) => {
      res.status(500).send(error);
    });
});
app.listen(process.env.PORT || 4040);
<commit_msg>update CORS settings<commit_after>const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
require('dotenv').config();
const R = require('ramda');
const htmlParser = require('./modules/html-parser.js');
const dbConnection = require('./modules/database-connection.js');

const app = express();
const allowedUrls = [new RegExp('quest-stat'), new RegExp('localhost')];

const corsOptions = {
  origin: (origin, callback) => {
    if (R.any((allowedUrl) => R.test(allowedUrl, origin))(allowedUrls)) {
      callback(null, true);
    } else {
      callback(new Error('Not allowed by CORS'));
    }
  },
  methods: ['GET', 'POST'],
  optionsSuccessStatus: 200,
  preflightContinue: true,
  credentials: true
};

// app.use(allowCrossDomain);
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.options('*', cors(corsOptions));
app.post('/games/', cors(corsOptions), (req, res) => {
  const gameId = R.path(['body', 'id'], req);
  const domain = R.path(['body', 'domain'], req);

  if (R.isNil(gameId) || R.isNil(domain)) {
    res.status(400).send('Bad Request');
  }

  const gameData = {
    info: null,
    stat: null
  };

  dbConnection.getGameInfoFromDatabase(gameId)
    .then((results) => {
      if (R.isNil(results) || R.isEmpty(results)) {
        return htmlParser.getGameInfo(domain, gameId)
          .then((parsedGameData) => dbConnection.saveGameInfoToDatabase(parsedGameData));
      }
      return R.head(results);
    })
    .then((data) => {
      gameData.info = data;
      return dbConnection.getLevelFromDatabase(gameId);
    })
    .then((levels) => {
      if (R.isNil(levels) || R.isEmpty(levels)) {
        return htmlParser.getGameStat(domain, gameId, gameData.info)
          .then((gameStat) => dbConnection.saveGameDataToDatabase(gameData.info, gameStat));
      }
      return levels;
    })
    .then((stat) => {
      gameData.stat = stat;
      res.send(gameData);
    })
    .catch((error) => {
      res.status(500).send(error);
    });
});
app.listen(process.env.PORT || 4040);

{'fraction_non_alphanumeric': 0.17857142857142858, 'fraction_numerical': 0.0064039408866995075, 'mean_word_length': 3.6095346197502836, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1612063', 'n_tokens_mistral': 1329, 'n_tokens_neox': 1298, 'n_words': 245}