-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlaunch.js
84 lines (68 loc) · 2.41 KB
/
launch.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
var request = require('request');
var path = require('path');
var express = require('express');
var app = express();
var fs = require('fs');
var ramlStore = require('./store/fileserver');
var spawn = require('child_process').spawn;
const PORT = 10500;
module.exports = function(folder) {
console.log('opening %s', folder);
app.use('/raml-store', ramlStore(path.join(process.cwd(), folder)));
var server = app.listen(9000, function () {
console.log('Open http://localhost:%d/raml-store/ to browse api-designer', server.address().port);
});
launch_webapp_once(PORT, function() {
return spawn('open', [("http://localhost:" + PORT)]);
});
}
var starts_with = function(str, search) {
return 0 === str.indexOf(search);
};
var file_or_url_to_absolute = function(file) {
if (starts_with(file, 'http:') || starts_with(file, 'https:')) {
return file;
} else {
return path.resolve(process.cwd(), file);
}
};
var launch_webapp = function(port, cb) {
app.get('/', function(req, res) {
console.log(path.join(__dirname, 'html/api-designer-fs.html'));
fs.readFile(path.join(__dirname, 'html/api-designer-fs.html'), 'utf-8', function(err, content) {
return res.send(content);
});
});
app.use('/api-designer', express.static(path.join(__dirname, 'node_modules/api-designer/dist')));
app.use('/', express.static('/'));
// Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
return app.listen(port, cb);
};
var webapp_is_listening = function(port, cb) {
console.log(port);
return request("http://localhost:" + port + "/~raml-is", function(e, r, b) {
return cb(null, b === 'awesome');
});
};
var launch_webapp_once = function(port, cb) {
return webapp_is_listening(port, function(e, r) {
if (r) {
return cb();
} else {
return launch_webapp(port, cb);
}
});
};