Skip to content

Commit

Permalink
swap postgres indexer frontend with koa
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Dec 3, 2023
1 parent 809905d commit b53077d
Show file tree
Hide file tree
Showing 3 changed files with 451 additions and 28 deletions.
41 changes: 22 additions & 19 deletions packages/store-indexer/bin/postgres-frontend.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#!/usr/bin/env node
import "dotenv/config";
import { z } from "zod";
import fastify from "fastify";
import { fastifyTRPCPlugin } from "@trpc/server/adapters/fastify";
import { AppRouter, createAppRouter } from "@latticexyz/store-sync/trpc-indexer";
import Koa from "koa";
import compress from "koa-compress";
import cors from "@koa/cors";
import Router from "@koa/router";
import { createKoaMiddleware } from "trpc-koa-adapter";
import { createAppRouter } from "@latticexyz/store-sync/trpc-indexer";
import { createQueryAdapter } from "../src/postgres/createQueryAdapter";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
Expand All @@ -20,29 +23,29 @@ const env = parseEnv(

const database = drizzle(postgres(env.DATABASE_URL));

// @see https://fastify.dev/docs/latest/
const server = fastify({
maxParamLength: 5000,
logger: true,
});
const server = new Koa();
server.use(compress());
server.use(cors());

await server.register(import("@fastify/compress"));
await server.register(import("@fastify/cors"));
const router = new Router();

// k8s healthchecks
server.get("/healthz", (req, res) => res.code(200).send());
server.get("/readyz", (req, res) => res.code(200).send());
router.get("/healthz", (ctx) => {
ctx.status = 200;
});
router.get("/readyz", (ctx) => {
ctx.status = 200;
});

// @see https://trpc.io/docs/server/adapters/fastify
server.register(fastifyTRPCPlugin<AppRouter>, {
prefix: "/trpc",
trpcOptions: {
server.use(
createKoaMiddleware({
prefix: "/trpc",
router: createAppRouter(),
createContext: async () => ({
queryAdapter: await createQueryAdapter(database),
}),
},
});
})
);

await server.listen({ host: env.HOST, port: env.PORT });
server.listen({ host: env.HOST, port: env.PORT });
console.log(`postgres indexer frontend listening on http://${env.HOST}:${env.PORT}`);
9 changes: 9 additions & 0 deletions packages/store-indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"dependencies": {
"@fastify/compress": "^6.5.0",
"@fastify/cors": "^8.3.0",
"@koa/cors": "^4.0.0",
"@koa/router": "^12.0.1",
"@latticexyz/block-logs-stream": "workspace:*",
"@latticexyz/common": "workspace:*",
"@latticexyz/protocol-parser": "workspace:*",
Expand All @@ -49,16 +51,23 @@
"dotenv": "^16.0.3",
"drizzle-orm": "^0.28.5",
"fastify": "^4.21.0",
"koa": "^2.14.2",
"koa-compress": "^5.1.1",
"postgres": "^3.3.5",
"rxjs": "7.5.5",
"superjson": "^1.12.4",
"trpc-koa-adapter": "^1.1.3",
"viem": "1.14.0",
"zod": "^3.21.4"
},
"devDependencies": {
"@types/better-sqlite3": "^7.6.4",
"@types/cors": "^2.8.13",
"@types/debug": "^4.1.7",
"@types/koa": "^2.13.12",
"@types/koa-compress": "^4.0.6",
"@types/koa__cors": "^4.0.3",
"@types/koa__router": "^12.0.4",
"concurrently": "^8.2.2",
"tsup": "^6.7.0",
"tsx": "^3.12.6",
Expand Down
Loading

0 comments on commit b53077d

Please sign in to comment.