1import os from "os";
2import { Client, CredentialManager } from "@atcute/client";
3import { getPdsEndpoint } from "@atcute/identity";
4import {
5 CompositeDidDocumentResolver,
6 PlcDidDocumentResolver,
7 WebDidDocumentResolver,
8} from "@atcute/identity-resolver";
9import { config } from "./config";
10import type {} from "@atcute/atproto";
11import {
12 applyMiddleware,
13 applyMiddlewareAll,
14 getRecord,
15 log,
16 ok,
17 putRecord,
18 type Middleware,
19 type Result,
20} from "./utils";
21import store from "./store";
22import routes from "./routes";
23import { handleEvents } from "./jetstream";
24
25const docResolver = new CompositeDidDocumentResolver({
26 methods: {
27 plc: new PlcDidDocumentResolver(),
28 web: new WebDidDocumentResolver(),
29 },
30});
31
32const pdsUrl = getPdsEndpoint(await docResolver.resolve(config.repoDid));
33if (pdsUrl === undefined) {
34 throw `no pds found`;
35}
36console.info(`pds is ${pdsUrl}`);
37
38const creds = new CredentialManager({ service: pdsUrl });
39const session = await creds.login({
40 identifier: config.repoDid,
41 password: config.appPass,
42});
43export const atpClient = new Client({ handler: creds });
44
45// fetch host record for this host
46const maybeRecord = await getRecord(
47 "systems.gaze.barometer.host",
48 store.hostname,
49);
50if (maybeRecord.ok) {
51 store.host = maybeRecord.value;
52}
53
54// if it doesnt exist we make a new one
55if (store.host === null) {
56 await putRecord(
57 {
58 $type: "systems.gaze.barometer.host",
59 name: config.hostName ?? store.hostname,
60 description: config.hostDescription,
61 os: os.platform(),
62 },
63 store.hostname,
64 );
65}
66
67const traceRequest: Middleware = async (req) => {
68 const url = new URL(req.url);
69 log.info(`${req.method} ${url.pathname}`);
70 return req;
71};
72const server = Bun.serve({
73 routes: applyMiddlewareAll([traceRequest], routes),
74});
75
76console.log(`server running on http://localhost:${server.port}`);
77
78await handleEvents();