generated from thepurplebubble/template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
64 lines (54 loc) · 1.4 KB
/
index.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
import "dotenv/config";
import Express, { request } from "express";
import bodyParser from "body-parser";
import { createClient } from "redis";
import { scheduleJobs } from "./jobs.js";
import {
syncServers,
syncHashes,
storeMessage,
searchByRecipient,
searchByHash,
} from "./util.js";
const express = new Express();
express.use(bodyParser.json());
export const redis = createClient({
url: process.env["REDIS_URL"],
});
redis.on("error", (err) => console.error("Redis Client Error", err));
await redis.connect();
scheduleJobs();
// TODO: more error handling and HTTP response codes
// TODO: typescript
express.post("/fetch", (req, res) => {
if (req.body.recipient) {
res.json({
messages: searchByRecipient(req.body.recipient),
});
} else if (req.body.hashes) {
messages = [];
req.body.hashes.forEach((hash) => {
messages.add(searchByHash(hash));
});
res.json({
messages: messages,
});
} else {
res.status(400).send("400 Bad Request");
}
});
express.post("/send", (req, res) => {
storeMessage(req.body);
res.send("200 OK");
});
express.post("/sync/servers", (req, res) => {
res.json(syncServers(req.body.servers));
});
express.post("/sync/hashes", (req, res) => {
res.json(syncHashes(req.body.hashes));
});
express.listen(process.env["PORT"], () => {
console.log(
`Purple Bubble Server is now listening on port ${process.env["PORT"]}`
);
});