forked from mogsie/SCXMLD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·152 lines (119 loc) · 4.04 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
147
148
149
150
151
152
#!/usr/bin/env node
'use strict';
var smaasApi = require('./app/api'),
_ = require('underscore'),
cors = require('cors');
function initExpress (opts, cb) {
opts = opts || {};
opts.port = opts.port || process.env.PORT || 8002;
var express = require('express'),
path = require('path'),
logger = require('morgan'),
app = express();
app.use(logger('dev'));
// buffer the body
app.use(function(req, res, next) {
req.body = '';
req.on('data', function(data) {
return req.body += data;
});
return req.on('end', next);
});
var websiteUrl = 'http://localhost:' + opts.port;
if (process.env.WEBSITE_URL) {
websiteUrl = process.env.WEBSITE_URL;
} else {
console.log('Missing "WEBSITE_URL" variable.');
}
app.use(cors({
origin: websiteUrl,
exposedHeaders: ['WWW-Authenticate', 'Location', 'X-Configuration']
}));
app.set('views', path.join(__dirname, './views'));
app.engine('html', require('ejs').renderFile);
app.use(express.static(path.join(__dirname, './public')));
opts.app = app;
initApi(opts, cb);
}
function initApi(opts, cb){
opts = opts || {};
opts.port = opts.port || process.env.PORT || 8002;
opts.basePath = opts.basePath || '/api/v1';
opts.dbProvider = opts.dbProvider || require('SCXMLD-simple-database-provider');
opts.simulationProvider = opts.simulationProvider || require('SCXMLD-simple-simulation-provider');
opts.middlewares = opts.middlewares || [];
if(!opts.app) {
return cb(new Error('Missing express app'));
}
var db = opts.dbProvider();
db.init(function (err) {
if(err) return cb(err);
console.log('Db initialized');
// Initialize the api
var simulation = opts.simulationProvider(db);
var api = smaasApi(simulation, db);
var smaasJSON = require('smaas-swagger-spec');
smaasJSON.host = process.env.SMAAS_HOST_URL || ('localhost' + ':' + opts.port);
smaasJSON.basePath = opts.basePath;
opts.app.get('/smaas.json', function (req, res) {
res.status(200).send(smaasJSON);
});
opts.app.get('/:username/smaas.json', function (req, res) {
var userSmaasJSON = _.clone(smaasJSON);
userSmaasJSON.basePath = userSmaasJSON.basePath.replace(':username', req.params.username);
res.status(200).send(userSmaasJSON);
});
opts.app.get(smaasJSON.basePath + '/:StateChartName/:InstanceId/_viz', api.instanceViz);
opts.app.get(smaasJSON.basePath + '/:StateChartName/_viz', api.statechartViz);
function methodNotImplementedMiddleware(req, res){
return res.send(501, { message: 'Not implemented' });
}
Object.keys(smaasJSON.paths).forEach(function(endpointPath){
var endpoint = smaasJSON.paths[endpointPath];
var actualPath = smaasJSON.basePath + endpointPath.replace(/{/g, ':').replace(/}/g, '');
Object.keys(endpoint).forEach(function(methodName){
var method = endpoint[methodName];
var handler = api[method.operationId] || methodNotImplementedMiddleware;
switch(methodName) {
case 'get': {
opts.app.get(actualPath, opts.middlewares, handler);
break;
}
case 'post': {
opts.app.post(actualPath, opts.middlewares, handler);
break;
}
case 'put': {
opts.app.put(actualPath, opts.middlewares, handler);
break;
}
case 'delete': {
opts.app.delete(actualPath, opts.middlewares, handler);
break;
}
default:{
console.log('Unsupported method name:', methodName);
}
}
});
});
opts.app.use(function(req, res) {
res.status(404).send('Can\'t find ' + req.path);
});
cb(null, opts);
});
}
if(require.main === module) {
initExpress({}, function (err, opts) {
console.log('Starting server on port:', opts.port);
if(err) throw err;
opts.app.listen(opts.port, function () {
console.log('Server started');
});
});
} else {
module.exports = {
initApi: initApi,
initExpress: initExpress
};
}