'use strict';

// ------------------------------------------------------------------
// Dependencies
// ------------------------------------------------------------------
var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var morgan = require('morgan');

//var fs = require('fs');
//var path = require('path');

var config = require('./app/config');
var port = config.port || process.env.PORT || 3000;

// ------------------------------------------------------------------
// Application Setup
// ------------------------------------------------------------------
// Create Express Server
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(morgan('dev'));

// Static content
app.use('/lib', express.static(__dirname + '/node_modules/'));
app.use('/', express.static(__dirname + '/pub'));

// CORS
app.use(function(req, res, next) {
	res.header("Access-Control-Allow-Origin", "*");
	res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
	next();
});

app.get('/api/', function (req, res) {
	var test = { hello: "world" };
	res.json(test);
});

// ------------------------------------------------------------------
// Start Server
// ------------------------------------------------------------------
app.listen(port);
console.log("Server started. Listening on port: " + port);

process.on('SIGINT', function() {
	console.log("\nGracefully shutting down from SIGINT (Ctrl-C)");

	process.exit(0);
});