1import express from 'express';
2import * as tests from '../controller/speedtests.js';
3import * as config from '../controller/config.js';
4import password from '../middlewares/password.js';
5
6const app = express.Router();
7
8app.get("/", password(false), async (req, res) => {
9 res.json(await config.getUsedStorage());
10});
11
12app.get("/tests/history/json", password(false), async (req, res) => {
13 res.set({"Content-Disposition": "attachment; filename=\"speedtests.json\""});
14 res.send(JSON.stringify(await tests.listAll(), null, 4));
15});
16
17app.get("/tests/history/csv", password(false), async (req, res) => {
18 res.set({"Content-Disposition": "attachment; filename=\"speedtests.csv\""});
19 let list = await tests.listAll();
20
21 if (list.length === 0) return res.send("");
22 let fields = Object.keys(list[0]);
23
24 let replacer = (key, value) => value === null ? '' : value;
25
26 let csv = list.map(row => fields.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','));
27 csv.unshift('"' + fields.join('","') + '"');
28 csv = csv.join('\r\n');
29
30 res.send(csv);
31});
32
33app.delete("/tests/history", password(false), async (req, res) => {
34 let result = await tests.deleteTests();
35 res.status(result ? 200 : 500).json({message: result ? "Tests deleted" : "Error deleting tests"});
36});
37
38app.put("/tests/history", password(false), async (req, res) => {
39 if (process.env.PREVIEW_MODE === "true")
40 return res.status(403).json({message: "You can't import the tests in preview mode"});
41
42 let result = await tests.importTests(req.body);
43 res.status(result ? 200 : 500).json({message: result ? "Tests imported" : "Error importing tests"});
44});
45
46app.get("/config", password(false), async (req, res) => {
47 res.set({"Content-Disposition": "attachment; filename=\"config.json\""});
48 config.exportConfig().then(obj => res.json(obj));
49});
50
51app.put("/config", password(false), async (req, res) => {
52 if (process.env.PREVIEW_MODE === "true")
53 return res.status(403).json({message: "You can't import the config in preview mode"});
54 let result = await config.importConfig(req.body);
55 res.status(result ? 200 : 500).json({message: result ? "Config imported" : "Error importing config"});
56});
57
58app.delete("/config", password(false), async (req, res) => {
59 let result = await config.factoryReset();
60 res.status(result ? 200 : 500).json({message: result ? "Config reset" : "Error resetting config"});
61});
62
63export default app;