import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import readline from 'readline';
import express from 'express';
import redis from 'redis';
import debug from 'debug';

// APIs
import v1 from './routes/v1/api.js';

// config
import serverConfig from './config/server.json';
import apiConfig from './config/api.json';

const mongoOptions = {
  'db': { 'native_parser': true },
  'server': { 'poolSize': 5 },
  'user': serverConfig.db.mongo.user,
  'pass': serverConfig.db.mongo.pass
};

const app = express();
const mongo = mongoose.connection;

const serverLog = debug("uwave:server");
const mongoLog = debug("uwave:database:mongo");

mongo.on('error', e => mongoLog(e));
mongo.on('reconnected', () => mongoLog("reconnected"));
mongo.on('disconnected', () => mongoLog("disconnected"))

mongo.once('open', () => {
  // Initialize app now that we have a connection to the database
  mongoLog('connection successfull');
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: true }));

  // here you can invoke further API versions
  v1(app, apiConfig.v1);
});


// workaround to properly close all connections on termination.
if(process.platform === "win32") {
  readline.createInterface({
    'input': process.stdin,
    'output': process.stdout
  }).on('SIGINT', () => process.emit('SIGINT'));
}

process.on('SIGINT', () => {
  mongoose.connection.close(() => {
    mongoLog('closing connection to mongo');
    process.exit(0);
  });
});

mongoLog(`connecting to database '${serverConfig.db.mongo.host}/uwave'...`);
mongoose.connect(`mongodb://${serverConfig.db.mongo.host}/uwave:${serverConfig.db.mongo.port}`, mongoOptions);
@@ -295,19 +295,21 @@
 %0Aimport 
-api
+mongo
 Config f
@@ -326,19 +326,21 @@
 fig/
-api
+mongo
 .json';%0A
 %0Acon
@@ -339,174 +339,49 @@
 n';%0A
-%0Aconst mongoOptions = %7B%0A  'db': %7B 'native_parser': true %7D,%0A  'server': %7B 'poolSize': 5 %7D,%0A  'user': serverConfig.db.mongo.user,%0A  'pass': serverConfig.db.mongo.pass%0A%7D
+import apiConfig from './config/api.json'
 ;%0A%0Ac
@@ -1387,37 +1387,27 @@
 base '$%7B
-serverConfig.db.mongo
+mongoConfig
 .host%7D/u
@@ -1448,38 +1448,28 @@
 odb://$%7B
-server
+mongo
 Config.
-db.mongo.
 host%7D/uw
@@ -1478,29 +1478,19 @@
 e:$%7B
-serverConfig.db.mongo
+mongoConfig
 .por
@@ -1503,9 +1503,16 @@
 ongo
-O
+Config.o
 ptio

move mongo config to config/mongo.json