{"size": 3618, "ext": "js", "max_stars_repo_path": "server.js", "max_stars_repo_name": "vertiond/vtc-scanner", "max_stars_repo_stars_event_min_datetime": "2018-05-02T14:53:40.000Z", "max_stars_repo_stars_event_max_datetime": "2021-02-04T04:34:18.000Z", "max_issues_repo_path": "server.js", "max_issues_repo_name": "vertiond/vtc-scanner", "max_issues_repo_issues_event_min_datetime": "2017-12-01T03:05:05.000Z", "max_issues_repo_issues_event_max_datetime": "2018-08-31T08:35:03.000Z", "max_forks_repo_path": "server.js", "max_forks_repo_name": "vertiond/vtc-scanner", "max_forks_repo_forks_event_min_datetime": "2018-04-11T22:02:59.000Z", "max_forks_repo_forks_event_max_datetime": "2021-02-11T23:32:30.000Z"}
{"max_stars_count": 5, "max_issues_count": 3, "max_forks_count": 5, "avg_line_length": 29.6557377049, "max_line_length": 95, "alphanum_fraction": 0.5392482034}
const _ = require('lodash');
const path = require('path');
const dns = require('dns');

const config = require(path.join(__dirname, '/config/config'));

const mongoose = require('mongoose');

const models = require(path.join(__dirname, '/app/models/'));
const routes = require(path.join(__dirname, '/app/routes/'));
const crons = require(path.join(__dirname, '/app/crons/'));

const restify = require('restify');
const server = restify.createServer();
const restifyPlugins = restify.plugins;

const logger = require('./app/lib/logger');

server.use(restifyPlugins.bodyParser());
server.use(restifyPlugins.queryParser());
server.use(restifyPlugins.gzipResponse());
server.pre(restify.pre.sanitizePath());

server.use(
  function crossOrigin(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'X-Requested-With');
    return next();
  }
);

// per request logging

server.pre((req, res, next) => {
  const reqIp = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
  logger.debug(`${reqIp} - ${req.method} - ${req.getPath()}`);
  return next();
});


models();
const Pool = mongoose.model('Pool');
const Node = mongoose.model('Node');
routes(server);

mongoose.connect('mongodb://' + config.db.host + '/' + config.db.name, {useMongoClient: true})
  .then(async () => {

    if((await Node.count()) === 0) {
      logger.info('Bootstrapping node search with seeds');
      await Promise.all(_.map(config.vertcoin.seeds, async (it) => {
        let ip = _.head(await (new Promise((resolve, reject) => dns.resolve(it, (err, res) => {
          if (err)
            resolve([]);
          else
            resolve(res);
        }))));
        if(ip === undefined) {
            logger.info('Skipped '+it);
        } else {
            if ((await Node.find({ip: ip}).count()) === 0)
              Node.create({
                ip: ip,
                port: config.vertcoin.port
              });
        }
      }));
      logger.info('Seed notes added');
    }

    if((await Pool.count()) === 0) {
      logger.info('Bootstrapping pool search with seeds');
      await Promise.all(_.map(config.p2pool.net1.seeds, async (it) => {
        let ip = _.head(await (new Promise((resolve, reject) => dns.resolve(it, (err, res) => {
          if (err)
            resolve([]);
          else
            resolve(res);
        }))));

        if(ip === undefined) {
            logger.info('Skipped '+it);
        } else {
            if ((await Pool.find({ip: ip+':'+config.p2pool.net1.port}).count() === 0)) {
              Pool.create({
                ip: ip+':'+config.p2pool.net1.port,
                dns: it
              });
            }
        }
      }));
      logger.info('Seed pools for net1 added');

      await Promise.all(_.map(config.p2pool.net2.seeds, async (it) => {
        let ip = _.head(await (new Promise((resolve, reject) => dns.resolve(it, (err, res) => {
          if (err)
            resolve([]);
          else
            resolve(res);
        }))));
        if(ip === undefined) {
            logger.info('Skipped '+it);
        } else {
            if ((await Pool.find({ip: ip+':'+config.p2pool.net2.port}).count() === 0)) {
              Pool.create({
                ip: ip+':'+config.p2pool.net2.port,
                dns: it
              });
            }
        }
      }));
      logger.info('Seed pools for net2 added');
      
    }

    server.listen(config.app.port, async () => {
      logger.info('%s listening at %s', server.name, server.url);

      crons(server);
    });
  }).catch(logger.debug);
