-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.js
More file actions
100 lines (87 loc) · 2.83 KB
/
server.js
File metadata and controls
100 lines (87 loc) · 2.83 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
const express = require("express");
const next = require("next");
const fetch = require("node-fetch");
const cors = require("cors");
const bodyParser = require("body-parser");
const redis = require("redis");
const asyncRedis = require("async-redis");
const bcrypt = require("bcrypt");
const port = 2000;
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
server.use(cors());
server.use(bodyParser.json());
const syncRedisClient = redis.createClient();
const redisClient = asyncRedis.decorate(syncRedisClient);
redisClient.on("connect", () => {
console.log("Redis client connected");
});
redisClient.on("error", err => {
console.log(`Something went wrong ${err}`);
});
server.get("/editor/:id?", (req, res) => {
const { id } = req.params;
return app.render(req, res, "/editor", { id, json: !id && {} });
});
server.get("/", (req, res) => {
return app.render(req, res, "/editor", { id: req.params.id });
});
server.use("/api/save", async (req, res) => {
const { json, password } = req.body;
let { id } = req.body;
if (!id) {
id = Math.random()
.toString(36)
.substring(2, 15);
}
const passHash = await redisClient.get("passwd-" + id);
if (passHash) {
if (!password || !bcrypt.compareSync(password, passHash)) {
return res.status(401).json({
error: password
? "Not authorized, incorrect password"
: "Not authorized, password needed"
});
}
} else if (password) {
const hash = bcrypt.hashSync(password, 10);
await redisClient.set("passwd-" + id, hash);
}
await redisClient.incr("save-" + id);
await redisClient.set(id, JSON.stringify(json));
return res.json({ id, json });
});
server.get("/api/:id", async (req, res) => {
const { id } = req.params;
const cached = await redisClient.get(id);
if (cached) {
const passHash = await redisClient.get("passwd-" + id);
const json = JSON.parse(cached);
if (passHash) {
res.set("x-protected", true);
}
if (!req.headers["x-skip-incr"]) {
await redisClient.incr("get-" + id);
}
return res.json(json);
}
// if (id === "f87al12d83w") {
// const data = await fetch(
// "https://jsonblob.com/api/jsonBlob/ba4226ac-d21a-11e8-88b0-8176dc2ca97e"
// ).then(r => r.json());
// redisClient.set(id, JSON.stringify(data), "EX", 30); // 30 seconds
// return res.json(data);
// }
return res.status(404).json({ error: "JSON not found" });
});
server.get("*", (req, res) => {
return handle(req, res);
});
server.listen(port, err => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
});