-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.mjs
90 lines (83 loc) · 2.48 KB
/
index.mjs
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
import fastify from "fastify";
import got from "got";
import "dotenv/config";
import fs from "fs/promises";
import etag from "@fastify/etag";
import staticPlugin from "@fastify/static";
import path from "path";
import { fileURLToPath } from 'url';
import { css2 as css2Next } from "./css2-next.mjs";
import { css2 as css2Old } from "./css2.mjs";
import { css } from "./css.mjs";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const domain = process.env.DOMAIN ?? "localhost";
if (!domain) {
throw new Error("Please set DOMAIN in .env file.");
}
const server = fastify();
// Register plugins
server.register(etag);
server.register(staticPlugin, {
root: path.join(__dirname),
prefix: '/', // optional: default '/'
});
const base = "gwfh.mranftl.com";
const data = await got.get(`https://${base}/api/fonts/`).json();
const subsets = JSON.parse(await fs.readFile("./subsets.json", "utf8"));
server.get("/", (response, reply) => {
if (process.env.NODE_ENV === "development") {
return reply.code(200).send("Hello from DEV.");
}
reply.redirect("https://fonts.coollabs.io");
});
server.get("/icon", async (request, reply) => {
const css = `/* fallback */
@font-face {
font-family: 'Material Icons';
font-style: normal;
font-weight: 400;
src: url(https://cdn.fonts.coollabs.io/icons/material-icons/v125.woff2) format('woff2');
}
.material-icons {
font-family: 'Material Icons';
font-weight: normal;
font-style: normal;
font-size: 24px;
line-height: 1;
letter-spacing: normal;
text-transform: none;
display: inline-block;
white-space: nowrap;
word-wrap: normal;
direction: ltr;
-webkit-font-feature-settings: 'liga';
-webkit-font-smoothing: antialiased;
}`;
reply.header("content-type", "text/css");
reply.send(css);
return reply.end();
});
server.get("/css", async (request, reply) => {
return css(request, reply, data, domain, subsets);
});
server.get("/css2", async (request, reply) => {
return css2Next(request, reply, data, domain, subsets);
});
server.get("/css2-next", async (request, reply) => {
return css2Next(request, reply, data, domain, subsets);
});
if (process.env.NODE_ENV === "development") {
server.get("/demo", async (request, reply) => {
return reply.sendFile("./demo.html");
});
}
try {
await server.listen({ port: 3000, host: "0.0.0.0" });
console.log(
`Server listening on http://0.0.0.0:${server.server.address().port}`
);
} catch (err) {
server.log.error(err);
process.exit(1);
}