-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
138 lines (127 loc) · 3.29 KB
/
main.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
const express = require("express");
const fetch = require("node-fetch");
const app = express();
const port = 3010;
const cords = [
[{ x: 0, y: 0 }, { x: 0, y: 16 }, { x: 0, y: 24 }, { x: 0, y: 32 }],
[
{ x: 8, y: 0 },
{ x: 8, y: 8 },
{ x: 16, y: 8 },
{ x: 8, y: 16 },
{ x: 8, y: 24 },
{ x: 16, y: 24 },
{ x: 24, y: 24 }
],
[
{ x: 32, y: 8 },
{ x: 40, y: 8 },
{ x: 48, y: 8 },
{ x: 32, y: 16 },
{ x: 48, y: 16 },
{ x: 32, y: 24 },
{ x: 40, y: 24 },
{ x: 48, y: 24 }
],
[
{ x: 56, y: 8 },
{ x: 64, y: 8 },
{ x: 72, y: 8 },
{ x: 56, y: 16 },
{ x: 56, y: 24 }
],
[
{ x: 88, y: 8 },
{ x: 96, y: 8 },
{ x: 80, y: 16 },
{ x: 96, y: 16 },
{ x: 80, y: 24 },
{ x: 88, y: 24 },
{ x: 96, y: 24 }
],
[
{ x: 104, y: 0 },
{ x: 104, y: 8 },
{ x: 112, y: 8 },
{ x: 104, y: 16 },
{ x: 104, y: 24 },
{ x: 112, y: 24 },
{ x: 120, y: 24 }
],
[
{ x: 128, y: 8 },
{ x: 136, y: 8 },
{ x: 144, y: 8 },
{ x: 128, y: 16 },
{ x: 144, y: 16 },
{ x: 128, y: 24 },
{ x: 136, y: 24 }
]
];
async function getLiveLogo() {
const response = await fetch("https://logo-api.g2.iterate.no/logo");
const body = await response.json();
return body;
}
function getCoordinates(index) {
return {
x: index % 8,
y: Math.floor(index / 8)
};
}
getLiveLogo().then(logoFromApi => {
const { logo } = logoFromApi;
const svg = `<svg width="152" height="32">
${logo.map((char, charIndex) =>
char.map((panel, panelIndex) =>
panel.map((pixel, pixelIndex) => {
const { x, y } = getCoordinates(pixelIndex);
return `<rect width="1" height="1" style="fill:${pixel};" x=${cords[
charIndex
][panelIndex].x + x} y=${cords[charIndex][panelIndex].y + y} />`;
})
)
)}
</svg>`;
return svg;
});
app.get('/health', (req, res) => res.send("I'm alive!"))
app.get("/logo.svg", (req, res) => {
getLiveLogo().then(logoFromApi => {
const { logo } = logoFromApi;
const svg = `<svg width="152" height="32" viewBox="0 0 152 32" version="1.1" xmlns="http://www.w3.org/2000/svg">
${logo.map((char, charIndex) =>
char.map((panel, panelIndex) =>
panel.map((pixel, pixelIndex) => {
const { x, y } = getCoordinates(pixelIndex);
return `<rect width="1" height="1" style="fill:${pixel};" x=${cords[
charIndex
][panelIndex].x + x} y=${cords[charIndex][panelIndex].y + y} />`;
})
)
)}
</svg>`;
res.send(svg);
});
});
app.get("/:width/:height/logo.svg", (req, res) => {
getLiveLogo().then(logoFromApi => {
const { logo } = logoFromApi;
const svg = `<svg width=${req.params.width} height=${
req.params.height
} viewBox="0 0 152 32" version="1.1" xmlns="http://www.w3.org/2000/svg">
${logo.map((char, charIndex) =>
char.map((panel, panelIndex) =>
panel.map((pixel, pixelIndex) => {
const { x, y } = getCoordinates(pixelIndex);
return `<rect width="1" height="1" style="fill:${pixel};" x=${cords[
charIndex
][panelIndex].x + x} y=${cords[charIndex][panelIndex].y + y} />`;
})
)
)}
</svg>`;
res.send(svg);
});
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));