2014-08-13 21:06:14 +00:00
|
|
|
var express = require('express');
|
|
|
|
var app = express();
|
2014-08-27 23:04:27 +00:00
|
|
|
var compression = require('compression');
|
|
|
|
var serveStatic = require('serve-static');
|
2014-09-02 22:19:42 +00:00
|
|
|
var fs = require('fs');
|
2014-08-13 21:06:14 +00:00
|
|
|
|
|
|
|
// Configure ejs engine
|
2014-08-27 23:04:27 +00:00
|
|
|
app.set('views', __dirname + '/../views');
|
2014-08-13 21:06:14 +00:00
|
|
|
app.engine('html', require('ejs').renderFile);
|
|
|
|
|
|
|
|
// Force HTTPS on stackedit.io
|
|
|
|
app.all('*', function(req, res, next) {
|
|
|
|
if (req.headers.host == 'stackedit.io' && req.headers['x-forwarded-proto'] != 'https') {
|
2014-08-18 23:30:45 +00:00
|
|
|
return res.redirect('https://stackedit.io' + req.url);
|
2014-08-13 21:06:14 +00:00
|
|
|
}
|
2014-08-31 19:51:13 +00:00
|
|
|
/\.(eot|ttf|woff|svg)$/.test(req.url) && res.header('Access-Control-Allow-Origin', '*');
|
2014-08-18 23:30:45 +00:00
|
|
|
next();
|
2014-08-13 21:06:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Use gzip compression
|
2014-08-27 23:04:27 +00:00
|
|
|
app.use(compression());
|
2014-08-13 21:06:14 +00:00
|
|
|
|
|
|
|
// Serve static resources
|
2014-08-31 19:51:13 +00:00
|
|
|
var staticOverride = process.env.STATIC_OVERRIDE;
|
|
|
|
staticOverride && app.use(serveStatic(__dirname + '/../' + staticOverride));
|
2014-08-27 23:04:27 +00:00
|
|
|
app.use(serveStatic(__dirname + '/../public'));
|
|
|
|
|
|
|
|
app.post('/pdfExport', require('./pdf').export);
|
|
|
|
app.post('/sshPublish', require('./ssh').publish);
|
|
|
|
app.post('/picasaImportImg', require('./picasa').importImg);
|
|
|
|
app.get('/downloadImport', require('./download').importPublic);
|
|
|
|
|
2014-09-02 22:19:42 +00:00
|
|
|
var packageJson = JSON.parse(fs.readFileSync(__dirname + '/../package.json', {
|
|
|
|
encoding: 'utf8'
|
|
|
|
}));
|
|
|
|
var cdnLocation = staticOverride == 'public-stackedit.io' ? '//stackedit.s3.amazonaws.com/' + packageJson.version + '/' : '';
|
2014-08-27 23:04:27 +00:00
|
|
|
app.use(function(req, res, next) {
|
|
|
|
res.renderDebug = function(page) {
|
|
|
|
return res.render(page, {
|
2014-08-31 19:51:13 +00:00
|
|
|
cache: !req.query.hasOwnProperty('debug'),
|
|
|
|
cdn: cdnLocation
|
2014-08-27 23:04:27 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Serve landing.html in /
|
|
|
|
app.get('/', function(req, res) {
|
|
|
|
res.renderDebug('landing.html');
|
|
|
|
});
|
2014-08-13 21:06:14 +00:00
|
|
|
|
2014-08-25 08:18:14 +00:00
|
|
|
// Serve editor.html in /viewer
|
2014-08-27 23:04:27 +00:00
|
|
|
app.get('/editor', function(req, res) {
|
|
|
|
res.renderDebug('editor.html');
|
2014-08-25 08:18:14 +00:00
|
|
|
});
|
|
|
|
|
2014-08-13 21:06:14 +00:00
|
|
|
// Serve viewer.html in /viewer
|
2014-08-27 23:04:27 +00:00
|
|
|
app.get('/viewer', function(req, res) {
|
|
|
|
res.renderDebug('viewer.html');
|
2014-08-13 21:06:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Error 404
|
2014-08-27 23:04:27 +00:00
|
|
|
app.use(function(req, res) {
|
2014-08-13 21:06:14 +00:00
|
|
|
res.status(404);
|
|
|
|
res.render('error_404.html');
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = app;
|