web - remove file state caching
This commit is contained in:
parent
dcbe7509d2
commit
2420bd2e80
1
app.js
1
app.js
@ -8,7 +8,6 @@
|
|||||||
// GDS: Global Data System
|
// GDS: Global Data System
|
||||||
global['gds'] = {
|
global['gds'] = {
|
||||||
debug: (process.env.NODE_ENV === 'debug') ? true : false,
|
debug: (process.env.NODE_ENV === 'debug') ? true : false,
|
||||||
cache: {},
|
|
||||||
cfg: require(__dirname+'/bin/config')
|
cfg: require(__dirname+'/bin/config')
|
||||||
};
|
};
|
||||||
global['__dirname'] = __dirname;
|
global['__dirname'] = __dirname;
|
||||||
|
@ -45,24 +45,11 @@ methods.start = () => {
|
|||||||
|
|
||||||
//static files
|
//static files
|
||||||
app.use('/res', (req, res, next) => {
|
app.use('/res', (req, res, next) => {
|
||||||
if(typeof global['gds'].cache.web == 'undefined') global['gds'].cache.web = {};
|
|
||||||
|
|
||||||
let dir = global['__dirname'] + '/res/web';
|
let dir = global['__dirname'] + '/res/web';
|
||||||
let joined_path = path.join(dir, /^[^?]+/.exec(req.url)[0]);
|
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) => {
|
fs.exists(joined_path, (exists) => {
|
||||||
global['gds'].cache.web[joined_path] = exists;
|
|
||||||
if(exists) {
|
if(exists) {
|
||||||
let contentType = mime.contentType(path.extname(joined_path));
|
let contentType = mime.contentType(path.extname(joined_path));
|
||||||
res.setHeader('Content-Type', contentType);
|
res.setHeader('Content-Type', contentType);
|
||||||
@ -73,7 +60,6 @@ methods.start = () => {
|
|||||||
global['logs'].info("[web] (404) path not found: "+joined_path);
|
global['logs'].info("[web] (404) path not found: "+joined_path);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// BodyParser & CookieParser
|
// BodyParser & CookieParser
|
||||||
|
@ -13,26 +13,6 @@ path = require('path');
|
|||||||
|
|
||||||
var cfg = require(global['__dirname']+'/bin/config');
|
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 getRoutes = async () => {
|
||||||
let db = global['requireModule']('database');
|
let db = global['requireModule']('database');
|
||||||
await db.connect();
|
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(/^\//, ''), {
|
return res.render(req.path.replace(/^\//, ''), {
|
||||||
session: req.session,
|
session: req.session,
|
||||||
apps: apps.reply,
|
apps: apps.reply,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user