(function() { 'use strict'; var DIR = __dirname + '/../../', DIR_HTML = DIR + 'html/', DIR_LIB = DIR + 'lib/', DIR_SERVER = __dirname + '/', DIR_FS = DIR_HTML + 'fs/', fs = require('fs'), mellow = require('mellow'), ponse = require('ponse'), files = require('files-io'), minify = require('minify'), config = require(DIR_SERVER + 'config'), Util = require(DIR_LIB + 'util'), CloudFunc = require(DIR_LIB + 'cloudfunc'), format = require(DIR_LIB + 'format'), PATH_INDEX = DIR_FS + 'index.html', TMPL_PATH = [ 'file', 'panel', 'path', 'pathLink', 'link', ], Template = {}, FS = CloudFunc.FS; module.exports = function(req, res, next) { readFiles(function() { route(req, res, next); }); }; /** * additional processing of index file */ function indexProcessing(options) { var keysPanel, left, right, LEFT = CloudFunc.PANEL_LEFT, RIGHT = CloudFunc.PANEL_RIGHT, data = options.data, panel = options.panel; if (!config('showKeysPanel')) { keysPanel = '
' + name); p.name = DIR_HTML + name + '.html'; ponse.sendFile(p); } else if (isFS) { name = name.replace(CloudFunc.FS, '') || '/'; path = mellow.convertPath(name); mellow.read(path, function(error, dir) { if (dir) dir.path = format.addSlashToEnd(name); if (error) if (error.code !== 'ENOTDIR') ponse.sendError(error, p); else fs.realpath(path, function(error, pathReal) { if (!error) p.name = pathReal; else p.name = path; p.gzip = false; ponse.sendFile(p); }); else buildIndex(dir, function(error, data) { var NOT_LOG = true; p.name = PATH_INDEX; if (error) ponse.sendError(error, p); else ponse.send(data, p, NOT_LOG); }); }); } } function buildIndex(json, callback) { var isMinify = config('minify'); Util.exec.if(!isMinify, function(error, name) { fs.readFile(name || PATH_INDEX, 'utf8', function(error, template) { var panel, data; if (!error) { panel = CloudFunc.buildFromJSON({ data : json, template : Template }), data = indexProcessing({ panel : panel, data : template, }); } Util.exec(callback, error, data); }); }, function(callback) { minify(PATH_INDEX, 'name', callback); }); } })();