This repository was archived by the owner on May 19, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
126 lines (115 loc) · 3.18 KB
/
Copy pathindex.js
File metadata and controls
126 lines (115 loc) · 3.18 KB
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
var path = require('path');
var express = require('express');
var compression = require('compression');
var bodyParser = require('body-parser');
var yaml = require('js-yaml');
var shins = require('shins/index.js');
var shinsDir = shins.srcDir();
var widdershins = require('widdershins');
var fetch = require('./fetch.js');
function parseJSON(obj) {
try {
return JSON.parse(obj);
}
catch (ex) {
return null;
}
}
function parseYAML(obj) {
try {
return yaml.safeLoad(obj);
}
catch (ex) {
return null;
}
}
var app = express();
app.use(compression());
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.sendCustomMessage = function(message) {
res.send('<html><h1>' + message);
};
next();
});
app.options('*',function(req,res,next){
res.set('Access-Control-Allow-Origin',req.headers['origin']||'*');
res.set('Access-Control-Allow-Methods','GET, POST, HEAD, OPTIONS');
res.set('Access-Control-Allow-Headers',req.headers['access-control-request-headers']||
'Content-Type, Authorization, Content-Length, X-Requested-With');
res.sendStatus(204);
});
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'index.html'))
});
app.get('/shins', function(req, res) {
if (!req.query.url) {
return res.sendCustomMessage('Please supply a URL parameter');
}
fetch.get(req.query.url, {}, {}, function (err, resp, obj) {
if (err || !obj) {
return res.status(500).sendCustomMessage('Could not fetch the given URL');
}
shins.render(obj, function(err, str){
res.send(str);
});
});
});
app.post('/openapi', function(req, res) {
var obj = req.body;
res.set('Access-Control-Allow-Origin','*');
if (typeof obj !== 'object') {
console.log(typeof obj);
return res.status(500).sendCustomMessage('Could not parse the request body');
}
var options = {};
var md = widdershins.convert(obj, options, function(err, md){
if (err) console.log(JSON.stringify(err));
if (typeof req.query.raw !== 'undefined') {
res.set('Content-Type','text/plain');
res.send(md);
}
else {
shins.render(md, function (err, str) {
res.set('Content-Type','text/html');
res.send(str);
});
}
});
});
app.get('/openapi', function(req, res) {
if (!req.query.url) {
return res.sendCustomMessage('Please supply a URL parameter');
}
fetch.get(req.query.url, {}, {}, function (err, resp, data) {
if (err) {
return res.status(404).sendCustomMessage('Could not fetch the given URL');
}
var obj = parseJSON(data) || parseYAML(data);
if (!obj) {
return res.status(404).sendCustomMessage('Could not parse the data');
}
var options = {};
options.loadedFrom = req.query.url;
res.set('Access-Control-Allow-Origin','*');
widdershins.convert(obj, options, function(err, md) {
if (typeof req.query.raw !== 'undefined') {
res.send(md);
}
else {
shins.render(md, function (err, str) {
res.send(str);
});
}
});
});
});
app.use("/", express.static(shinsDir));
var myport = process.env.PORT || 5678;
if (process.argv.length > 2)
myport = process.argv[2];
var server = app.listen(myport, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Shinola server listening at http://%s:%s', host, port);
});