1
0
Fork 0

web - remove file state caching

This commit is contained in:
Ruben Meyer 2020-08-31 09:35:18 +02:00
parent dcbe7509d2
commit 2420bd2e80
Signed by: rxbn_
GPG Key ID: BE3BF898BE352FE2
3 changed files with 13 additions and 48 deletions

1
app.js
View File

@ -8,7 +8,6 @@
// GDS: Global Data System
global['gds'] = {
debug: (process.env.NODE_ENV === 'debug') ? true : false,
cache: {},
cfg: require(__dirname+'/bin/config')
};
global['__dirname'] = __dirname;

View File

@ -45,24 +45,11 @@ methods.start = () => {
//static files
app.use('/res', (req, res, next) => {
if(typeof global['gds'].cache.web == 'undefined') global['gds'].cache.web = {};
let dir = global['__dirname'] + '/res/web';
let joined_path = path.join(dir, /^[^?]+/.exec(req.url)[0]);
// path already cached; not exist
if(global['gds'].cache.web[joined_path] == false) {
res.status(404).end();
global['logs'].info("[web] (404) path not found: "+joined_path);
// path already cached; exist
} else if(global['gds'].cache.web[joined_path] == true){
let contentType = mime.contentType(path.extname(joined_path));
res.setHeader('Content-Type', contentType);
fs.createReadStream(joined_path).pipe(res);
// check path
} else {
fs.exists(joined_path, (exists) => {
global['gds'].cache.web[joined_path] = exists;
if(exists) {
let contentType = mime.contentType(path.extname(joined_path));
res.setHeader('Content-Type', contentType);
@ -73,7 +60,6 @@ methods.start = () => {
global['logs'].info("[web] (404) path not found: "+joined_path);
}
});
}
});
// BodyParser & CookieParser

View File

@ -13,26 +13,6 @@ path = require('path');
var cfg = require(global['__dirname']+'/bin/config');
// reduce IO file checks - save file state in cache
var fileCheck = (file) => {
if(typeof global['gds'].cache.web == 'undefined') global['gds'].cache.web = {};
let dir = global['__dirname'] + '/bin/web/views';
let path_j = path.join(dir, file.toLowerCase());
if(typeof global['gds'].cache.web[path_j] == 'undefined') {
if(fs.existsSync(path_j+'.pug')) {
global['gds'].cache.web[path_j] = true;
} else {
global['gds'].cache.web[path_j] = false;
}
}
if(global['gds'].cache.web[path_j] === true) {
return path_j;
} else {
return false;
}
};
let getRoutes = async () => {
let db = global['requireModule']('database');
await db.connect();
@ -164,10 +144,10 @@ let getRoutes = async () => {
}
});
if(fileCheck(req.path)) {
// query apps
apps = await db.getApps();
let dir = global['__dirname'] + '/bin/web/views';
let path_j = path.join(dir, req.path.toLowerCase());
if(fs.existsSync(path_j+'.pug')) {
return res.render(req.path.replace(/^\//, ''), {
session: req.session,
apps: apps.reply,