-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
142 lines (123 loc) · 4.09 KB
/
app.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
*
* K2HR3 Web Application
*
* Copyright 2017 Yahoo Japan Corporation.
*
* K2HR3 is K2hdkc based Resource and Roles and policy Rules, gathers
* common management information for the cloud.
* K2HR3 can dynamically manage information as "who", "what", "operate".
* These are stored as roles, resources, policies in K2hdkc, and the
* client system can dynamically read and modify these information.
*
* For the full copyright and license information, please view
* the license file that was distributed with this source code.
*
* AUTHOR: Takeshi Nakatani
* CREATE: Tue Aug 15 2017
* REVISION:
*
*/
'use strict';
//
// Require
//
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon'); // eslint-disable-line no-unused-vars
var logger = require('morgan');
var cookieParser= require('cookie-parser');
var bodyParser = require('body-parser');
var r3Conf = require('./routes/lib/libr3appconfig').r3AppConfig;
var appConf = new r3Conf();
var r3util = require('./routes/lib/libr3util');
//
// Router
//
var index = require('./routes/index');
var tokens = require('./routes/tokens');
//
// The application
//
var app = express();
//
// View engine
//
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
//
// Setup icon
//
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); // uncomment after placing your favicon in /public
//
// Setup Log
//
app.use(logger(appConf.getAccessLogFormat(), appConf.getMorganLoggerOption(__dirname)));
//
// Setup others
//
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/', index);
app.use('/public/js', express.static(__dirname + '/public/js')); // for bundle.js
app.use('/public/tokens', tokens); // for user token
app.use('/status.html', express.static(__dirname + '/public/status.html'));
//
// Setup extension router
//
var cfgExtRouter= appConf.getExtRouter();
if(r3util.isSafeEntity(cfgExtRouter)){
var routerObject = {};
Object.keys(cfgExtRouter).forEach(function(routername){
// check name/path
if(r3util.isSafeString(cfgExtRouter[routername].name) && r3util.isSafeString(cfgExtRouter[routername].path)){
var routerTypeName = cfgExtRouter[routername].name;
if(!r3util.isSafeEntity(routerObject[routerTypeName])){
routerObject[routerTypeName] = require('./routes/' + cfgExtRouter[routername].name);
}
// check setConfig function
if(r3util.isSafeEntity(routerObject[routerTypeName].setConfig) && 'function' == typeof routerObject[routerTypeName].setConfig){
var routerConfig = r3util.isSafeEntity(cfgExtRouter[routername].config) ? cfgExtRouter[routername].config : null;
if(!routerObject[routerTypeName].setConfig(routerConfig, routername)){
console.error('failed to set configuration for extension router(name=' + JSON.stringify(cfgExtRouter[routername].name) + ', path=' + JSON.stringify(cfgExtRouter[routername].path) + ').');
}
}
// set router
app.use(cfgExtRouter[routername].path, routerObject[routerTypeName].router);
console.log('success set extension router(name=' + JSON.stringify(cfgExtRouter[routername].name) + ', path=' + JSON.stringify(cfgExtRouter[routername].path) + ').');
}else{
console.error('something wrong extrouter configration(name=' + JSON.stringify(cfgExtRouter[routername].name) + ', path=' + JSON.stringify(cfgExtRouter[routername].path) + ').');
}
});
}
//
// Last handling after all routing
//
app.use(function(req, res, next)
{
var err = new Error('Not Found');
err.status = 404;
next(err);
});
//
// Error handler
//
app.use(function(err, req, res, next) // eslint-disable-line no-unused-vars
{
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = ('development' === req.app.get('env')) ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noexpandtab sw=4 ts=4 fdm=marker
* vim<600: noexpandtab sw=4 ts=4
*/