-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
208 lines (188 loc) · 5.38 KB
/
Copy pathserver.js
File metadata and controls
208 lines (188 loc) · 5.38 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const express = require("express");
const app = express();
const cors = require("cors");
// const {createClient} = require("@supabase/supabase-js");
const PORT = process.env.PORT || 3000;
// const supabase = createClient(
// process.env.SUPABASE_URL,
// process.env.SUPABASE_KEY
// );
const GOOGLE_SHEETS_URL = process.env.GOOGLE_SHEETS_URL;
app.use(cors());
app.use(express.json({limit: '50kb'}));
if (process.env.RENDER) { // if it runs on render (and not locally)
const APP_URL = process.env.RENDER_EXTERNAL_URL;
const INTERVAL_MS = 14 * 60 * 1000; // ping every 14 minutes
async function ping() {
await fetch(APP_URL); // ping the server (don't care for the response)
console.log(`${new Date().toISOString()}: Pinged the server`);
}
setInterval(ping, INTERVAL_MS);
}
const formCache = {};
const CACHE_TTL = 5 * 60 * 1000;
function setCache(id, data) {
formCache[id] = {
...data,
expiresAt: Date.now() + CACHE_TTL
};
}
function getCache(id) {
const entry = formCache[id];
if (!entry) return null;
if (Date.now() > entry.expiresAt) {
delete formCache[id];
return null;
}
return entry;
}
/*
TODO:
1. once in a while, send an empty request to the db, to reduce downtime
2. change the method names
3. add helper methods (getCurrentFormVersion, isId / version valid...)
get:
1. the form id and version
2. a JSON of key-value (possible to make it without the key, and just ordered...)
*/
app.post('/upload-submission', async (req, res) => {
// const {form_id, form_version, submission} = req.body;
// if (!form_id || !submission || !form_version) {
// return res.status(400).json({error: "Invalid request"});
// }
// const {error} = await supabase
// .from("submissions")
// .insert({form_id, form_version, submission});
//
// if (error) {
// return res.status(500).json({error: error.message});
// }
try {
const response = await fetch(GOOGLE_SHEETS_URL, {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({
data: req.body
})
});
console.log(response);
} catch (err) {
console.error("Failed to send to Google Sheets:", err.message);
}
res.sendStatus(201);
});
// app.post('/upload-form', async (req, res) => {
// const {id, form, version} = req.body;
//
// if (!id || typeof version !== "number") {
// return res.status(400).json({error: "Invalid request, make sure version is a number"});
// }
//
// // fetch current version
// const {data, error} = await supabase
// .from("forms")
// .select("version")
// .eq("id", id)
// .single();
//
// if (error) {
// return res.status(500).json({error: error.message});
// }
//
// // make a new form if form doesn't exist
// if (!data) {
// const {error: insertError} = await supabase
// .from("forms")
// .insert({
// id,
// form,
// version: 1
// });
//
// if (insertError) {
// return res.status(500).json({error: insertError.message});
// }
//
// return res.status(201).json({version: 1});
// }
//
// // if version conflict, send the latest version
// if (data.version !== version) {
// return res.status(409).json({
// currentVersion: data.version
// });
// }
//
// // update the version
// const newVersion = version + 1;
// setCache(id, {form, version: newVersion})
//
// const {error: updateError} = await supabase
// .from("forms")
// .update({
// form,
// version: newVersion,
// })
// .eq("id", id);
//
// if (updateError) {
// return res.status(500).json({error: updateError.message});
// }
//
// return res.status(200).json({version: newVersion});
// });
// app.get('/get-form/:id/:version', async (req, res) => {
// const {id, version} = req.params;
// const clientVersion = Number(version);
//
// const cache = getCache(id);
// if (cache) {
//
// if (cache.version === clientVersion) {
// console.log("not modified");
// return res.sendStatus(304);
// }
//
// console.log("serving from cache")
// return res.status(200).json({
// form: cache.form,
// version: cache.version
// })
// }
//
// // fetch version
// console.time("request");
// const {data, error} = await supabase
// .from("forms")
// .select("form, version")
// .eq("id", id)
// .single();
// console.timeEnd("request");
//
// if (error) {
// return res.status(500).json({error: error.message});
// }
//
// if (!data) {
// return res.sendStatus(404);
// }
//
// setCache(id, {
// form: data.form,
// version: data.version
// });
//
// if (data.version === clientVersion) {
// return res.sendStatus(304);
// }
//
// console.log(data.version)
//
// return res.status(200).json({
// form: data.form,
// version: data.version
// });
// })
app.listen(PORT, () => {
console.log(`Server running on http://0.0.0.0:${PORT}`);
});