require('./lib/mongoose-connection');
var redis = require('./lib/redis-connection');
var express = require("express");
var path = require('path');
var bodyParser = require('body-parser');
var pdf = require('pdfcrowd');
var client = new pdf.Pdfcrowd('thomasdavis', '7d2352eade77858f102032829a2ac64e');
var app = express();
var request = require('superagent');
var expressSession = require('express-session');
var cookieParser = require('cookie-parser');
var compress = require('compression');
var minify = require('express-minify');
var controller = require('./controller');

var points = [];
var DEFAULT_THEME = 'nominaltech';

var RedisStore = require('connect-redis')(expressSession);

app.set('view engine', 'ejs');
app.use(compress());

app.use(minify({
    cache: __dirname + '/cache'
}));

app.use(require('./middleware/allow-cross-domain'));
app.use(cookieParser());

app.use(expressSession({
    store: new RedisStore({
        client: redis
    }),
    secret: 'keyboard cat'
}));

app.use(express.static(__dirname + '/public', {
    maxAge: 0
}));

app.use(bodyParser());
var fs = require('fs');
var guid = (function() {
    function s4() {
        return Math.floor((1 + Math.random()) * 0x10000)
            .toString(16)
            .substring(1);
    }
    return function() {
        return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
            s4() + '-' + s4() + s4() + s4();
    };
})();


function S4() {
    return Math.floor((1 + Math.random()) * 0x10000)
        .toString(16)
        .substring(1);
};

app.get('/', controller.home);
app.get('/edit', controller.resumeEditor);

app.all('/*', function(req, res, next) {
    //res.header("Access-Control-Allow-Origin", "*");
    //res.header("Access-Control-Allow-Headers", "X-Requested-With");

    // Make the db accessible to the router
    // probably not the most performant way to pass the db's around
    // TODO find a better way
    req.redis = redis;
    next();
});


app.get('/session', controller.checkSession);
app.post('/session', controller.createSession);
app.delete('/session/:id', controller.deleteSession);

app.get('/members', controller.renderMembersPage);
app.get('/stats', controller.showStats);

app.get('/pdf', function(req, res) {
    console.log(req.body.resume, req.body.theme);
    request
        .post('https://themes.jsonresume.org/theme/' + req.body.theme)
        .send({
            resume: req.body.resume
        })
        .set('Content-Type', 'application/json')
        .end(function(err, response) {
            client.convertHtml(response.text, pdf.sendHttpResponse(res), {
                use_print_media: "true"
            });
        });
});

app.get('/:uid.:format', controller.renderResume);
app.get('/:uid', controller.renderResume);
app.get('/:uid/stats', controller.showUserStats);
app.post('/:uid', controller.renderResume);

app.post('/resume', controller.upsertResume);
app.put('/resume', controller.updateTheme);

app.post('/user', controller.createUser);

app.put('/account', controller.changePassword);
app.delete('/account', controller.deleteUser);


process.addListener('uncaughtException', function(err) {
    console.error('Uncaught error in server.js', {
        err: err
        // hide stack in production
        //, stack: err.stack
    });
    // TODO some sort of notification
    // process.exit(1);
});

var port = Number(process.env.PORT || 5000);

app.listen(port, function() {
    console.log("Listening on " + port);
});

module.exports = app;
module.exports.DEFAULT_THEME = DEFAULT_THEME;

@@ -1,20 +1,49 @@
+// require('dotenv').load();%0A
 require('./lib/mongo
@@ -705,25 +705,24 @@
 ssSession);%0A
-%0A
 app.set('vie
@@ -740,16 +740,17 @@
 'ejs');%0A
+%0A
 app.use(
@@ -758,25 +758,24 @@
 ompress());%0A
-%0A
 app.use(mini
@@ -812,25 +812,24 @@
 cache'%0A%7D));%0A
-%0A
 app.use(requ
@@ -1005,24 +1005,83 @@
 rd cat'%0A%7D));
+%0A//app.use(expressSession(%7Bsecret:'somesecrettokenhere'%7D));
 %0A%0Aapp.use(ex
@@ -1490,17 +1490,16 @@
 %0A%7D)();%0A%0A
-%0A
 function
@@ -1612,83 +1612,8 @@
 %7D;%0A%0A
-app.get('/', controller.home);%0Aapp.get('/edit', controller.resumeEditor);%0A%0A
 app.
@@ -1956,17 +1956,16 @@
 );%0A%7D);%0A%0A
-%0A
 app.get(
@@ -2006,56 +2006,8 @@
 n);%0A
-app.post('/session', controller.createSession);%0A
 app.
@@ -2056,17 +2056,16 @@
 ssion);%0A
-%0A
 app.get(
@@ -2148,16 +2148,121 @@
 Stats);%0A
+// export pdf route%0A// this code is used by resume-cli for pdf export, see line ~188 for web-based export
 %0Aapp.get
@@ -2715,32 +2715,67 @@
        %7D);%0A%7D);%0A%0A
+app.get('/home', controller.home);%0A
 app.get('/:uid.:
@@ -2906,53 +2906,8 @@
 s);%0A
-app.post('/:uid', controller.renderResume);%0A%0A
 app.
@@ -2992,17 +2992,16 @@
 Theme);%0A
-%0A
 app.post
@@ -3034,16 +3034,63 @@
 eUser);%0A
+app.post('/session', controller.createSession);
 %0Aapp.put
@@ -3177,16 +3177,59 @@
 eUser);%0A
+app.post('/:uid', controller.renderResume);
 %0A%0Aproces

fix router order