-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
97 lines (88 loc) · 2.69 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
import {
convertCopticText,
fontSupported,
jimkinMethodValid,
} from "@stmarkus/coptic-font-unicode-converter";
import express from "express";
// constants
const app = express();
const PORT = process.env.PORT || 3000;
const rhakotis =
"Rhacotis (romanized as Rhakotis) was the name for a city on the northern coast of Egypt at the site of Alexandria. Classical sources suggest Rhacotis as an older name for Alexandria before the arrival of Alexander the Great.";
app.get("/", (request, response) => {
response.send("");
});
app.get("/status", (request, response) => {
const status = {
Status: "Running",
Rhakotis: rhakotis,
};
response.send(status);
});
app.get("/convert2Unicode/", function (req, res) {
const params = req.query;
let checkResult = {};
let syntaxValidRequest = true;
// check URL query parameter
for (const qParam of ["text", "font", "jimkin"]) {
console.log(`Checking for URL query param parameter ${qParam}`);
checkResult = checkForRequiredQueryParam(params, qParam);
if (!checkResult.ok) {
res.statusMessage = checkResult.message;
res.status(checkResult.code).send();
syntaxValidRequest = false;
break;
}
}
// URL query parameters
const font = params.font;
const jimkin = params.jimkin;
const text = params.text;
// only continue if request syntax is valid
if (syntaxValidRequest) {
// check font
console.log(`Checking provided font ${font}`);
fontSupported(font).then((checked) => {
let passed = true;
if (!checked) {
passed = false;
res.statusMessage = "Provided font is NOT supported. Supported list";
res.status(400).send();
}
// check jimkin method
console.log(`Checking provided jimkin method ${jimkin}`);
const jMethod = jimkinMethodValid(jimkin);
if (!jMethod) {
passed = false;
res.statusMessage = "Provided Jimkin Combining Method is NOT valid";
res.status(400).send();
}
// convertCopticText
if (passed) {
const convertedText = convertCopticText(font, text, jimkin).then(
(text) => {
res.send({ text: text });
}
);
} else {
res.end();
}
});
}
});
function checkForRequiredQueryParam(receivedParams, expectedParam) {
if (receivedParams.hasOwnProperty(expectedParam)) {
return { ok: true, code: 200 };
} else {
return {
ok: false,
code: 400,
message: `Missing expected URL query Parameter ${expectedParam}`,
};
}
}
var server = app.listen(8081, function () {
var host = server.address().address;
var port = server.address().port;
console.log("App listening at http://%s:%s", host, port);
});