Skip to content

Commit

Permalink
feat(store-indexer): replace fastify with koa (#2006)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Dec 4, 2023
1 parent e5a962b commit c314bad
Show file tree
Hide file tree
Showing 6 changed files with 488 additions and 64 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-tips-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/store-indexer": patch
---

Replaced Fastify with Koa for store-indexer frontends
46 changes: 27 additions & 19 deletions packages/store-indexer/bin/postgres-frontend.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/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 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 +22,35 @@ 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(cors());

const router = new Router();

await server.register(import("@fastify/compress"));
await server.register(import("@fastify/cors"));
router.get("/", (ctx) => {
ctx.body = "emit HelloWorld();";
});

// 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;
});

server.use(router.routes());
server.use(router.allowedMethods());

// @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}`);
37 changes: 30 additions & 7 deletions packages/store-indexer/bin/postgres-indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,38 @@ combineLatest([latestBlockNumber$, storedBlockLogs$])
});

if (env.HEALTHCHECK_HOST != null || env.HEALTHCHECK_PORT != null) {
const { default: fastify } = await import("fastify");
const { default: Koa } = await import("koa");
const { default: cors } = await import("@koa/cors");
const { default: Router } = await import("@koa/router");

const server = fastify();
const server = new Koa();
server.use(cors());

// k8s healthchecks
server.get("/healthz", (req, res) => res.code(200).send());
server.get("/readyz", (req, res) => (isCaughtUp ? res.code(200).send("ready") : res.code(424).send("backfilling")));
const router = new Router();

router.get("/", (ctx) => {
ctx.body = "emit HelloWorld();";
});

server.listen({ host: env.HEALTHCHECK_HOST, port: env.HEALTHCHECK_PORT }, (error, address) => {
console.log(`postgres indexer healthcheck server listening on ${address}`);
// k8s healthchecks
router.get("/healthz", (ctx) => {
ctx.status = 200;
});
router.get("/readyz", (ctx) => {
if (isCaughtUp) {
ctx.status = 200;
ctx.body = "ready";
} else {
ctx.status = 424;
ctx.body = "backfilling";
}
});

server.use(router.routes());
server.use(router.allowedMethods());

server.listen({ host: env.HEALTHCHECK_HOST, port: env.HEALTHCHECK_PORT });
console.log(
`postgres indexer healthcheck server listening on http://${env.HEALTHCHECK_HOST}:${env.HEALTHCHECK_PORT}`
);
}
51 changes: 33 additions & 18 deletions packages/store-indexer/bin/sqlite-indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { eq } from "drizzle-orm";
import { drizzle } from "drizzle-orm/better-sqlite3";
import Database from "better-sqlite3";
import { createPublicClient, fallback, webSocket, http, Transport } from "viem";
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 cors from "@koa/cors";
import Router from "@koa/router";
import { createKoaMiddleware } from "trpc-koa-adapter";
import { createAppRouter } from "@latticexyz/store-sync/trpc-indexer";
import { chainState, schemaVersion, syncToSqlite } from "@latticexyz/store-sync/sqlite";
import { createQueryAdapter } from "../src/sqlite/createQueryAdapter";
import { isDefined } from "@latticexyz/common/utils";
Expand Down Expand Up @@ -87,28 +89,41 @@ combineLatest([latestBlockNumber$, storedBlockLogs$])
console.log("all caught up");
});

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

const router = new Router();

await server.register(import("@fastify/compress"));
await server.register(import("@fastify/cors"));
router.get("/", (ctx) => {
ctx.body = "emit HelloWorld();";
});

// k8s healthchecks
server.get("/healthz", (req, res) => res.code(200).send());
server.get("/readyz", (req, res) => (isCaughtUp ? res.code(200).send("ready") : res.code(424).send("backfilling")));
router.get("/healthz", (ctx) => {
ctx.status = 200;
});
router.get("/readyz", (ctx) => {
if (isCaughtUp) {
ctx.status = 200;
ctx.body = "ready";
} else {
ctx.status = 424;
ctx.body = "backfilling";
}
});

server.use(router.routes());
server.use(router.allowedMethods());

// @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(`sqlite indexer frontend listening on http://${env.HOST}:${env.PORT}`);
10 changes: 7 additions & 3 deletions packages/store-indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"test:ci": "pnpm run test"
},
"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 @@ -48,17 +48,21 @@
"debug": "^4.3.4",
"dotenv": "^16.0.3",
"drizzle-orm": "^0.28.5",
"fastify": "^4.21.0",
"koa": "^2.14.2",
"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__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 c314bad

Please sign in to comment.