-
Notifications
You must be signed in to change notification settings - Fork 13
/
iotronic-docs-gen.js
280 lines (212 loc) · 8.28 KB
/
iotronic-docs-gen.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//###############################################################################
//##
//# Copyright (C) 2017 Nicola Peditto
//##
//# Licensed under the Apache License, Version 2.0 (the "License");
//# you may not use this file except in compliance with the License.
//# You may obtain a copy of the License at
//##
//# http://www.apache.org/licenses/LICENSE-2.0
//##
//# Unless required by applicable law or agreed to in writing, software
//# distributed under the License is distributed on an "AS IS" BASIS,
//# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//# See the License for the specific language governing permissions and
//# limitations under the License.
//##
//###############################################################################
var swaggerJSDoc = require('swagger-jsdoc');
var fs = require('fs');
nconf = require('nconf');
var optimist = require('optimist').usage("IoTronic API documentation generator.")
.string("iotronic").alias('i', "iotronic").describe('i', 'IoTronic suorce code path.')
.boolean("embedded").alias('e', "embedded").describe('e', 'true | false to spawn API webpage documentation.')
.string("port").alias('p', "port").describe('p', '[only with --embedded=true] Listening port.')
.string("web").alias('w', "web").describe('w', 'Web server path: where will be created the swagger json file "iotronic-swagger.json".');
var argv = optimist.argv;
/**
* @swagger
* definitions:
* IoTronicResponse:
* properties:
* result:
* type: string
* description: "possible results: [ SUCCESS | ERROR | WARNING ]"
* message:
* type: object
* description: "API return messagge"
*/
var genApiDocumentation = function (){
var swaggerJSDoc = require('swagger-jsdoc');
console.log("[API-DOCS] - Starting doc generation...");
if(port == undefined)
var swg_host = IoTronic_IP;
else
var swg_host = IoTronic_IP+':'+port;
// swagger definition
var swaggerDefinition = {
info: {
title: 'IoTronic API',
version: '2.1.0',
description: 'IoTronic-standalone API by Stack4Things.'
},
//host: swg_host,
basePath: '/',
licence:{
name: 'Apache v2',
url: 'https://www.apache.org/licenses/LICENSE-2.0'
},
//schemes:["http", "https"],
securityDefinitions: {
jwt: {
type: 'apiKey',
name: 'X-Auth-Token',
in: 'header'
}
},
security: [
{ jwt: [] }
]
};
// options for the swagger docs
var options = {
// import swaggerDefinitions
swaggerDefinition: swaggerDefinition,
// path to the API docs
apis: [
IOTRONIC_CODE +'docs/iotronic-docs-gen.js',
IOTRONIC_CODE +'lib/management/mng_auth.js',
IOTRONIC_CODE +'lib/management/mng_board.js',
IOTRONIC_CODE +'lib/management/mng_user.js',
IOTRONIC_CODE +'lib/management/mng_layout.js',
IOTRONIC_CODE +'lib/management/mng_project.js',
IOTRONIC_CODE +'lib/management/mng_request.js',
IOTRONIC_CODE +'lib/modules/service_manager.js',
IOTRONIC_CODE +'lib/modules/vnet_iotronic_manager.js',
IOTRONIC_CODE +'lib/modules/plugin_manager.js',
IOTRONIC_CODE +'lib/modules/driver_manager.js',
IOTRONIC_CODE +'lib/modules/gpio_manager.js',
IOTRONIC_CODE +'lib/modules/vfs_manager.js'
]
};
// initialize swagger-jsdoc
var swaggerSpec = swaggerJSDoc(options);
if(embedded == true ){
exposeApiDocumentation(swaggerSpec);
}else{
if (argv.w == undefined)
swaggerJSONfile = docs_path + "/iotronic-swagger.json";
else
swaggerJSONfile = argv.w + "/iotronic-swagger.json";
fs.writeFile(swaggerJSONfile, JSON.stringify(swaggerSpec), function (err) {
if (err) {
console.log("[API-DOCS] - Error writing iotronic-swagger.json: " + err);
} else {
console.log("[API-DOCS] - iotronic-swagger.json successfully created!");
}
});
}
};
var exposeApiDocumentation = function (swaggerSpec){
console.log("[API-DOCS] - Exposing API documentaion...");
var express = require('express');
var bodyParser = require('body-parser');
var rest = express();
// serve swagger
rest.get('/v1/iotronic-swagger.json', function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.status(200).send(swaggerSpec);
});
rest.get('/', function (req, res) {
res.status(200).redirect(link_docs);
});
rest.get('/v1/', function (req, res) {
res.status(200).redirect(link_docs);
});
// swagger-ui web-api VERSION
rest.get('/v1/iotronic-api-docs', function(req, res) {
res.status(200).sendFile(docs_path+"/index.html");
});
rest.get('/v1/iotronic-api-docs/swagger-ui.css', function(req, res) {
res.status(200).sendFile(docs_path+"/swagger-ui.css");
});
rest.get('/v1/iotronic-api-docs/swagger-ui-bundle.js', function(req, res) {
res.status(200).sendFile(docs_path+"/swagger-ui-bundle.js");
});
rest.get('/v1/iotronic-api-docs/swagger-ui-standalone-preset.js', function(req, res) {
res.status(200).sendFile(docs_path+"/swagger-ui-standalone-preset.js");
});
// Configuring REST server
rest.use(bodyParser.json()); // support json encoded bodies
rest.use(bodyParser.urlencoded({extended: true})); // support encoded bodies
rest.all('/*', function(req, res, next) {
res.type('application/json');
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", 'GET, POST, DELETE, PATCH, PUT');
next();
});
// START REST SERVER
if (https_enable == "true"){
//HTTPS
var https = require('https');
var s4t_key = fs.readFileSync(https_key, 'utf8');
var s4t_cert = fs.readFileSync(https_cert, 'utf8');
var credentials = {
key: s4t_key,
cert: s4t_cert
};
https.createServer(credentials, rest).listen(port, function(){});
}else{
//HTTP
var http = require('http');
http.createServer(rest).listen(port, function(){});
}
console.log("[API-DOCS] - " + link_docs);
};
if (argv.i != undefined && argv.e != undefined){
IOTRONIC_CODE = argv.i;
embedded = argv.e;
port = argv.p;
if(embedded == true && port == undefined){
// Wrong options
return console.log(optimist.help());
}else {
// Load settings
try{
IOTRONIC_CFG = process.env.IOTRONIC_HOME + "/settings.json";
nconf.file({file: IOTRONIC_CFG});
// Set fronted IP address
public_ip = nconf.get('config:server:public_ip');
if (public_ip == "")
IoTronic_IP = utility.getIP(intr, 'IPv4');
else if(public_ip == "env"){
IoTronic_IP = process.env.IOTRONIC_PUB_IP;
}
else if(public_ip == undefined){
console.log("[SYSTEM] - Iotronic public IP not defined: " + public_ip);
process.exit();
}
else
IoTronic_IP = public_ip;
https_enable = nconf.get('config:server:https:enable');
https_key = nconf.get('config:server:https:key');
https_cert = nconf.get('config:server:https:cert');
docs_path = nconf.get('config:server:docs:embedded:path');
}
catch (err) {
console.log('[API-DOCS] - ' + err);
process.exit();
}
if (https_enable == "true"){
url_swagger = "https://" + IoTronic_IP + ":" + port + "/v1/iotronic-swagger.json";
link_docs = "https://" + IoTronic_IP + ":" + port + "/v1/iotronic-api-docs/";
}else{
url_swagger = "http://" + IoTronic_IP + ":" + port + "/v1/iotronic-swagger.json";
link_docs = "http://" + IoTronic_IP + ":" + port + "/v1/iotronic-api-docs/";
}
genApiDocumentation();
}
} else {
// Wrong options
return console.log(optimist.help());
}