-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
327 lines (290 loc) · 6.65 KB
/
api.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
const path = require("path");
const fs = require("fs");
module.exports = async function (waw) {
const app = {};
const page = {},
pageChecks = [];
const method = {},
methodChecks = [];
const subdomains = [];
const customCheck = (url, router = "") => {
const _url = [],
local_url = ((router || "") + url).split("/");
for (let i = 0; i < local_url.length; i++) {
_url.push(local_url[i].startsWith(":") ? false : local_url[i]);
}
return { url, _url, router };
};
const doCheck = (_url, checks, handler, req, res, next, domain) => {
for (const check of checks) {
if (_url.length === check._url.length) {
let correct = true;
for (let i = 0; i < check._url.length; i++) {
if (check._url[i] && check._url[i] !== _url[i]) {
correct = false;
break;
}
}
if (correct) {
req.params = {};
for (let i = 0; i < check._url.length; i++) {
if (!check._url[i]) {
if (
((check.router || "") + check.url).split("/")[
i
] ||
_url[i]
) {
req.params[
((check.router || "") + check.url)
.split("/")
[i].replace(":", "")
] = _url[i];
}
}
}
if (
handler[
req.method.toLowerCase() +
domain +
check.router +
check.url
]
) {
handler[
req.method.toLowerCase() +
domain +
check.router +
check.url
](req, res, next);
} else if (
handler[
req.method.toLowerCase() + check.router + check.url
]
) {
handler[
req.method.toLowerCase() + check.router + check.url
](req, res, next);
} else if (handler[domain + check.router + check.url]) {
handler[domain + check.router + check.url](
req,
res,
next
);
} else if (handler[check.router + check.url]) {
handler[check.router + check.url](req, res, next);
}
return true;
}
}
}
return false;
};
const appManagement = (options) => {
if (!options.domain || !options.app) return;
waw.serve(options.app, {
domain: options.domain,
});
app[options.domain] = options.app;
};
const templateManagement = (options) => {
if (
!options.template ||
typeof options.template !== "object" ||
!options.template.path ||
!options.template.prefix ||
!options.template.pages
) {
return;
}
waw.serve(options.template.path, {
prefix: options.template.prefix,
});
if (typeof options.template.pages === "string") {
options.template.pages = options.template.pages.split(" ");
}
for (let pageName of options.template.pages) {
waw.build(options.template.path, pageName);
if (pageName === "index") {
pageName = "/";
}
if (!page[options.domain + "/" + pageName]) {
page[options.domain + "/" + pageName] = (req, res) => {
res.send(
waw.render(
path.join(
options.template.path,
"dist",
pageName + ".html"
),
waw.readJson(
path.join(
options.template.path,
"pages",
pageName,
"page.json"
)
)
)
);
};
}
}
};
const pageManagement = (options) => {
if (typeof options.page === "object" && !Array.isArray(options.page)) {
for (const url in options.page) {
if (url.includes("/:")) {
pageChecks.push(customCheck(url));
}
if (typeof options.page[url] === "function") {
page[options.domain + url] = options.page[url];
}
}
}
};
waw.apiCleanPage = (domain) => {
for (const key in page) {
if (key.startsWith(domain)) {
delete page[key];
}
}
}
const httpManagement = (options) => {
const router = (options.domain || "") + (options.router || "");
for (const methodName of ["get", "post", "put", "patch", "delete"]) {
if (
typeof options[methodName] === "object" &&
!Array.isArray(options[methodName])
) {
for (const url in options[methodName]) {
if (url.includes("/:")) {
methodChecks.push(customCheck(url, options.router));
}
if (typeof options[methodName][url] === "function") {
method[methodName + router + url] =
options[methodName][url];
}
}
}
}
};
waw.api = (options) => {
options.domain = options.domain || "";
if (options.subdomain && !subdomains.includes(options.domain)) {
subdomains.push(options.domain);
}
appManagement(options);
templateManagement(options);
pageManagement(options);
httpManagement(options);
};
await waw.wait(500);
const getHost = (host) => {
for (const domain of subdomains) {
if (host && host.endsWith(domain)) {
return domain;
}
}
return host;
};
waw.use((req, res, next) => {
const host = getHost(req.get("host"));
if (
methodChecks.length &&
doCheck(
req.originalUrl.split("/"),
methodChecks,
method,
req,
res,
next,
host
)
) {
return;
}
if (
typeof method[req.method.toLowerCase() + host + req.originalUrl] ===
"function"
) {
return method[req.method.toLowerCase() + host + req.originalUrl](
req,
res,
next
);
} else if (
typeof method[req.method.toLowerCase() + req.originalUrl] ===
"function"
) {
return method[req.method.toLowerCase() + req.originalUrl](
req,
res,
next
);
}
if (req.originalUrl.startsWith("/api/")) {
return next();
}
if (
pageChecks.length &&
doCheck(
req.originalUrl.split("/"),
pageChecks,
page,
req,
res,
next,
host
)
) {
return;
}
const url = req.originalUrl.split('?')[0];
if (typeof page[host + url] === "function") {
return page[host + url](req, res, next);
} else if (typeof page[url] === "function") {
return page[url](req, res, next);
}
if (app[req.get("host")]) {
if (
req.originalUrl !== "/" &&
fs.existsSync(path.join(app[req.get("host")], req.originalUrl))
) {
res.sendFile(path.join(app[req.get("host")], req.originalUrl));
} else {
res.sendFile(path.join(app[req.get("host")], "index.html"));
}
} else if (typeof page[host + "*"] === "function") {
return page[host + "*"](req, res, next);
} else if (typeof page["*"] === "function") {
return page["*"](req, res, next);
} else {
if (req.get("host") && !req.get("host").includes("localhost")) {
console.log(
"page is not configured, url: ",
req.originalUrl,
" and host: ",
req.get("host")
);
}
next();
}
});
/* Sample */
// waw.api({
// router: '/api/product',
// domain: 'webart.work',
// app: 'path',
// page: {
// '/': () => {}
// },
// post: {
// '/get': () => {}
// },
// get: {},
// put: {},
// patch: {},
// delete: {}
// });
};