-
Notifications
You must be signed in to change notification settings - Fork 2
/
web.js
104 lines (95 loc) · 3.89 KB
/
web.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// jscs:disable requireCapitalizedComments
/**
* Created by kras on 06.07.16.
*/
'use strict';
const path = require('path');
const express = require('express');
const route = express.Router;
const router = route();
const ejsLocals = require('ejs-locals');
const di = require('core/di');
const config = require('./config');
const moduleName = require('./module-name');
const dispatcher = require('./dispatcher');
const staticRouter = require('lib/util/staticRouter');
const extViews = require('lib/util/extViews');
const extendDi = require('core/extendModuleDi');
const theme = require('lib/util/theme');
const alias = require('core/scope-alias');
const sysMenuCheck = require('lib/util/sysMenuCheck');
const lastVisit = require('lib/last-visit');
const viewPathResolver = require('lib/util/viewResolver');
const errorSetup = require('core/error-setup');
const strings = require('core/strings');
const {load} = require('core/i18n');
const isProduction = process.env.NODE_ENV === 'production';
errorSetup(path.join(__dirname, 'strings'));
strings.registerBase('frontend', require('./strings/frontend-scripts'));
strings.registerBase('tpl', require('./strings/templates-default'));
router.get('/public/:mine/:report/:sheet', dispatcher.pubSheet);
router.get('/public/:mine/:report/:sheet/:template', dispatcher.pubSheet);
router.get('/', dispatcher.index);
router.get('/:mine/build', dispatcher.build);
router.get('/:mine/check', dispatcher.check);
router.get('/:mine/:report', lastVisit.saver, dispatcher.report);
router.get('/:mine/:report/:sheet', lastVisit.saver, dispatcher.sheet);
router.get('/:mine/:report/:sheet/data', dispatcher.data);
router.post('/:mine/:report/:sheet/data', dispatcher.data);
router.get('/:mine/:report/:sheet/:filterName/filter', dispatcher.filters);
router.get('/:mine/:report/:sheet/:filterName/param', dispatcher.filters);
router.post('/:mine/:report/:sheet/:format/start', dispatcher.export.start);
router.get('/:mine/:report/:sheet/:format/status', dispatcher.export.check);
router.get('/:mine/:report/:sheet/:format/download', dispatcher.export.download);
const app = express();
module.exports = app;
app.locals.sysTitle = config.sysTitle;
app.locals.staticsSuffix = process.env.ION_ENV === 'production' ? '.min' : '';
app.locals.resolveTpl = viewPathResolver(app);
app.use('/' + moduleName, express.static(path.join(__dirname, 'view/static')));
app.engine('ejs', ejsLocals);
app.set('view engine', 'ejs');
app._init = function () {
return load(path.join(__dirname, 'i18n'))
.then(() => di(
moduleName,
extendDi(moduleName, config.di),
{
module: app
},
'app',
[],
'modules/' + moduleName)
)
.then(scope => alias(scope, scope.settings.get(moduleName + '.di-alias')))
.then((scope) => {
const staticOptions = isProduction ? scope.settings.get('staticOptions') : undefined;
const themePath = scope.settings.get(moduleName + '.theme') || config.theme || 'default';
try {
theme(
app,
moduleName,
__dirname,
themePath,
scope.sysLog,
staticOptions
);
app.locals.pageTitle = scope.settings.get(moduleName + '.pageTitle')
|| scope.settings.get('pageTitle')
|| `ION ${config.sysTitle}`;
app.locals.pageEndContent = scope.settings.get(moduleName +'.pageEndContent') || scope.settings.get('pageEndContent') || '';
extViews(app, scope.settings.get(moduleName + '.templates'));
let statics = staticRouter(scope.settings.get(moduleName + '.statics'), staticOptions);
if (statics) {
app.use('/' + moduleName, statics);
}
scope.auth.bindAuth(app, moduleName, {auth: false});
app.use('/' + moduleName, sysMenuCheck(scope, app, moduleName));
app.use('/' + moduleName, router);
} catch (err) {
return Promise.reject(err);
}
return Promise.resolve();
}
);
};