feature(template) add: read all templates on start

This commit is contained in:
coderaiser 2017-09-27 13:05:37 +03:00
parent 29ac0677af
commit 671101d201
2 changed files with 32 additions and 50 deletions

View file

@ -1,16 +1,13 @@
'use strict';
const DIR = __dirname + '/../';
const DIR_TMPL = DIR + 'tmpl/';
const DIR_SERVER = './';
const DIR_COMMON = '../common/';
const DIR_FS = DIR_TMPL + 'fs/';
const fs = require('fs');
const flop = require('flop');
const ponse = require('ponse');
const files = require('files-io');
const rendy = require('rendy');
const format = require('format-io');
const squad = require('squad');
@ -20,8 +17,10 @@ const config = require(DIR_SERVER + 'config');
const root = require(DIR_SERVER + 'root');
const prefixer = require(DIR_SERVER + 'prefixer');
const CloudFunc = require(DIR_COMMON + 'cloudfunc');
const prefix = squad(prefixer, apart(config, 'prefix'));
const Template = require(DIR_SERVER + 'template')();
const prefix = squad(prefixer, apart(config, 'prefix'));
const isDev = process.env.NODE_ENV === 'development';
const getIndexPath = () => {
@ -29,23 +28,12 @@ const getIndexPath = () => {
return DIR + `${dist}/index.html`;
};
const TMPL_PATH = [
'file',
'panel',
'path',
'pathLink',
'link'
];
const Template = {};
const FS = CloudFunc.FS;
module.exports = (req, res, next) => {
check(req, res, next);
readFiles(() => {
route(req, res, next);
});
route(req, res, next);
};
/**
@ -127,40 +115,6 @@ function indexProcessing(options) {
return data;
}
function readFiles(callback) {
const paths = {};
const lengthTmpl = Object.keys(Template).length;
const lenthPath = TMPL_PATH.length;
const isRead = lengthTmpl === lenthPath;
if (typeof callback !== 'function')
throw Error('callback should be a function!');
if (isRead)
return callback();
const filesList = TMPL_PATH.map((name) => {
const path = DIR_FS + name + '.hbs';
paths[path] = name;
return path;
});
files.read(filesList, 'utf8', (error, files) => {
if (error)
throw error;
Object.keys(files).forEach((path) => {
const name = paths[path];
Template[name] = files[path];
});
callback();
});
}
/**
* routing of server queries
*/

28
server/template.js Normal file
View file

@ -0,0 +1,28 @@
'use strict';
const fs = require('fs');
const join = require('path').join;
const DIR = __dirname + '/../';
const DIR_TMPL = DIR + 'tmpl/';
const DIR_FS = DIR_TMPL + 'fs/';
const TMPL_PATH = [
'file',
'panel',
'path',
'pathLink',
'link',
];
module.exports = () => {
const templates = {};
TMPL_PATH.forEach((name) => {
const path = join(DIR_FS, `${name}.hbs`);
templates[name] = fs.readFileSync(path, 'utf8');
});
return templates;
};