-
Notifications
You must be signed in to change notification settings - Fork 1
/
http-proxy.ts
58 lines (47 loc) · 1.28 KB
/
http-proxy.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
import { Client } from "pg";
import { Hono } from "hono";
const app = new Hono();
const client = new Client({
host: process.env.PROXY_HOST,
user: process.env.PROXY_USER,
database: process.env.PROXY_NAME,
password: process.env.PROXY_PASS,
port: Number(process.env.PROXY_PORT),
});
// @ts-ignore - using bun
await client.connect();
app.post("/query", async (c) => {
const key = c.req.header("proxy-key");
if (key !== process.env.PROXY_KEY) {
return c.json({ error: "Invalid key" }, 401);
}
const { sql, params, method } = await c.req.json();
// prevent multiple queries
const sqlBody = sql.replace(/;/g, "");
try {
if (method === "all") {
const result = await client.query({
text: sqlBody,
values: params,
rowMode: "array",
});
return c.json(result.rows);
}
if (method === "execute") {
const result = await client.query({
text: sqlBody,
values: params,
});
return c.json(result.rows);
}
return c.json({ error: "Unknown method value" }, 500);
} catch (e) {
console.error(e);
return c.json({ error: "error" }, 500);
}
});
console.log(`Proxy listening on port ${process.env.PROXY_HTTP_PORT}`);
export default {
port: process.env.PROXY_HTTP_PORT,
fetch: app.fetch,
};