forked from hackclub/blot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
133 lines (109 loc) · 3.37 KB
/
server.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
// should be top directory of project
import express from 'express';
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { build } from "./build.js";
import { wrapHTML } from "./backend/wrapHTML.js";
import navBar from "./backend/pages/navBar.js";
import guides from "./backend/pages/guides.js";
import gallery from "./backend/pages/gallery.js";
import landing from "./backend/pages/landing.js";
import docs from "./backend/pages/docs.js";
import signUpEmail from "./backend/api/signUpEmail.js";
import checkSignIn from "./backend/api/checkSignIn.js";
import saveFile from "./backend/api/saveFile.js";
import getUser from "./backend/api/getUser.js";
import submitCode from "./backend/api/submitCode.js";
import getFiles from "./backend/api/getFiles.js";
import createShareLink from "./backend/api/createShareLink.js";
import { supabase } from "./backend/api/supabase.js";
build({
index: wrapHTML(`
${navBar(true)}
${landing()}
`),
docs: wrapHTML(`
${navBar()}
${docs()}
`),
editor: wrapHTML(`
<!-- TODO: add automatically when building -->
<link rel="stylesheet" href='./assets/initApp.css'>
<link rel="stylesheet" href='https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css'>
<main></main>
<script type="module" src="./src/initApp.js"></script>
`),
guides: wrapHTML(`
${navBar()}
${guides()}
`),
gallery: wrapHTML(`
${navBar()}
${gallery()}
`),
assembly: wrapHTML(`
${navBar()}
Go <a class="underline decoration-sky-500" href="https://github.com/hackclub/blot/blob/main/docs/ASSEMBLY.md">here</a>.
`),
404: wrapHTML(`
${navBar()}
<div class="p-2">nothing here</div>
`),
test: wrapHTML(`
${navBar()}
<div class="bg-red-400">test another page</div>
`),
});
const app = express();
const port = process.env.PORT || 3000;
app.use(express.json());
app.post('/signUpEmail', signUpEmail);
app.post('/check-signed-in', checkSignIn);
app.post('/get-files', getFiles);
app.post('/save-file', saveFile);
app.post('/get-user', getUser);
app.post('/submit-code', submitCode);
app.post('/create-share-link', createShareLink);
app.get('/read-share-link', async (req, res) => {
const { id } = req.query;
try {
console.log("check in database");
// check if email is in database
let { data: file, error: fileError } = await supabase
.from('share_link')
.select('*')
.eq('id', id)
.single();
if (fileError && fileError.message !== 'No rows found') {
res.send("no share link here");
return;
}
res.send(file.content);
} catch (error) {
res.status(500).send({ error: error.message });
}
});
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
app.use(express.static(join(__dirname, 'dist')));
app.use((req, res, next) => {
let pathName = req.path;
if (pathName === "/") pathName = "/index";
if (req.path.indexOf('.') === -1) {
const file = path.join(__dirname, 'dist', `${pathName}.html`);
res.sendFile(file, (err) => {
if (err) next();
});
} else {
next();
}
});
app.use((req, res, next) => {
const file = path.join(__dirname, 'dist', `404.html`);
res.status(404).sendFile(file);
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});