forked from Zirak/kitten-analysts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysts-server.js
96 lines (90 loc) · 2.94 KB
/
analysts-server.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
import * as http from 'node:http';
const CORS_ALLOW_ORIGIN = process.env.KITTENS_FRONTEND
|| 'https://kittensgame.com';
/*
metrics = {
at: 1674851419333,
resources: [{
name: 'catnip',
value: 44,
}],
...
}
*/
let metrics = null;
async function consumeMetrics(req) {
let buff = '';
for await (const d of req) {
buff += d.toString();
}
metrics = JSON.parse(buff);
console.log(metrics);
}
function writeMetrics(res) {
for (const [metricName, metric] of Object.entries(metrics)) {
// lel
if (metricName === 'at') {
continue;
}
for (const { name, value } of metric) {
const mname = `kittens_${metricName}_${name}`;
res.write(`#TYPE ${mname} gauge\n`);
res.write(`${mname} ${value} ${metrics.at}\n\n`);
}
}
res.end();
}
http.createServer((req, res) => {
console.log(`=> [${(new Date).toUTCString()}] ${req.method} ${req.url}`);
switch (req.url) {
case '/metrics': {
if (metrics) {
res.writeHead(200);
try {
writeMetrics(res);
} catch (e) {
console.error(e);
}
res.end();
} else {
res.writeHead(500);
res.end();
}
break;
}
case '/meow': {
res.writeHead(200, {
'Access-Control-Allow-Headers': 'content-type',
'Access-Control-Allow-Methods': 'PUT',
'Access-Control-Allow-Origin': CORS_ALLOW_ORIGIN,
});
consumeMetrics(req).catch((err) => {
console.error(err);
}).finally(() => res.end());
break;
}
default: {
res.writeHead(400);
// luv u gutenberg
res.end(`
Call me Ishmael. Some years ago—never mind how long precisely—having
little or no money in my purse, and nothing particular to interest me
on shore, I thought I would sail about a little and see the watery part
of the world. It is a way I have of driving off the spleen and
regulating the circulation. Whenever I find myself growing grim about
the mouth; whenever it is a damp, drizzly November in my soul; whenever
I find myself involuntarily pausing before coffin warehouses, and
bringing up the rear of every funeral I meet; and especially whenever
my hypos get such an upper hand of me, that it requires a strong moral
principle to prevent me from deliberately stepping into the street, and
methodically knocking people’s hats off—then, I account it high time to
get to sea as soon as I can. This is my substitute for pistol and ball.
With a philosophical flourish Cato throws himself upon his sword; I
quietly take to the ship. There is nothing surprising in this. If they
but knew it, almost all men in their degree, some time or other,
cherish very nearly the same feelings towards the ocean with me.`);
}
}
}).listen(9091, () => {
console.log('Cat in the bag yo');
});