-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
146 lines (134 loc) · 4.43 KB
/
index.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
143
144
145
146
console.log('dir: ' + __dirname);
var http = require('http'),
fs = require('fs'),
path = require('path'),
express = require('express'),
app = express(),
hbs = require('hbs'),
srcDir = path.join(__dirname, 'script/com/infrared5/os/madmin'),
controller = require(path.join(srcDir, 'controller')),
service = require(path.join(srcDir, '/service')),
winston = require('winston'),
templates = path.join(__dirname, 'public/template'),
logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
prettyPrint: true,
colorize: true,
timestamp: true
})
]
});
// body parser.
app.use( express.bodyParser() );
// ## CORS middleware
// see: http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express-nodejs
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}
};
app.use(allowCrossDomain);
// Default
app.get( '/', controller.admin);
// Define public files for urls in templates.
app.use( express.static(__dirname + '/public') );
app.use( express.errorHandler({ dumpExceptions: true, showStack: true }) );
// Handlebars Templating.
var cachedPartials = {};
var generatePartialTemplate = function(filename) {
return function(options) {
var html, template;
if(cachedPartials.hasOwnProperty(filename)) {
template = cachedPartials[filename];
}
else {
try {
html = fs.readFileSync(filename, 'UTF8');
template = cachedPartials[filename] = hbs.compile(html);
}
catch(error) {
logger.error('Could not load partial from ' + filename + '. ' + error.message);
html = fs.readFileSync(templates + '/unknown-error.ejs');
template = hbs.compile(html);
}
}
// template = hbs.compile(html);
return template(this);
};
};
app.set('views', __dirname + '/public/template');
app.set('view engine', 'hbs');
hbs.registerHelper('route_console', generatePartialTemplate(templates + '/route-console.hbs'));
hbs.registerHelper('uneditable_console', generatePartialTemplate(templates + '/uneditable-console.hbs'));
hbs.registerHelper('editable_console', generatePartialTemplate(templates + '/editable-console.hbs'));
hbs.registerHelper('parameter_form', generatePartialTemplate(templates + '/parameter-form.hbs'));
hbs.registerHelper('parameter_item', generatePartialTemplate(templates + '/parameter-item.hbs'));
/**
* Helper to stringify object
* @param {Object} value Route model object
* @return {String}
*/
hbs.registerHelper('stringify', function(value) {
return JSON.stringify(value);
});
/**
* Helper to define a unique identifier either based on model or time of request.
* @param {Object} value Route model object.
* @return {String} Unique string identifier.
*/
hbs.registerHelper('index-input', function(value) {
var id;
if(value.hasOwnProperty('id') &&
typeof value.id === 'string' &&
value.id.length > 0) {
id = value.id;
}
else {
id = (new Date()).getTime().toString();
}
return id;
});
/**
* Helper to return length of provided value assumed as an Array. Returns 0 is value is not an Array.
* @param {Array} value
* @return {Number}
*/
hbs.registerHelper('iflength', function(value) {
return Array.isArray(value) && value.length > 0;
});
// RESTful paths
app.get( '/generate', controller.generate );
app.get( '/admin', controller.admin );
app.post( '/admin', controller.create );
app.get( '/admin/:entryId', controller.read );
app.post( '/admin/:entryId', controller.update );
app.delete( '/admin/:entryId', controller.remove );
// Fault >
app.use( function(err, req, res, next){
logger.error(err.stack);
res.statusCode = 500;
res.render('500');
});
app.use( function(req, res, next){
res.statusCode = 404;
res.render('404');
});
var madmin = module.exports;
madmin.init = function(port, json) {
service.setApplication(app);
service.loadAPI(json).then( function() {
app.listen(port);
logger.info("madmin server running on port " + port + " in " + app.settings.env + " mode");
},
function(error) {
logger.error(error.toString());
});
};