-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
149 lines (121 loc) · 3.54 KB
/
app.js
File metadata and controls
149 lines (121 loc) · 3.54 KB
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
import fastify from "fastify";
import fastifyView from "@fastify/view";
import fastifyStatic from "@fastify/static";
import fastifyFormbody from "@fastify/formbody";
import { Liquid } from "liquidjs";
import esbuild from "esbuild";
import { JSONFilePreset } from "lowdb/node";
import fs from "node:fs";
import path from "node:path";
import url from "node:url";
import childProcess from "node:child_process";
import { randomUUID } from "node:crypto";
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const viewsPath = path.join(__dirname, "views");
const staticsPath = path.join(__dirname, "statics");
const publicPath = path.join(__dirname, "public");
const db = await JSONFilePreset(path.join(__dirname, "data", "data.json"), {
create: [],
consume: [],
});
async function buildClientsideAssets() {
if (!fs.existsSync(publicPath)) {
fs.mkdirSync(publicPath);
}
childProcess.execSync(
"npx tailwindcss -i ./statics/css/style.css -o ./public/css/style.css",
);
// build js bundle with esbuild
await esbuild
.build({
entryPoints: [path.join(staticsPath, "js", "main.js")],
bundle: true,
minify: true, // equivalent to webpack production mode
outfile: path.join(publicPath, "js", "main.js"),
platform: "browser",
external: ["fs", "path"],
})
.catch((error) => {
console.error(`Failed to build JS bundle. Error: ${error}`);
process.exit(1);
});
}
const routePrefix = process.env.WEEKNOTES_ROUTE_PREFIX ?? "";
const livePage = process.env.WEEKNOTES_LIVE_PAGE ?? "";
await buildClientsideAssets();
function getRoutePath(path = "") {
const basePath = routePrefix ? `/${routePrefix}` : "/";
if (!path) {
return basePath;
}
return routePrefix ? `/${routePrefix}/${path}` : `/${path}`;
}
const app = fastify({
routerOptions: {
ignoreTrailingSlash: true,
},
});
app.register(fastifyStatic, {
root: publicPath,
prefix: getRoutePath(),
});
app.register(fastifyView, {
engine: {
liquid: new Liquid({
root: viewsPath,
extname: ".liquid",
}),
},
});
app.register(fastifyFormbody);
app.get(getRoutePath(), (req, reply) => {
const links = db.data;
return reply.view("./views/index.liquid", {
links,
routePrefix,
livePage,
});
});
app.get(getRoutePath("links"), (req, reply) => {
const links = db.data;
return reply.send(links);
});
app.post(getRoutePath("create"), async (req, reply) => {
const { type, url, description } = req.body;
const uuid = randomUUID();
db.data[type].push({
id: uuid,
url,
description,
});
await db.write();
reply.redirect(getRoutePath());
});
app.post(getRoutePath("delete"), async (req, reply) => {
let inCreateIdx = db.data.create.findIndex(({ id }) => req.body.id === id);
if (inCreateIdx !== -1) {
db.data.create.splice(inCreateIdx, 1);
await db.write();
return reply.send({ message: "link deleted" });
}
let inConsumeIdx = db.data.consume.findIndex(({ id }) => req.body.id === id);
if (inConsumeIdx !== -1) {
db.data.consume.splice(inConsumeIdx, 1);
await db.write();
return reply.send({ message: "link deleted" });
}
});
app.post(getRoutePath("delete-all"), async (req, reply) => {
db.data.create = [];
db.data.consume = [];
await db.write();
return reply.send({ message: "Successfully deleted all posts" });
});
app.listen({ port: 3007, host: "0.0.0.0" }, (err, address) => {
if (err) {
console.error(err);
process.exit(1);
}
console.log(`Server is running on ${address}/${routePrefix}`);
});