OTM/server.js
choelzl 82b7fd846b
All checks were successful
continuous-integration/drone/push Build is passing
init
2021-07-16 09:15:33 +02:00

89 lines
1.7 KiB
JavaScript

const fastify = require('fastify')();//{ logger: true });
const path = require('path');
const crypto = require('crypto');
const csass = require("./compile_sass").compileSassMain();
const static_opts = {
root: path.join(__dirname, 'public'),
prefix: '/public/',
};
const pov_opts = {
engine: {
ejs: require('ejs')
}
};
fastify.hashgen = (str)=>{
return crypto.createHash('md5').update('CHELCEL_PRE_SALT_##'+str+'##_SALT_POST_CHELCEL').digest('hex');
};
fastify.cookiegen = (request, type, file_hash)=>{
let tk = {};
if(request.cookies.token ){
tk = fastify.jwt.decode(request.cookies.token);
}
if(tk[type]== undefined){
tk[type]=[];
}
if(tk[type].indexOf(file_hash)== -1){
tk[type].push(file_hash);
}
const token = fastify.jwt.sign(tk)
return token;
}
fastify.register(require('fastify-static'), static_opts);
fastify.register(require('fastify-cookie'));
fastify.register(require('point-of-view'), pov_opts);
fastify.register(require('fastify-leveldb'), {
name: 'db'
}, err => {
if (err) throw err
});
fastify.get('/', (req, reply) => {
reply.view('/template/home.html');
});
fastify.get('/dbg', (req, reply) => {
reply.view('/template/templates.html');
});
fastify.get('/:id', (req, reply) => {
try{
const ec = parseInt(req.params.id);
switch(ec){
case 400:
case 401:
case 402:
case 403:
case 404:
case 405:
reply.code(ec).send("Client Error");
break;
case 500:
reply.code(ec).send("Internal Error");
break;
default:
throw undefined;
}
}catch(e){
reply.view('/template/journey.html');
}
});
fastify.register(require('./router/api'), { prefix: '/api' });
fastify.listen(8080,'0.0.0.0' ,(err,address) => {
if (err) throw err;
console.log("Listening on", address);
});