decentralised sync engine
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: wip setup flow

serenity 753a3c21 2604b4fb

+62 -6
+62 -6
src/index.ts
··· 1 - import { SERVER_PORT } from "@/lib/env"; 1 + import { OWNER_DID, SERVER_PORT, SERVICE_DID } from "@/lib/env"; 2 + import { setRegistrationState } from "@/lib/state"; 3 + import { getRecordFromAtUri } from "@/lib/utils/atproto"; 4 + import { newErrorResponse } from "@/lib/utils/http/responses"; 5 + import { connectToPrism } from "@/lib/utils/prism"; 6 + import { 7 + attachLatticeRegistrationListener, 8 + wrapHttpRegistrationCheck, 9 + wrapWsRegistrationCheck, 10 + } from "@/lib/utils/registration"; 2 11 import { routes } from "@/routes"; 3 12 import { setupServer } from "@/server"; 4 13 5 14 const main = async () => { 15 + let latticeUrlOrigin = decodeURIComponent( 16 + SERVICE_DID.startsWith("did:web:") ? SERVICE_DID.slice(8) : "", 17 + ); 18 + if (latticeUrlOrigin === "localhost") 19 + latticeUrlOrigin += `:${SERVER_PORT.toString()}`; 20 + if (latticeUrlOrigin === "") { 21 + // TODO: resolve did:plc endpoint to get the origin of the lattice endpoint described by the did:plc doc 22 + // for now we just throw. 23 + throw new Error( 24 + "did:plc support not yet implemented. Provide a did:web for now. did:plc support will come in the future.", 25 + ); 26 + } 27 + 28 + const latticeRecord = await getRecordFromAtUri({ 29 + // @ts-expect-error alas, template literal weirdness continues uwu 30 + authority: OWNER_DID, 31 + collection: "systems.gmstn.development.lattice", 32 + rKey: latticeUrlOrigin, 33 + }); 34 + 35 + if (latticeRecord.ok) setRegistrationState(true); 36 + 37 + const prismWebsocket = connectToPrism({ 38 + wantedCollections: ["systems.gmstn.development.*"], 39 + }); 40 + 41 + attachLatticeRegistrationListener(prismWebsocket); 42 + 6 43 const server = await setupServer(); 7 44 for (const [url, route] of Object.entries(routes)) { 8 45 if (!route.wsHandler) { 9 - const { handler, method } = route; 46 + const { handler, method, skipRegistrationCheck } = route; 10 47 server.route({ 11 48 url, 12 49 method, 13 - handler, 50 + handler: skipRegistrationCheck 51 + ? handler 52 + : wrapHttpRegistrationCheck(handler), 14 53 }); 15 54 } else { 16 - const { wsHandler, method, handler } = route; 55 + const { 56 + wsHandler, 57 + method, 58 + handler: httpHandler, 59 + skipRegistrationCheckHttp, 60 + skipRegistrationCheckWs, 61 + } = route; 62 + const handler = 63 + httpHandler ?? 64 + (() => 65 + newErrorResponse(404, { 66 + message: 67 + "This is a websocket only route. Did you mean to initiate a websocket connection here?", 68 + })); 17 69 server.route({ 18 70 url, 19 71 method: method ?? "GET", 20 - handler: handler ?? (() => new Response()), 21 - wsHandler, 72 + handler: skipRegistrationCheckHttp 73 + ? handler 74 + : wrapHttpRegistrationCheck(handler), 75 + wsHandler: skipRegistrationCheckWs 76 + ? wsHandler 77 + : wrapWsRegistrationCheck(wsHandler), 22 78 }); 23 79 } 24 80 }