From 84b86bb6a1fd19a3a5ac75664df16190294ddfc5 Mon Sep 17 00:00:00 2001
From: Nanobyte
Date: Sun, 21 Mar 2021 22:40:57 +0100
Subject: [PATCH] Make dumpDatabase configurable
---
config.json.example | 25 +++++++++++++++++++++++++
src/config.ts | 27 ++++++++++++++++++++++++++-
src/routes/dumpDatabase.ts | 37 ++++++++++++++-----------------------
3 files changed, 65 insertions(+), 24 deletions(-)
diff --git a/config.json.example b/config.json.example
index f8548c1..d859e4d 100644
--- a/config.json.example
+++ b/config.json.example
@@ -38,5 +38,30 @@
"max": 20, // 20 requests in 15min time window
"statusCode": 200
}
+ },
+ "dumpDatabase": {
+ "enabled": true,
+ "minTimeBetweenMs": 60000, // 1 minute between dumps
+ "exportPath": "/opt/exports",
+ "tables": [{
+ "name": "sponsorTimes",
+ "order": "timeSubmitted"
+ },
+ {
+ "name": "userNames"
+ },
+ {
+ "name": "categoryVotes"
+ },
+ {
+ "name": "noSegments",
+ },
+ {
+ "name": "warnings",
+ "order": "issueTime"
+ },
+ {
+ "name": "vipUsers"
+ }]
}
}
diff --git a/src/config.ts b/src/config.ts
index 93c6a43..477aa72 100644
--- a/src/config.ts
+++ b/src/config.ts
@@ -45,7 +45,32 @@ addDefaults(config, {
},
userCounterURL: null,
youtubeAPIKey: null,
- postgres: null
+ postgres: null,
+ dumpDatabase: {
+ enabled: true,
+ minTimeBetweenMs: 60000,
+ exportPath: '/opt/exports',
+ tables: [{
+ name: "sponsorTimes",
+ order: "timeSubmitted"
+ },
+ {
+ name: "userNames"
+ },
+ {
+ name: "categoryVotes"
+ },
+ {
+ name: "noSegments",
+ },
+ {
+ name: "warnings",
+ order: "issueTime"
+ },
+ {
+ name: "vipUsers"
+ }]
+ }
});
// Add defaults
diff --git a/src/routes/dumpDatabase.ts b/src/routes/dumpDatabase.ts
index d687f07..ec3e801 100644
--- a/src/routes/dumpDatabase.ts
+++ b/src/routes/dumpDatabase.ts
@@ -11,26 +11,13 @@ const licenseHeader = `The API and database follow Attribution Template
If you need to use the database or API in a way that violates this license, contact me with your reason and I may grant you access under a different license.
`;
-const tables = [{
- name: "sponsorTimes",
- order: "timeSubmitted"
-},
-{
- name: "userNames"
-},
-{
- name: "categoryVotes"
-},
-{
- name: "noSegments",
-},
-{
- name: "warnings",
- order: "issueTime"
-},
-{
- name: "vipUsers"
-}];
+const tables = config?.dumpDatabase?.tables ?? [];
+const MILLISECONDS_BETWEEN_DUMPS = config?.dumpDatabase?.minTimeBetweenMs ?? ONE_MINUTE;
+const exportPath = config?.dumpDatabase?.exportPath ?? '/opt/exports';
+
+if (tables.length === 0) {
+ Logger.warn('[dumpDatabase] No tables configured');
+}
const links: string[] = tables.map((table) => `/database/${table.name}.csv`);
@@ -40,13 +27,17 @@ const linksHTML: string = tables.map((table) => ` ONE_MINUTE;
+ const updateQueued = now - lastUpdate > MILLISECONDS_BETWEEN_DUMPS;
res.status(200)
@@ -72,7 +63,7 @@ export default function dumpDatabase(req: Request, res: Response, showPage: bool
for (const table of tables) {
db.prepare('run', `COPY (SELECT * FROM "${table.name}"${table.order ? ` ORDER BY "${table.order}"` : ``})
- TO '/opt/exports/${table.name}.csv' WITH (FORMAT CSV, HEADER true);`);
+ TO '${exportPath}/${table.name}.csv' WITH (FORMAT CSV, HEADER true);`);
}
}
-}
\ No newline at end of file
+}