forked from lidge-jun/ima2-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
162 lines (148 loc) · 4.85 KB
/
server.js
File metadata and controls
162 lines (148 loc) · 4.85 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import "dotenv/config";
import express from "express";
import { readFile } from "fs/promises";
import {
existsSync,
writeFileSync,
unlinkSync,
mkdirSync,
readFileSync as fsReadFileSync,
} from "fs";
import { dirname, join } from "path";
import { fileURLToPath, pathToFileURL } from "url";
import { onShutdown } from "./bin/lib/platform.js";
import { ensureDefaultSession } from "./lib/sessionStore.js";
import { startOAuthProxy } from "./lib/oauthLauncher.js";
import { migrateGeneratedStorage } from "./lib/storageMigration.js";
import { configureRoutes } from "./routes/index.js";
import { config } from "./config.js";
const rootDir = dirname(fileURLToPath(import.meta.url));
async function loadApiKey() {
if (process.env.OPENAI_API_KEY) {
return { apiKey: process.env.OPENAI_API_KEY, apiKeySource: "env" };
}
const candidates = [
config.storage.configFile,
join(rootDir, ".ima2", "config.json"),
];
for (const cfgPath of candidates) {
if (!existsSync(cfgPath)) continue;
try {
const cfg = JSON.parse(await readFile(cfgPath, "utf-8"));
if (cfg.apiKey) return { apiKey: cfg.apiKey, apiKeySource: "config" };
} catch {}
}
return { apiKey: null, apiKeySource: "none" };
}
async function createOpenAI(apiKey) {
if (!apiKey) return null;
const OpenAI = (await import("openai")).default;
return new OpenAI({ apiKey });
}
function readPackageVersion() {
try {
return JSON.parse(fsReadFileSync(join(rootDir, "package.json"), "utf-8")).version;
} catch {
return "0.0.0";
}
}
export function buildApp(ctx) {
const app = express();
app.use(express.json({ limit: ctx.config.server.bodyLimit }));
app.use(express.static(join(ctx.rootDir, "ui", "dist")));
app.use("/generated", express.static(ctx.config.storage.generatedDir, {
maxAge: ctx.config.storage.staticMaxAge,
immutable: true,
}));
configureRoutes(app, ctx);
return app;
}
function advertise(ctx) {
try {
mkdirSync(dirname(ctx.config.storage.advertiseFile), { recursive: true });
writeFileSync(
ctx.config.storage.advertiseFile,
JSON.stringify({
port: Number(ctx.config.server.port),
pid: process.pid,
startedAt: ctx.startedAt,
version: ctx.packageVersion,
}),
);
} catch (e) {
console.warn("[advertise] skipped:", e.message);
}
}
function unadvertise(ctx) {
try {
if (!existsSync(ctx.config.storage.advertiseFile)) return;
const cur = JSON.parse(fsReadFileSync(ctx.config.storage.advertiseFile, "utf-8"));
if (cur.pid === process.pid) unlinkSync(ctx.config.storage.advertiseFile);
} catch {}
}
export async function createRuntimeContext(overrides = {}) {
const loadedKey =
overrides.apiKey !== undefined
? {
apiKey: overrides.apiKey,
apiKeySource: overrides.apiKeySource ?? (overrides.apiKey ? "env" : "none"),
}
: await loadApiKey();
const apiKey = loadedKey.apiKey;
const openai = overrides.openai ?? await createOpenAI(apiKey);
const oauthPort = config.oauth.proxyPort;
return {
rootDir,
config,
oauthPort,
oauthUrl: `http://127.0.0.1:${oauthPort}`,
hasApiKey: !!apiKey,
apiKey,
apiKeySource: loadedKey.apiKeySource,
openai,
startedAt: overrides.startedAt ?? Date.now(),
packageVersion: overrides.packageVersion ?? readPackageVersion(),
};
}
export async function startServer(overrides = {}) {
const ctx = await createRuntimeContext(overrides);
await migrateGeneratedStorage(ctx);
const app = buildApp(ctx);
const oauthChild =
overrides.oauthChild !== undefined
? overrides.oauthChild
: !ctx.config.oauth.autoStart
? null
: startOAuthProxy({
oauthPort: ctx.oauthPort,
restartDelayMs: ctx.config.oauth.restartDelayMs,
});
onShutdown(() => {
unadvertise(ctx);
try { oauthChild?.kill(); } catch {}
});
process.on("exit", () => unadvertise(ctx));
const server = app.listen(ctx.config.server.port, () => {
console.log(`Image Gen running at http://localhost:${ctx.config.server.port}`);
console.log(`Provider policy: OAuth only (API key hard-disabled). OAuth proxy port ${ctx.oauthPort}.`);
advertise(ctx);
try {
const s = ensureDefaultSession();
console.log(`[db] default session: ${s.id} (${s.title})`);
} catch (err) {
console.error("[db] bootstrap failed:", err.message);
}
});
server.on("error", (err) => {
if (err?.code === "EADDRINUSE") {
console.error(`[server] Port ${ctx.config.server.port} is already in use. Stop the existing image_gen server before starting another dev server.`);
process.exit(1);
}
console.error("[server] Failed to start:", err?.message || err);
process.exit(1);
});
return { app, server, oauthChild, ctx };
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
await startServer();
}