-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (47 loc) · 1.68 KB
/
server.js
File metadata and controls
59 lines (47 loc) · 1.68 KB
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
// server.js (bootstrap)
import "dotenv/config";
import OpenAI from "openai";
import pkg from "pg";
import { createApp } from "./app.js";
const { Pool } = pkg;
console.log("Starting doc-assist server...");
const parseBool = (value, defaultValue) => {
if (value == null || value === "") return defaultValue;
const s = String(value).trim().toLowerCase();
if (["1", "true", "yes", "y", "on"].includes(s)) return true;
if (["0", "false", "no", "n", "off"].includes(s)) return false;
return defaultValue;
};
const isProd = String(process.env.NODE_ENV || "").toLowerCase() === "production";
const requireToken = parseBool(process.env.DOCASSIST_REQUIRE_TOKEN, isProd);
if (isProd && !process.env.OPENAI_API_KEY) {
throw new Error("Missing OPENAI_API_KEY (required in production)");
}
if (isProd && requireToken && !process.env.DOCASSIST_TOKEN) {
throw new Error(
"Missing DOCASSIST_TOKEN (required in production when DOCASSIST_REQUIRE_TOKEN is enabled)"
);
}
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false,
},
});
const openaiClient = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
const { app, ensureTables, config } = createApp({
pool,
openaiClient,
});
await ensureTables();
const PORT = Number.parseInt(process.env.PORT || "3000", 10);
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
console.log(`DOCASSIST_BODY_LIMIT=${config.bodyLimit}`);
console.log(`OPENAI_MODEL=${config.openaiModel}`);
console.log(`DOCASSIST_MAX_OUTPUT_TOKENS=${config.maxOutputTokens}`);
console.log(`DOCASSIST_TOKEN=${config.token ? "(set)" : "(not set)"}`);
});
export { createApp } from "./app.js";