-
Notifications
You must be signed in to change notification settings - Fork 24
/
server.ts
115 lines (89 loc) · 3.41 KB
/
server.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
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
import path from "path";
import { fileURLToPath } from "url";
import process from "process";
import minimist from "minimist";
import Koa from "koa";
import Router from "koa-router";
import { koaBody } from "koa-body";
import serve from "koa-static";
import { globalServerConfig } from "./server/config.ts";
import { buildNodeRegistry, ensureDirExists } from "./server/utils.ts";
import { setupResourcesApi } from "./server/resources.ts";
import { setupExecutorApi } from "./server/executor.ts";
import { pingExternalModule } from "./server/external.ts";
import { makeNode } from "./src/nodes/_nodes/_Base.ts";
import { AppVersionUtils } from "./src/util/AppVersionUtils.ts";
import { setupTranscriptionAPI } from "./server/transcribe.ts";
// Attach node maker for custom nodes to use
(global as any).__ocMakeNode = makeNode;
// Catch unhandled exceptions
process.on("uncaughtException", function (err) {
console.log("Uncaught Error: " + err.message);
});
// Base setup
const argv: Record<string, any> = minimist(process.argv.slice(2));
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const DIR_FRONTEND = path.join(__dirname, "dist");
const DIR_DATA = path.join(__dirname, "data");
const DIR_CUSTOM_NODES = path.join(__dirname, "custom_nodes");
ensureDirExists(DIR_DATA);
ensureDirExists(DIR_CUSTOM_NODES);
ensureDirExists(DIR_FRONTEND);
globalServerConfig.dirFrontend = DIR_FRONTEND;
globalServerConfig.dirData = DIR_DATA;
globalServerConfig.dirCustomNodes = DIR_CUSTOM_NODES;
globalServerConfig.modulePythonUrl =
argv.module_python_url || "http://localhost:12619";
globalServerConfig.nodeRegistry = buildNodeRegistry(DIR_CUSTOM_NODES);
// Server setup
const appMain = new Koa();
const routerMain = new Router();
const portOpenAi: number = argv.port_openai || 5002;
// Setup a simple route to return the version of the app
routerMain.get("/app-version", (ctx) => {
ctx.body = { version: process.env.npm_package_version };
});
// Setup a simple route to ping external modules
routerMain.get("/ping-module/:module", async (ctx) => {
const available = await pingExternalModule(ctx.params.module as any);
ctx.status = available ? 200 : 400;
ctx.body = "";
});
// Setup internal APIs
setupResourcesApi(routerMain);
setupTranscriptionAPI(routerMain);
setupExecutorApi(routerMain, portOpenAi);
appMain
// body parsing
.use(koaBody({ jsonLimit: "10240gb" }))
// routing
.use(routerMain.routes())
.use(routerMain.allowedMethods())
// frontend
.use(serve(DIR_FRONTEND));
const port: number = argv.port || 12538;
void (async () => {
// Check for updates
try {
const latestVersion = await AppVersionUtils.getVersionFromGithub();
const currentVersion = process.env.npm_package_version;
if (`${latestVersion}` !== `${currentVersion}`) {
console.log("---");
console.log(
[
`A new update is available (v${currentVersion} => v${latestVersion}).`,
"To get the latest fixes and features, shut down the app",
"and run 'git pull' followed by 'npm install'.",
].join(" ")
);
console.log("---");
}
} catch (error) {
console.error("Failed to check for updates:", error);
}
})();
appMain.listen(port, () => {
console.log(
`OmniChain v${process.env.npm_package_version} started on http://localhost:${port}`
);
});