-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
112 lines (87 loc) · 3.04 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
'use strict';
var path = require('path');
var express = require('express');
var app = express();
var fs = require('fs-extra');
var middleware = require('swagger-express-middleware');
var SwaggerParser = require('swagger-parser');
var specfile = path.join(__dirname, 'specs/swagger.yaml');
var mtime;
process.on('SIGINT', function() {
console.log('Caught Ctrl+C...');
process.exit();
}); // Ctrl+C
process.on('SIGTERM', function() {
console.log('Caught kill...');
process.exit();
}); // docker stop
if (!fs.existsSync(specfile)) {
fs.copySync(__dirname + '/swagger.yaml.min', specfile);
}
mtime = fs.statSync(specfile).mtime.getTime();
//Using fixed default.json
app.use('/editor/config/defaults.json', express.static(path.join(__dirname, 'config/defaults.json')));
//Setting backend
app.use('/editor/spec', function(req, res){
if (req.method === 'GET' || req.method === 'HEAD') {
// Serve the file
res.sendFile(specfile);
} else if (req.method === 'PUT') {
// Write file to disk
var stream = fs.createWriteStream(specfile);
stream.on('finish', function () {
res.end('ok');
});
req.pipe(stream);
} else {
// Method not allowed
res.writeHead(405, {
'Allow': 'GET, HEAD, PUT',
'Content-Length': '0'
});
res.end();
}
});
app.use('/editor', express.static(path.join(__dirname, 'node_modules/swagger-editor')));
app.use(express.static(path.join(__dirname, 'specs/')));
if (fs.existsSync('./extra-routes.js')) {
require('./extra-routes')(app);
}
middleware(specfile, app, function(err, middleware, api) {
// Add all the Swagger Express Middleware, or just the ones you need.
// NOTE: Some of these accept optional options (omitted here for brevity)
app.use(
middleware.metadata(),
middleware.CORS(),
middleware.files(),
middleware.parseRequest(),
middleware.validateRequest(),
middleware.mock()
);
fs.watch(specfile, {}, function(eventType){
fs.stat(specfile, function(err, stats){
console.log(stats.mtime+'/'+mtime+"\n");
if(stats.mtime.getTime() != mtime){
mtime = stats.mtime.getTime();
console.log(`event type is: ${eventType}`);
SwaggerParser.validate(specfile)
.then(function (api) {
middleware.init(specfile, function(err){
if (err) {
console.log(err);
}
})
})
.catch(function(err) {
console.error('Onoes! The API is invalid. ' + err.message);
});
}
});
});
var server = app.listen(process.env.PORT || 8080, function () {
var host = server.address().address;
var port = server.address().port;
console.log("Example app listening at http://%s:%s", host, port);
});
});
module.exports = app;