-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
64 lines (56 loc) · 2.03 KB
/
main.ts
File metadata and controls
64 lines (56 loc) · 2.03 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
import { generate } from "./badge/badge.ts";
import { readSVG } from "./icon/icon.ts";
import { Application, Router } from "./deps.ts";
const app = new Application();
const router = new Router();
router
.get("/", async ctx => {
const defaultQuery = '{"fields":[{"content":"Tai","color":0,"width":20},{"content":"Invalid Badge","color":6,"width":80}],"style":0}';
const query = ctx.request.url.searchParams.get("json") || defaultQuery;
// Set response headers
ctx.response.headers.set("Access-Control-Allow-Origin", "*");
ctx.response.headers.set("Cache-Control", "no-store");
ctx.response.type = "image/svg+xml";
// Generate badge or invalid badge and return
try {
const json = JSON.parse(query);
ctx.response.body = await generate(json);
}
catch (e) {
const json = JSON.parse(defaultQuery);
ctx.response.body = await generate(json);
}
})
.get("/:userId/:project/:badgeId", async ctx => {
const { userId, project, badgeId } = ctx.params;
const apiBase = Deno.env.get("API_BASE") || "http://localhost:8002";
const data = await fetch(`${apiBase}/${userId}/${project}/${badgeId}/json`)
.then(res => res.json());
if (data) {
const badge = await generate(data);
ctx.response.body = badge;
ctx.response.headers.set("Access-Control-Allow-Origin", "*");
ctx.response.headers.set("Cache-Control", "no-store");
ctx.response.type = "image/svg+xml";
}
else ctx.throw(404);
})
.get("/icon/:iconURI", async ctx => {
const { iconURI } = ctx.params;
const svg = await readSVG(iconURI);
if (!svg) {
ctx.response.status = 404;
ctx.response.body = "Not found";
console.error("[ERR] Icon not found:", iconURI);
return;
}
ctx.response.body = svg;
ctx.response.headers.set("Access-Control-Allow-Origin", "*");
ctx.response.headers.set("Cache-Control", "no-store");
ctx.response.type = "image/svg+xml";
});
app.use(router.routes());
app.use(router.allowedMethods());
const port = Deno.env.get("PORT") || "9002";
console.log(`http://localhost:${port}`);
app.listen({ port: parseInt(port) });