2018-07-30 16:59:41 -04:00
|
|
|
const PORT = 8090;
|
2018-08-23 21:41:06 -04:00
|
|
|
var fs = require('fs');
|
|
|
|
var https = require('https');
|
|
|
|
var http = require('http');
|
2018-07-23 16:58:23 -04:00
|
|
|
var express = require('express');
|
|
|
|
var app = express();
|
|
|
|
|
2018-08-23 21:41:06 -04:00
|
|
|
var credentials;
|
|
|
|
var useHttps = true;
|
|
|
|
try {
|
|
|
|
var privateKey = fs.readFileSync('/etc/letsencrypt/live/lenord.me/privkey.pem', 'utf-8');
|
|
|
|
var certificate = fs.readFileSync('/etc/letsencrypt/live/lenord.me/fullchain.pem', 'utf-8');
|
|
|
|
credentials = {key: privateKey, cert: certificate};
|
|
|
|
} catch (error) {
|
|
|
|
useHttps = false;
|
|
|
|
console.warn('WARN::NOT USING HTTPS, reverting to HTTP');
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
|
2018-07-30 22:02:00 -04:00
|
|
|
console.log('Starting express server');
|
2018-07-25 08:23:17 -04:00
|
|
|
|
2018-07-23 16:58:23 -04:00
|
|
|
// set the view engine to ejs
|
|
|
|
app.set('view engine', 'ejs');
|
|
|
|
|
2018-07-25 08:23:17 -04:00
|
|
|
// add folder for static content:
|
|
|
|
app.use(express.static(__dirname + '/assets'));
|
2018-07-23 16:58:23 -04:00
|
|
|
|
2018-07-27 13:32:34 -04:00
|
|
|
app.get(/\/.*/, function(req, res) {
|
|
|
|
let pathname = 'pages' + req.path;
|
2018-07-31 16:09:51 -04:00
|
|
|
let page = pathname.substr(pathname.lastIndexOf('/') + 1);
|
|
|
|
|
|
|
|
if (pathname !== null && pathname !== undefined) {
|
|
|
|
if ((pathname)[pathname.length - 1] === '/') {
|
|
|
|
pathname += 'index';
|
|
|
|
page = 'index';
|
|
|
|
}
|
|
|
|
if (pathname.includes('projects') && page !== 'index') {
|
|
|
|
// projects has a custom template that is used for all projects
|
|
|
|
// so we need to change the pathname that the renderer is using
|
|
|
|
// to that template:
|
|
|
|
pathname = pathname.substr(0, pathname.lastIndexOf(page));
|
|
|
|
pathname += 'project_template'
|
|
|
|
page = 'partials/md/' + page;
|
|
|
|
}
|
2018-07-27 13:32:34 -04:00
|
|
|
}
|
2018-07-31 16:09:51 -04:00
|
|
|
console.log('request for path: ' + pathname + ', and page: ' + page);
|
2018-07-27 13:32:34 -04:00
|
|
|
|
2018-08-02 20:44:04 -04:00
|
|
|
res.render(pathname, {"page": page});
|
2018-07-27 13:32:34 -04:00
|
|
|
});
|
|
|
|
|
2018-08-23 21:41:06 -04:00
|
|
|
if (useHttps) {
|
|
|
|
var httpsServer = https.createServer(credentials, app);
|
|
|
|
httpsServer.listen(PORT);
|
|
|
|
} else {
|
|
|
|
var httpServer = http.createServer(app);
|
|
|
|
httpServer.listen(PORT);
|
|
|
|
}
|