148 lines
3.6 KiB
JavaScript
148 lines
3.6 KiB
JavaScript
const express = require('express'); //TODO: make use of httpd istad of express
|
|
// A genareted db with all the pages
|
|
const siteMap = require('./siteMap');
|
|
// All compilers for all page source types
|
|
const compile = require('./compile');
|
|
const settings = require('./settings');
|
|
const db = require('./db')
|
|
db.init()
|
|
|
|
console.log("siteMap:", siteMap) //DEBUG
|
|
|
|
// create webserver
|
|
const app = express()
|
|
app.set('view engine', 'ejs');
|
|
app.set('views', __dirname + '/views')
|
|
|
|
// routing
|
|
app.get('/db/:collection/:file', function(req, res, next){
|
|
db.get(req.params.collecion, req.params.file, function(err, data){
|
|
if(err){
|
|
res.status(404)
|
|
res.send('FAIL')
|
|
return
|
|
}
|
|
res.type('json')
|
|
res.send(data)
|
|
})
|
|
})
|
|
app.post('/db/:collection/:file', function(req, res, next){
|
|
db.write(req.params.collecion, req.params.file, req.body, function(err){
|
|
if(err){
|
|
res.status(500)
|
|
res.send('FAIL')
|
|
return
|
|
}
|
|
res.send('OK')
|
|
})
|
|
})
|
|
app.use(renderPage)
|
|
app.use(render404)
|
|
|
|
//TODO: allow static content
|
|
function renderPage(req, res, next){
|
|
let path = req.path.split('/')
|
|
let cur = siteMap
|
|
|
|
// search for the page in siteMap
|
|
for (let i = 1; i < path.length; i++) {
|
|
const page = path[i];
|
|
|
|
if(cur.type == 'directory'){
|
|
// if page is not found render 404
|
|
if(typeof cur.subPages[page] == 'undefined'){
|
|
console.log(`404 - ${req.path}`) //TODO: add a logging system
|
|
return render404(req, res, next)
|
|
}
|
|
|
|
cur = cur.subPages[page]
|
|
continue
|
|
}
|
|
|
|
// page is not a directory so it's a page!
|
|
cur = cur.subPages[page]
|
|
break
|
|
}
|
|
|
|
let page = cur
|
|
delete cur, path
|
|
|
|
if(page.type == 'directory'){
|
|
page = page.index
|
|
}
|
|
|
|
compile(page, function(html){
|
|
if(typeof html != 'string') return render500() //TODO: add a logging system
|
|
|
|
res.render('main', {
|
|
siteMap,
|
|
path: req.path,
|
|
page: page,
|
|
main: html
|
|
})
|
|
})
|
|
}
|
|
|
|
/** Render static content
|
|
*
|
|
* looks for the content in the folder static
|
|
*
|
|
* @param {Req} req Request object from Express.
|
|
* @param {Res} res Responce object from Express.
|
|
* @param {Function} next The next function from Espress.
|
|
*/
|
|
// function renderStatic(req, res, next){
|
|
// if(typeof siteMap[req.params.vak] == 'undefined') return next();
|
|
// vak = siteMap[req.params.vak]
|
|
|
|
// dir = __dirname.split('/'); dir.pop() // remove dir of this code
|
|
// dir = `${dir.join('/')}/${vak.title}/notes/static${req.path}`
|
|
|
|
// res.sendFile(dir, onError404(req, res, next))
|
|
// }
|
|
|
|
//TODO: add a custom and/or smarter 404 page
|
|
/** Render the 404 page
|
|
*
|
|
* renders a simple 404 page.
|
|
*
|
|
* @param {Req} req Request object from Express.
|
|
* @param {Res} res Responce object from Express.
|
|
* @param {Function} next The next function from Espress.
|
|
*/
|
|
function render404(req, res, next){
|
|
res.status(404)
|
|
// res.render('main', {
|
|
// siteMap,
|
|
// main: "<h1>404 - Page not found</h1>"
|
|
// }, (err) => {
|
|
// if(err){
|
|
res.send("<h1>404 - Page not found</h1>")
|
|
// }
|
|
// })
|
|
}
|
|
|
|
//TODO: add a custom 500 page
|
|
/** Render the 500 page
|
|
*
|
|
* renders a simple 500 page.
|
|
*
|
|
* @param {Req} req Request object from Express.
|
|
* @param {Res} res Responce object from Express.
|
|
* @param {Function} next The next function from Espress.
|
|
*/
|
|
function render500(req, res, next){
|
|
res.status(500)
|
|
// res.render('main', {
|
|
// siteMap,
|
|
// main: "<h1>500 - Internal server error</h1>"
|
|
// }, (err) => {
|
|
// if(err){
|
|
res.send("<h1>500 - Internal server error</h1>")
|
|
// }
|
|
// })
|
|
}
|
|
|
|
// start the actial web server
|
|
app.listen(settings.serverPort, ()=>{console.log("server listening on port", settings.serverPort)})
|