-
Notifications
You must be signed in to change notification settings - Fork 162
/
index.js
72 lines (69 loc) · 1.96 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"use strict";
const {
PDS,
envToCfg,
envToSecrets,
readEnv,
httpLogger,
} = require("@atproto/pds");
const pkg = require("@atproto/pds/package.json");
const main = async () => {
const env = readEnv();
env.version ||= pkg.version;
const cfg = envToCfg(env);
const secrets = envToSecrets(env);
const pds = await PDS.create(cfg, secrets);
await pds.start();
httpLogger.info("pds has started");
pds.app.get("/tls-check", (req, res) => {
checkHandleRoute(pds, req, res);
});
// Graceful shutdown (see also https://aws.amazon.com/blogs/containers/graceful-shutdowns-with-ecs/)
process.on("SIGTERM", async () => {
httpLogger.info("pds is stopping");
await pds.destroy();
httpLogger.info("pds is stopped");
});
};
async function checkHandleRoute(
/** @type {PDS} */ pds,
/** @type {import('express').Request} */ req,
/** @type {import('express').Response} */ res
) {
try {
const { domain } = req.query;
if (!domain || typeof domain !== "string") {
return res.status(400).json({
error: "InvalidRequest",
message: "bad or missing domain query param",
});
}
if (domain === pds.ctx.cfg.service.hostname) {
return res.json({ success: true });
}
const isHostedHandle = pds.ctx.cfg.identity.serviceHandleDomains.find(
(avail) => domain.endsWith(avail)
);
if (!isHostedHandle) {
return res.status(400).json({
error: "InvalidRequest",
message: "handles are not provided on this domain",
});
}
const account = await pds.ctx.accountManager.getAccount(domain);
if (!account) {
return res.status(404).json({
error: "NotFound",
message: "handle not found for this domain",
});
}
return res.json({ success: true });
} catch (err) {
httpLogger.error({ err }, "check handle failed");
return res.status(500).json({
error: "InternalServerError",
message: "Internal Server Error",
});
}
}
main();