/*
 * Provides general information 
 * Created by: Michael Pastushkov <michael@pastushkov.com>
 */

const config = require('config')
const utils = require('../utils/utils');
const Formats = require('../utils/formats');

let server;
let routes;

async function info(req, res) {

    if (req.query.log_level) {
        level = Number(req.query.log_level);
        utils.set_log_level(level);
        console.log(`Log level temporarily set to ${level}`);
    }

    let ret = {
        time: Formats.DateTime(new Date()),
        name: server.name,
        version: server.version,
        routing: [],
    };

    for (let route of routes.routing)
        ret.routing.push(`${route[0]} ${route[1]}`);

    res.send(ret);
}

function init(srv, rte) {
    server = srv;
    routes = rte.exports;
}

module.exports.run = info;
module.exports.init = init;
