/ service / index.js
index.js
 1  "use strict";
 2  const {
 3    PDS,
 4    envToCfg,
 5    envToSecrets,
 6    readEnv,
 7    httpLogger,
 8  } = require("@atproto/pds");
 9  const pkg = require("@atproto/pds/package.json");
10  
11  const main = async () => {
12    const env = readEnv();
13    env.version ||= pkg.version;
14    const cfg = envToCfg(env);
15    const secrets = envToSecrets(env);
16    const pds = await PDS.create(cfg, secrets);
17    await pds.start();
18    httpLogger.info("pds has started");
19    pds.app.get("/tls-check", (req, res) => {
20      checkHandleRoute(pds, req, res);
21    });
22    // Graceful shutdown (see also https://aws.amazon.com/blogs/containers/graceful-shutdowns-with-ecs/)
23    process.on("SIGTERM", async () => {
24      httpLogger.info("pds is stopping");
25      await pds.destroy();
26      httpLogger.info("pds is stopped");
27    });
28  };
29  
30  async function checkHandleRoute(
31    /** @type {PDS} */ pds,
32    /** @type {import('express').Request} */ req,
33    /** @type {import('express').Response} */ res
34  ) {
35    try {
36      const { domain } = req.query;
37      if (!domain || typeof domain !== "string") {
38        return res.status(400).json({
39          error: "InvalidRequest",
40          message: "bad or missing domain query param",
41        });
42      }
43      if (domain === pds.ctx.cfg.service.hostname) {
44        return res.json({ success: true });
45      }
46      const isHostedHandle = pds.ctx.cfg.identity.serviceHandleDomains.find(
47        (avail) => domain.endsWith(avail)
48      );
49      if (!isHostedHandle) {
50        return res.status(400).json({
51          error: "InvalidRequest",
52          message: "handles are not provided on this domain",
53        });
54      }
55      const account = await pds.ctx.accountManager.getAccount(domain);
56      if (!account) {
57        return res.status(404).json({
58          error: "NotFound",
59          message: "handle not found for this domain",
60        });
61      }
62      return res.json({ success: true });
63    } catch (err) {
64      httpLogger.error({ err }, "check handle failed");
65      return res.status(500).json({
66        error: "InternalServerError",
67        message: "Internal Server Error",
68      });
69    }
70  }
71  
72  main();