forked from usnistgov/ndn-dpdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.ts
67 lines (58 loc) · 1.6 KB
/
serve.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
import * as path from "node:path";
import { fileURLToPath } from "node:url";
import FastifyExpress from "@fastify/express";
import FastifyProxy from "@fastify/http-proxy";
import FastifyStatic from "@fastify/static";
import Fastify from "fastify";
import webpack from "webpack";
import devMiddleware from "webpack-dev-middleware";
import { env } from "./env";
export async function serve(port = 3333): Promise<void> {
const publicDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "public");
const compiler = webpack({
mode: "development",
devtool: "cheap-module-source-map",
entry: "./src/main.tsx",
module: {
rules: [
{
test: /\.tsx?$/,
exclude: /node_modules/,
loader: "ts-loader",
},
],
},
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
output: {
filename: "bundle.js",
path: publicDir,
},
});
const fastify = Fastify();
await fastify.register(FastifyExpress);
fastify.use(devMiddleware(compiler));
await fastify.register(FastifyStatic, { root: publicDir });
for (const u of [
{ upstream: env.F_GQLSERVER, prefix: "/F" },
{ upstream: env.A_GQLSERVER, prefix: "/A" },
{ upstream: env.B_GQLSERVER, prefix: "/B" },
]) {
await fastify.register(FastifyProxy, {
...u,
rewritePrefix: "/",
websocket: true,
});
}
fastify.get("/env.json", () => ({
...env,
F_GQLSERVER: "/F",
A_GQLSERVER: "/A",
B_GQLSERVER: env.A_GQLSERVER === env.B_GQLSERVER ? "/A" : "/B",
}));
await fastify.listen({
port,
host: "127.0.0.1",
});
}