This repository has been archived by the owner on Feb 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
176 lines (142 loc) · 3.7 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const express = require('express');
const fs = require('fs');
const openapiJSDoc = require('openapi-jsdoc');
const swaggerUiAssetPath = require("swagger-ui-dist").absolutePath();
const app = express();
// General
app.use((req, res, next) => {
res.set('Access-Control-Allow-Origin', '*');
next();
});
// Load Functions
const walk = (dir) => {
let results = [];
fs.readdirSync(dir).forEach((file) => {
file = dir + '/' + file;
const stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
/* Recurse into a subdirectory */
results = results.concat(walk(file));
} else {
/* Is a file */
results.push(file);
}
});
return results;
}
const modules = walk('./func');
const functions = {};
modules.forEach(module => {
functions[module.replace('./func/', '').replace('.js', '')] = require(module);
});
// Documentation
const api = openapiJSDoc({
definition: {
// info object, see https://swagger.io/specification/#infoObject
info: {
title: 'Moonhorse', // required
version: '1.0.0', // required
description: 'The Moonhorse API'
}
},
apis: ['./components.yaml', './index.js', './func/**/*.js']
});
app.get('/', (req, res) => {
res.redirect('/swaggerui/?url=/api-docs.json');
});
app.use('/swaggerui/', express.static(swaggerUiAssetPath));
app.get('/api-docs.json', (req, res) => {
res.setHeader('Content-Type', 'application/json')
res.send(api)
});
// Function Helpers
const stack = (req, res, next) => {
// Populate stack
req.stack = req.query.a;
if (!req.stack) {
res.stack = [];
}
if (!Array.isArray(req.stack)) {
req.stack = [ req.stack ];
}
next();
};
const types = (req, res, next) => {
req.stack = req.stack.map(val => {
// To number
if (!isNaN(+val)) {
return +val;
}
// To bool/null/undefined
if (val == 'false') {
return false;
} else if (val == 'true') {
return true;
} else if (val == 'null') {
return null;
} else if (val == 'undefined') {
return undefined;
}
return val;
});
next();
};
const skip = (req, res, next) => {
if (req.query.s && req.query.s > 0) {
req.query.s--;
nextEndpoint(req, res);
return;
} else {
next();
}
};
const nextEndpoint = (req, res) => {
// Call next
if (!req.query.next) {
res.status(200).send({
_stack: req.stack,
result: req.stack[req.stack.length - 1],
});
return;
}
const stackstring = req.stack.map(entry => `a=${encodeURIComponent(entry)}`).join('&');
const parts = req.query.next.split('?');
let next = '';
if (parts.length > 1) {
if (req.query.s) {
parts[1] = parts[1].replace(/[\&]?a=[.]*/, '');
}
next = `${parts[0]}?${stackstring}&${parts[1]}${req.query.s ? `&s=${req.query.s}` : ''}`;
} else {
next = `${parts[0]}?${stackstring}${req.query.s ? `&s=${req.query.s}` : ''}`;
}
res.redirect(next);
};
// Conditionals
app.get('/then', stack, types, skip, (req, res, next) => {
const skipCount = parseInt(req.query.skip, 10);
if (req.stack.pop()) {
req.query.s = skipCount;
}
next();
}, nextEndpoint);
app.get('/skip', stack, types, skip, (req, res, next) => {
const skipCount = parseInt(req.query.skip, 10);
req.query.s = skipCount;
next();
}, nextEndpoint);
// Function Call
app.get('/*', stack, types, skip, (req, res, next) => {
const functionName = req.params[0];
// Call function
if (!functions[functionName]) {
res.status(404).send(`function ${functionName} not found`);
return;
}
functions[functionName](req.stack);
next();
}, nextEndpoint);
// Install Handler
app.listen(process.env.PORT, (err) => {
err ? console.error(err) : console.log(`listening on :${process.env.PORT}`);
});