Skip to content

Commit

Permalink
feat: Add sqlite db to test api (#54)
Browse files Browse the repository at this point in the history
Setup the test-api to use a sqlite db to store stats file, and expose a route to fetch the store information.
  • Loading branch information
nicholas-codecov committed Jan 19, 2024
1 parent c1fdbd6 commit 803897e
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 64 deletions.
23 changes: 10 additions & 13 deletions integration-tests/test-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
# syntax=docker/dockerfile:1
ARG BUILDPLATFORM=arm64
FROM --platform=$BUILDPLATFORM node:20-alpine
WORKDIR /app
# use the official Bun image
# see all versions at https://hub.docker.com/r/oven/bun/tags
FROM oven/bun:1-alpine
WORKDIR /usr/src/app

ENV NODE_ENV=production
RUN apk update && apk upgrade
RUN apk add --no-cache sqlite

COPY . .
RUN bun install --frozen-lockfile

RUN npm install -g pnpm
RUN pnpm install

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 hono

USER hono
# run the app
USER bun
EXPOSE 8000
ENV PORT 8000

CMD ["pnpm", "run", "start"]
CMD [ "bun", "run", "src/index.ts" ]
Binary file added integration-tests/test-api/bun.lockb
Binary file not shown.
3 changes: 3 additions & 0 deletions integration-tests/test-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
},
"volta": {
"extends": "../../package.json"
},
"devDependencies": {
"@types/bun": "^1.0.2"
}
}
67 changes: 50 additions & 17 deletions integration-tests/test-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
import { serve } from "@hono/node-server";
import { Hono } from "hono";
import { Database } from "bun:sqlite";

console.log("connecting to sqlite");
const sqlite = new Database(":memory:", { readwrite: true });
console.log("connected to sqlite");

console.log('creating "stats" table');
const query = sqlite.query(
"CREATE TABLE `stats` (`id` text, `json_stats` text );",
);
query.run();
console.log('created "stats" table');

console.log("starting api");
const app = new Hono();

app.all("/ping", (c) => {
return c.text("pong", 200);
});

app.all(
"/test-url/:status/:badPUT{true|false}/upload/bundle_analysis/v1",
"/test-url/:id/:status/:badPUT{true|false}/upload/bundle_analysis/v1",
(c) => {
const id = c.req.param("id");
const status = parseInt(c.req.param("status"));
const badPUT = c.req.param("badPUT") === "true";
const url = new URL(c.req.url);
let putURL = `${url.protocol}//${url.host}/file-upload`;
const putURL = `${url.protocol}//${url.host}/file-upload/${id}/${status}`;

if (status >= 400 && !badPUT) {
return c.text(`Error code: ${status}`, { status });
}

if (badPUT) {
putURL = `${putURL}/${status}`;
}
console.log("PUT URL", putURL);

return c.json(
{
Expand All @@ -28,24 +43,42 @@ app.all(
},
);

app.all("/file-upload/:status{[0-9]{3}}", async (c) => {
app.all("/file-upload/:id/:status{[0-9]{3}}", async (c) => {
const id = c.req.param("id");
const status = parseInt(c.req.param("status"));

if (status >= 400) {
return c.text(`Error code: ${status}`, { status });
}

await c.req.json();
console.log("uploading file");
const data: unknown = await c.req.json();
console.log("finished upload");

console.log("inserting stats");
const insertStats = JSON.stringify(data);
const query = sqlite.query(
`INSERT INTO stats (id, json_stats) VALUES ('${id}', '${insertStats}')`,
);
query.run();
query.finalize();
console.log("inserted stats");

return c.text("File uploaded successfully", { status: 200 });
});

serve(
{
fetch: app.fetch,
port: 8000,
},
(info) => {
console.info(`🚀 Server listening on ${info.address}:${info.port}`);
},
);
app.all("/get-stats/:id", (c) => {
const id = c.req.param("id");

const query = sqlite.query("SELECT * FROM stats WHERE id = $id");
const result = query.get({ $id: id }) as { id: string; json_stats: string };
query.finalize();

if (result) {
return c.json({ stats: result.json_stats }, { status: 200 });
}

return c.text("Not found", { status: 404 });
});

export default app;

0 comments on commit 803897e

Please sign in to comment.