'use strict'

const express = require('express');
const app = express();
const routes = require('./routes');
const log = require('./bin/config').log;
const bodyParser = require('body-parser');
const url = require('url');
var path = require("path");


//Expect a JSON body
app.use(bodyParser.json({
    limit: '50mb'                   //Request size - 50MB
}));

//Handle CORS
app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');

    if ('OPTIONS' == req.method) {
        res.status(200).send();
    } else {
        next();
    }
});


//Health Checker
app.use('/healthz', async (req, res) => {

    return res.status(200).json({
        ping: 'PONG'
    }) 
});


//Version
app.use('/ver', (req, res) => {
    res.status(200).send('0.1.0');
});



//------------------------- Open Routes -------------------------//
app.use('/test', routes.test);
app.use('/api/process', routes.language_handler);

app.use(express.static(path.join(__dirname, '../public')));

module.exports = app;