-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
91 lines (80 loc) · 2.49 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { dirname, Hono, honoLogger, ky, parse, serve } from "./deps.ts";
import dictProvider from "./providers/dict.ts";
import infoProvider from "./providers/info.ts";
import noopProvider from "./providers/noop.ts";
import synthesisProvider from "./providers/synthesis.ts";
import { saveStore, store } from "./store.ts";
const args = parse(Deno.args, {
string: ["host", "port", "originalUrl"],
default: {
host: "127.0.0.1",
port: "50132",
originalUrl: "http://127.0.0.1:50032",
},
});
const baseClient = ky.create({
prefixUrl: args.originalUrl,
});
if (store.enginePath != undefined) {
if (await baseClient.get("").catch(() => null)) {
console.log("The server is already running, not starting the engine.");
} else if (Deno.stat(store.enginePath).catch(() => null) == undefined) {
console.log(
`The engine path ${store.enginePath} does not exist, not starting the engine.`,
);
store.enginePath = undefined;
} else {
console.log(`Starting the engine at ${store.enginePath}...`);
const process = new Deno.Command(
store.enginePath,
{
stdout: "inherit",
stderr: "inherit",
},
).spawn();
self.addEventListener("unload", () => {
process.kill();
});
}
} else {
console.log("No engine path, not starting the engine.");
}
while (true) {
try {
await baseClient.get("");
break;
} catch {
console.log("Waiting for the server to be ready...");
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
const speakers: {
speakerUuid: string;
}[] = await baseClient.get("v1/speakers").json();
const path: {
speakerFolderPath: string;
} = await baseClient.get("v1/speaker_folder_path", {
searchParams: {
speakerUuid: speakers[0].speakerUuid,
},
}).json();
const speakerFolderPath = path.speakerFolderPath;
const enginePath = dirname(dirname(speakerFolderPath)) + "/engine/engine.exe";
console.log(`Engine path: ${enginePath}`);
store.enginePath = enginePath;
await saveStore();
const app = new Hono();
app.use("*", honoLogger());
app.use("*", async (c, next) => {
await next();
c.res.headers.set("Access-Control-Allow-Origin", "*");
c.res.headers.set("Access-Control-Allow-Headers", "*");
c.res.headers.set("Access-Control-Allow-Methods", "*");
});
app.options("*", (c) => {
return c.text("", 200);
});
[infoProvider, noopProvider, synthesisProvider, dictProvider].forEach((
provider,
) => provider({ baseClient, app }));
serve(app.fetch, { hostname: args.host, port: parseInt(args.port) });