2018-10-31 17:00:31 +01:00
|
|
|
var express = require('express');
|
|
|
|
var path = require('path');
|
|
|
|
var favicon = require('serve-favicon');
|
|
|
|
var logger = require('morgan');
|
|
|
|
var cookieParser = require('cookie-parser');
|
|
|
|
var bodyParser = require('body-parser');
|
|
|
|
var session = require('express-session');
|
|
|
|
var MemoryStore = require('memorystore')(session)
|
|
|
|
var flash = require('connect-flash');
|
|
|
|
var theme = "default";
|
|
|
|
var app = express();
|
2018-11-21 01:42:52 +01:00
|
|
|
var request = require('request');
|
2018-10-31 17:00:31 +01:00
|
|
|
// view engine setup
|
|
|
|
app.set('views', path.join(__dirname, '../themes'));
|
|
|
|
app.set('view engine', 'pug');
|
|
|
|
app.use(express.static(__dirname + '../content/folio-images'));
|
|
|
|
app.use(logger('dev'));
|
2018-11-14 17:32:35 +01:00
|
|
|
|
|
|
|
// wherever your db lives
|
2018-11-21 01:42:52 +01:00
|
|
|
var DATABASE_URL = 'http://are0h:pa$$@localhost:5984/forfipamo';
|
2018-11-14 17:32:35 +01:00
|
|
|
|
|
|
|
// middleware itself, preceding any parsers
|
|
|
|
app.use(function(req, res, next){
|
|
|
|
var proxy_path = req.path.match(/^\/forfipamo(.*)$/);
|
|
|
|
if(proxy_path){
|
|
|
|
var db_url = DATABASE_URL + proxy_path[1];
|
2018-11-21 01:42:52 +01:00
|
|
|
console.log("YUP "+db_url);
|
|
|
|
console.log("METH: "+req)
|
2018-11-14 17:32:35 +01:00
|
|
|
req.pipe(request({
|
|
|
|
uri: db_url,
|
|
|
|
method: req.method
|
|
|
|
})).pipe(res);
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-11-21 01:42:52 +01:00
|
|
|
app.use(bodyParser.json({limit: '50mb'}));
|
2018-11-11 21:22:01 +01:00
|
|
|
app.use(bodyParser.urlencoded(
|
|
|
|
{
|
2018-11-21 01:42:52 +01:00
|
|
|
extended: false,
|
|
|
|
limit: '50mb'
|
2018-10-31 17:00:31 +01:00
|
|
|
}));
|
|
|
|
app.use(cookieParser());
|
|
|
|
app.use(express.static(path.join(__dirname, '../content')));
|
|
|
|
app.use(express.static(path.join(__dirname, '../themes')));
|
2018-11-11 21:22:01 +01:00
|
|
|
app.use(session(
|
|
|
|
{
|
|
|
|
store: new MemoryStore(
|
|
|
|
{
|
2018-10-31 17:00:31 +01:00
|
|
|
checkPeriod: 86400000 // prune expired entries every 24h
|
|
|
|
}),
|
|
|
|
secret: '1KqZ18W8KskE1iSw',
|
|
|
|
saveUninitialized: false,
|
|
|
|
resave: false,
|
2018-11-11 21:22:01 +01:00
|
|
|
cookie:
|
|
|
|
{
|
|
|
|
maxAge: 608800000
|
|
|
|
}
|
2018-10-31 17:00:31 +01:00
|
|
|
}))
|
|
|
|
app.use(flash());
|
|
|
|
//sections
|
|
|
|
var front = require('./routes/front/index')(session);
|
|
|
|
var back = require('./routes/back/index');
|
2018-11-11 21:22:01 +01:00
|
|
|
var postDashboard = require('./routes/back/dash_posts');
|
2018-10-31 17:00:31 +01:00
|
|
|
//api
|
|
|
|
var folioLibrary = require('./api/content/folio');
|
|
|
|
var projectLibrary = require('./api/content/project');
|
|
|
|
var bookmarkLibrary = require('./api/content/bookmarks');
|
|
|
|
var postLibrary = require('./api/content/posts');
|
|
|
|
var mailer = require('./api/content/mailer');
|
|
|
|
// API PATHS
|
|
|
|
app.use('/api/folio', folioLibrary);
|
|
|
|
app.use('/api/projects', projectLibrary);
|
|
|
|
app.use('/api/bookmarks', bookmarkLibrary);
|
2018-11-11 00:08:00 +01:00
|
|
|
app.use('/api/post', postLibrary);
|
2018-10-31 17:00:31 +01:00
|
|
|
// PAGES
|
|
|
|
app.use('/', front);
|
|
|
|
app.use('/@/dashboard', back);
|
2018-11-11 21:22:01 +01:00
|
|
|
app.use('/@/dashboard/posts', postDashboard);
|
2018-10-31 17:00:31 +01:00
|
|
|
//app.use('/mailer', mailer);
|
|
|
|
// catch 404 and forward to error handler
|
2018-11-11 21:22:01 +01:00
|
|
|
app.use(function(req, res, next)
|
|
|
|
{
|
2018-10-31 17:00:31 +01:00
|
|
|
var err = new Error('Not Found');
|
|
|
|
err.status = 404;
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
// error handlers
|
|
|
|
// development error handler
|
|
|
|
// will print stacktrace
|
2018-11-11 21:22:01 +01:00
|
|
|
if (app.get('env') === 'development')
|
|
|
|
{
|
|
|
|
app.use(function(err, req, res, next)
|
|
|
|
{
|
2018-10-31 17:00:31 +01:00
|
|
|
res.status(err.status || 500);
|
2018-11-11 21:22:01 +01:00
|
|
|
res.render(theme + '/error',
|
|
|
|
{
|
2018-10-31 17:00:31 +01:00
|
|
|
message: err.message,
|
|
|
|
error: err
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// production error handler
|
|
|
|
// no stacktraces leaked to user
|
2018-11-11 21:22:01 +01:00
|
|
|
app.use(function(err, req, res, next)
|
|
|
|
{
|
2018-10-31 17:00:31 +01:00
|
|
|
res.status(err.status || 500);
|
2018-11-11 21:22:01 +01:00
|
|
|
res.render(theme + '/error',
|
|
|
|
{
|
2018-10-31 17:00:31 +01:00
|
|
|
message: err.message,
|
2018-11-11 21:22:01 +01:00
|
|
|
error:
|
|
|
|
{}
|
2018-10-31 17:00:31 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
module.exports = app;
|