-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
96 lines (83 loc) · 2.41 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
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
import compression from "compression";
import cors from "cors";
import { CronJob } from "cron";
import express from "express";
import helmet from "helmet";
import { readFile } from "node:fs";
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { getAllProducts } from "./getAllProducts.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const productIdMap = {};
const idMap = (products) => {
for (const product of products) {
productIdMap[product.productNumber] = product;
}
console.log("IdMap created");
};
readFile("data/products.json", function read(err, data) {
if (!err && data) {
console.log("Products.json found, no need to fetch");
idMap(JSON.parse(data));
} else if (err.code === "ENOENT") {
console.log("Products.json not found, this takes a while...");
getAllProducts(productIdMap).then((products) => {
idMap(products);
});
} else {
console.log("Error with products.json: ", err.code);
}
});
const app = express();
const port = process.env.PORT || 3000;
app.use(compression());
app.use(helmet());
app.use(cors());
app.get("/v1/products", (_req, res) => {
// print who IP of the request
// const ip = req.headers["x-forwarded-for"] || req.socket.remoteAddress;
// console.log(`Request from ${ip}`);
res.sendFile(`${__dirname}/data/products.json`);
});
app.get("/v1/products/secret", (_req, res) => {
// just used for some testing
res.sendFile(`${__dirname}/data/products.json`);
});
app.get("/v1/products/updated", (_req, res) => {
res.sendFile(`${__dirname}/data/updated.json`);
});
app.get("/v1/product/:id", (req, res) => {
const result = productIdMap[req.params.id];
if (result) {
res.json(result);
} else {
res.status(404).json({ error: "404, product not found" });
}
// res.json(productIdMap[req.params.id] || { "error": "Product not found"})
});
app.get("/", (_req, res) => {
res.redirect("v1/products");
});
app.get("*", (_req, res) => {
res
.status(404)
.send(
"404, page not found. See <a href='https://github.com/C4illin/systembolaget-data'>https://github.com/C4illin/systembolaget-data</a> for documentation.",
);
});
app.listen(port, () => {
console.log(`Listening at http://localhost:${port}`);
});
new CronJob(
"0 0 3 * * *",
() => {
console.log("Updating ALL products");
getAllProducts(productIdMap).then((products) => {
idMap(products);
});
},
null,
true,
"Europe/Stockholm",
);