feat: 环境变量 SUB_STORE_DATA_URL 启动时自动从此地址拉取并恢复数据

This commit is contained in:
xream 2023-12-13 09:54:57 +08:00
parent e05475aa5e
commit ce6cd794c8
No known key found for this signature in database
GPG Key ID: 1D2C5225471789F9
2 changed files with 22 additions and 1 deletions

View File

@ -1,6 +1,6 @@
{
"name": "sub-store",
"version": "2.14.126",
"version": "2.14.127",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js",
"scripts": {

View File

@ -1,5 +1,7 @@
import express from '@/vendor/express';
import $ from '@/core/app';
import migrate from '@/utils/migration';
import download from '@/utils/download';
import registerSubscriptionRoutes from './subscriptions';
import registerCollectionRoutes from './collections';
@ -41,6 +43,7 @@ export default function serve() {
if ($.env.isNode) {
const path = eval(`require("path")`);
const fs = eval(`require("fs")`);
const data_url = eval('process.env.SUB_STORE_DATA_URL');
const fe_be_path = eval('process.env.SUB_STORE_FRONTEND_BACKEND_PATH');
const fe_port = eval('process.env.SUB_STORE_FRONTEND_PORT') || 3001;
const fe_host =
@ -133,5 +136,23 @@ export default function serve() {
}
});
}
if (data_url) {
$.info(`[BACKEND] downloading data from ${data_url}`);
download(data_url)
.then((content) => {
$.write(content, '#sub-store');
$.cache = JSON.parse(content);
$.persistCache();
migrate();
$.info(`[BACKEND] restored data from ${data_url}`);
})
.catch((e) => {
$.error(`[BACKEND] restore data failed`);
console.error(e);
throw e;
});
}
}
}