decentralised message store
1import { setupDbWithMigrations } from "@/db"; 2import { DB_URL, SERVER_PORT } from "@/lib/env"; 3import { newErrorResponse } from "@/lib/utils/http/responses"; 4import { 5 wrapHttpRegistrationCheck, 6 wrapWsRegistrationCheck, 7} from "@/lib/utils/registration"; 8import { routes } from "@/routes"; 9import { setupServer } from "@/server"; 10 11const main = async () => { 12 if (DB_URL === ":memory:") await setupDbWithMigrations("./drizzle"); 13 14 const server = await setupServer(); 15 for (const [url, route] of Object.entries(routes)) { 16 if (!route.wsHandler) { 17 const { handler, method, skipRegistrationCheck } = route; 18 server.route({ 19 url, 20 method, 21 handler: skipRegistrationCheck 22 ? handler 23 : wrapHttpRegistrationCheck(handler), 24 }); 25 } else { 26 const { 27 wsHandler, 28 method, 29 handler: httpHandler, 30 preHandler, 31 skipRegistrationCheckHttp, 32 skipRegistrationCheckWs, 33 } = route; 34 35 const handler = 36 httpHandler ?? 37 (() => 38 newErrorResponse(404, { 39 message: 40 "This is a websocket only route. Did you mean to initiate a websocket connection here?", 41 })); 42 43 server.register(() => { 44 server.route({ 45 url, 46 method: method ?? "GET", 47 handler: skipRegistrationCheckHttp 48 ? handler 49 : wrapHttpRegistrationCheck(handler), 50 wsHandler: skipRegistrationCheckWs 51 ? wsHandler 52 : wrapWsRegistrationCheck(wsHandler), 53 preHandler, 54 }); 55 }); 56 } 57 } 58 59 server.listen({ port: SERVER_PORT }).catch((err: unknown) => { 60 server.log.error(err); 61 process.exit(1); 62 }); 63}; 64 65main() 66 .then(() => { 67 console.log(`Server is running on port ${SERVER_PORT.toString()}`); 68 }) 69 .catch((err: unknown) => { 70 console.error("Something went wrong :("); 71 console.error( 72 "=========================== FULL ERROR BELOW ===========================", 73 ); 74 console.error(err); 75 });