-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
150 lines (141 loc) · 3.71 KB
/
app.js
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
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import fs from "node:fs/promises";
import { createServer } from "node:http";
import Fastify from "fastify";
import fastifyStatic from "@fastify/static";
import wisp from "wisp-server-node";
import { baremuxPath } from "@mercuryworkshop/bare-mux/node";
import { epoxyPath } from "@mercuryworkshop/epoxy-transport";
import { libcurlPath } from "@mercuryworkshop/libcurl-transport";
import { bareModulePath } from "@mercuryworkshop/bare-as-module3";
import config from "./config.js";
const fastify = Fastify({
serverFactory: handler => {
return createServer()
.on("request", (req, res) => {
handler(req, res);
})
.on("upgrade", (req, socket, head) => {
wisp.routeRequest(req, socket, head);
});
},
logger: true
});
async function aeroExamplesServe(fastify) {
fastify.get("/sw.js", async (_req, reply) => {
try {
const swFilePath = join(
fileURLToPath(new URL(".", import.meta.url)),
"../examples/swWithExtras.js"
);
const swFile = await fs.readFile(swFilePath, "utf8");
reply
.code(200)
.header("Content-Type", "application/javascript")
.send(swFile);
} catch (err) {
reply.code(500).send(err);
}
});
fastify.get("/aero/defaultConfig.js", async (_req, reply) => {
try {
const configFilePath = join(
fileURLToPath(new URL(".", import.meta.url)),
"../src/defaultConfig.js"
);
const configFile = await fs.readFile(configFilePath, "utf8");
reply
.code(200)
.header("Content-Type", "application/javascript")
.send(configFile);
} catch (err) {
reply.code(500).send(err);
}
});
fastify.get("/aero/config.js", async (_req, reply) => {
try {
const configFilePath = join(
fileURLToPath(new URL(".", import.meta.url)),
"../examples/config.js"
);
const configFile = await fs.readFile(configFilePath, "utf8");
reply
.code(200)
.header("Content-Type", "application/javascript")
.send(configFile);
} catch (err) {
reply.code(500).send(err);
}
});
fastify.get("/aero/sandbox/config.js", async (_req, reply) => {
try {
const configFilePath = join(
fileURLToPath(new URL(".", import.meta.url)),
"../src/AeroSandbox/examples/config.js"
);
const configFile = await fs.readFile(configFilePath, "utf8");
reply
.code(200)
.header("Content-Type", "application/javascript")
.send(configFile);
} catch (err) {
reply.code(500).send(err);
}
});
}
fastify.register(aeroExamplesServe);
fastify.register(fastifyStatic, {
root: join(fileURLToPath(new URL(".", import.meta.url)), "../tests"),
prefix: "/tests",
decorateReply: false
});
fastify.register(fastifyStatic, {
root: join(fileURLToPath(new URL(".", import.meta.url)), "../extras"),
prefix: "/aero/extras/",
decorateReply: false
});
fastify.register(fastifyStatic, {
root: join(
fileURLToPath(new URL(".", import.meta.url)),
"../src/AeroSandbox/dist/debug/"
),
prefix: "/aero/sandbox/",
decorateReply: false
});
fastify.register(fastifyStatic, {
root: join(
fileURLToPath(new URL(".", import.meta.url)),
"../dist/debug/sw"
),
prefix: "/aero/",
decorateReply: false
});
fastify.register(fastifyStatic, {
root: join(fileURLToPath(new URL(".", import.meta.url)), "./demo-site"),
decorateReply: false
});
fastify.register(fastifyStatic, {
root: baremuxPath,
prefix: "/baremux/",
decorateReply: false
});
fastify.register(fastifyStatic, {
root: epoxyPath,
prefix: "/epoxy/",
decorateReply: false
});
fastify.register(fastifyStatic, {
root: libcurlPath,
prefix: "/libcurl/",
decorateReply: false
});
fastify.register(fastifyStatic, {
root: bareModulePath,
prefix: "/baremod/",
decorateReply: false
});
fastify.listen({
host: config.host,
port: config.port
});