-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
85 lines (75 loc) · 2.51 KB
/
server.js
File metadata and controls
85 lines (75 loc) · 2.51 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
const http = require("http");
const fs = require("fs");
const path = require("path");
const PORT = 3000;
const ROOT = __dirname;
const parseEnv = () => {
const envPath = path.join(ROOT, ".env");
if (!fs.existsSync(envPath)) return {};
const lines = fs.readFileSync(envPath, "utf8").split(/\r?\n/);
const env = {};
for (const line of lines) {
if (!line || line.trim().startsWith("#")) continue;
const idx = line.indexOf("=");
if (idx === -1) continue;
const key = line.slice(0, idx).trim();
const value = line.slice(idx + 1).trim();
env[key] = value;
}
return env;
};
const getContentType = (filePath) => {
const ext = path.extname(filePath).toLowerCase();
if (ext === ".html") return "text/html; charset=utf-8";
if (ext === ".css") return "text/css; charset=utf-8";
if (ext === ".js") return "text/javascript; charset=utf-8";
if (ext === ".json") return "application/json; charset=utf-8";
if (ext === ".png") return "image/png";
if (ext === ".jpg" || ext === ".jpeg") return "image/jpeg";
if (ext === ".gif") return "image/gif";
if (ext === ".svg") return "image/svg+xml";
if (ext === ".webp") return "image/webp";
return "application/octet-stream";
};
const server = http.createServer((req, res) => {
const url = req.url.split("?")[0];
if (url === "/env.json") {
const env = parseEnv();
const payload = {
SUPABASE_URL: env.SUPABASE_URL || "",
SUPABASE_ANON_KEY: env.SUPABASE_ANON_KEY || env.SUPABASE_PUBLISHABLE_KEY || "",
};
res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
res.end(JSON.stringify(payload));
return;
}
if (url === "/env.js" || url === "/api/env.js") {
const env = parseEnv();
const payload = {
SUPABASE_URL: env.SUPABASE_URL || "",
SUPABASE_ANON_KEY: env.SUPABASE_ANON_KEY || env.SUPABASE_PUBLISHABLE_KEY || "",
};
res.writeHead(200, { "Content-Type": "text/javascript; charset=utf-8" });
res.end(`window.__ENV = ${JSON.stringify(payload)};`);
return;
}
const safePath = url === "/" ? "/index.html" : url;
const filePath = path.join(ROOT, safePath);
if (!filePath.startsWith(ROOT)) {
res.writeHead(403);
res.end("Forbidden");
return;
}
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404);
res.end("Not found");
return;
}
res.writeHead(200, { "Content-Type": getContentType(filePath) });
res.end(data);
});
});
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});