Skip to content

Commit 803897e

Browse files
authored
feat: Add sqlite db to test api (#54)
Setup the test-api to use a sqlite db to store stats file, and expose a route to fetch the store information.
1 parent c1fdbd6 commit 803897e

File tree

6 files changed

+89
-64
lines changed

6 files changed

+89
-64
lines changed

integration-tests/test-api/Dockerfile

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
# syntax=docker/dockerfile:1
2-
ARG BUILDPLATFORM=arm64
3-
FROM --platform=$BUILDPLATFORM node:20-alpine
4-
WORKDIR /app
2+
# use the official Bun image
3+
# see all versions at https://hub.docker.com/r/oven/bun/tags
4+
FROM oven/bun:1-alpine
5+
WORKDIR /usr/src/app
56

6-
ENV NODE_ENV=production
7+
RUN apk update && apk upgrade
8+
RUN apk add --no-cache sqlite
79

810
COPY . .
11+
RUN bun install --frozen-lockfile
912

10-
RUN npm install -g pnpm
11-
RUN pnpm install
12-
13-
RUN addgroup --system --gid 1001 nodejs
14-
RUN adduser --system --uid 1001 hono
15-
16-
USER hono
13+
# run the app
14+
USER bun
1715
EXPOSE 8000
1816
ENV PORT 8000
19-
20-
CMD ["pnpm", "run", "start"]
17+
CMD [ "bun", "run", "src/index.ts" ]

integration-tests/test-api/bun.lockb

14.7 KB
Binary file not shown.

integration-tests/test-api/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
},
1111
"volta": {
1212
"extends": "../../package.json"
13+
},
14+
"devDependencies": {
15+
"@types/bun": "^1.0.2"
1316
}
1417
}
Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
1-
import { serve } from "@hono/node-server";
21
import { Hono } from "hono";
2+
import { Database } from "bun:sqlite";
33

4+
console.log("connecting to sqlite");
5+
const sqlite = new Database(":memory:", { readwrite: true });
6+
console.log("connected to sqlite");
7+
8+
console.log('creating "stats" table');
9+
const query = sqlite.query(
10+
"CREATE TABLE `stats` (`id` text, `json_stats` text );",
11+
);
12+
query.run();
13+
console.log('created "stats" table');
14+
15+
console.log("starting api");
416
const app = new Hono();
517

18+
app.all("/ping", (c) => {
19+
return c.text("pong", 200);
20+
});
21+
622
app.all(
7-
"/test-url/:status/:badPUT{true|false}/upload/bundle_analysis/v1",
23+
"/test-url/:id/:status/:badPUT{true|false}/upload/bundle_analysis/v1",
824
(c) => {
25+
const id = c.req.param("id");
926
const status = parseInt(c.req.param("status"));
1027
const badPUT = c.req.param("badPUT") === "true";
1128
const url = new URL(c.req.url);
12-
let putURL = `${url.protocol}//${url.host}/file-upload`;
29+
const putURL = `${url.protocol}//${url.host}/file-upload/${id}/${status}`;
1330

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

18-
if (badPUT) {
19-
putURL = `${putURL}/${status}`;
20-
}
35+
console.log("PUT URL", putURL);
2136

2237
return c.json(
2338
{
@@ -28,24 +43,42 @@ app.all(
2843
},
2944
);
3045

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

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

38-
await c.req.json();
54+
console.log("uploading file");
55+
const data: unknown = await c.req.json();
56+
console.log("finished upload");
57+
58+
console.log("inserting stats");
59+
const insertStats = JSON.stringify(data);
60+
const query = sqlite.query(
61+
`INSERT INTO stats (id, json_stats) VALUES ('${id}', '${insertStats}')`,
62+
);
63+
query.run();
64+
query.finalize();
65+
console.log("inserted stats");
3966

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

43-
serve(
44-
{
45-
fetch: app.fetch,
46-
port: 8000,
47-
},
48-
(info) => {
49-
console.info(`🚀 Server listening on ${info.address}:${info.port}`);
50-
},
51-
);
70+
app.all("/get-stats/:id", (c) => {
71+
const id = c.req.param("id");
72+
73+
const query = sqlite.query("SELECT * FROM stats WHERE id = $id");
74+
const result = query.get({ $id: id }) as { id: string; json_stats: string };
75+
query.finalize();
76+
77+
if (result) {
78+
return c.json({ stats: result.json_stats }, { status: 200 });
79+
}
80+
81+
return c.text("Not found", { status: 404 });
82+
});
83+
84+
export default app;

0 commit comments

Comments
 (0)