-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
72 lines (60 loc) · 1.62 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import pg from "pg";
import { Application, Router } from "https://deno.land/x/[email protected]/mod.ts";
import { oakCors } from "https://deno.land/x/[email protected]/mod.ts";
import {
processInput,
setupSchema,
initialSetup,
} from "./browser-test/src/domain.ts";
const { Pool } = pg;
const pool = new Pool({
user: "postgres",
password: "password",
host: "localhost",
port: 54321,
database: "electric",
});
try {
await pool.query("select * from input");
await pool.query("select * from mutable");
} catch (_err) {
await pool.query(setupSchema());
await pool.query(initialSetup());
}
const router = new Router();
router.get("/health", (ctx) => {
ctx.response.body = "healthy";
});
router.post("/submit", async (context) => {
const client = await pool.connect();
try {
await client.query("BEGIN;");
const body = await context.request.body.json();
for (const input of body) {
const queries = processInput(input, 0, true);
for (const query of queries) {
await client.query(query);
}
}
await client.query("COMMIT");
context.response.status = 200;
context.response.body = { message: "Data processed" };
// for some reason the middleware doesn't set this
context.response.headers.append(
"Access-Control-Allow-Origin",
"http://localhost:5173"
);
} catch (error) {
context.response.status = 400;
context.response.body = { message: `Bad request: ${error}` };
}
});
const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());
app.use(
oakCors({
origin: /^.+localhost:5173$/,
})
);
app.listen({ port: 3112 });