-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
58 lines (47 loc) · 1.44 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
import express from "express";
import { RaySo, InvalidParameterException } from "rayso";
const app = express();
app.use(express.json());
const raySoConfig = (params) => ({
title: params.title,
theme: params.theme,
padding: params.padding,
language: params.language,
background: params.background,
darkMode: params.darkMode,
});
const errorResponse = (res, status, message) => {
res.status(status).json({ error: message });
};
const handleRaySo = async (req, res, codeExtractor) => {
try {
const code = codeExtractor(req);
if (!code) {
errorResponse(res, 400, "Code parameter is missing.");
return;
}
const raySo = new RaySo(raySoConfig(req.query || req.body));
const response = await raySo.cook(code);
res.set("Content-Type", "image/jpeg");
res.send(response);
} catch (error) {
if (error instanceof InvalidParameterException) {
errorResponse(res, 400, error.message);
} else {
errorResponse(res, 500, "An Internal Server Error.");
}
}
};
app.get("/", (req, res) => {
res.end("Hello World!");
});
app.get("/api", (req, res) => {
handleRaySo(req, res, (req) => req.query.code);
});
app.post("/api", (req, res) => {
handleRaySo(req, res, (req) => req.body.code);
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
});