feat (restful): Add /api/utils/refresh

The API call does the following:
- Fetch GitHub avatar and update artifact store url
- Revoke all cached resources
This commit is contained in:
Peng-YM 2022-07-11 23:06:40 +08:00
parent 6881148021
commit 2c4e47166d
8 changed files with 37 additions and 14 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

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

View File

@ -8,4 +8,4 @@ export const GIST_BACKUP_KEY = 'Auto Generated Sub-Store Backup';
export const GIST_BACKUP_FILE_NAME = 'Sub-Store'; export const GIST_BACKUP_FILE_NAME = 'Sub-Store';
export const ARTIFACT_REPOSITORY_KEY = 'Sub-Store Artifacts Repository'; export const ARTIFACT_REPOSITORY_KEY = 'Sub-Store Artifacts Repository';
export const RESOURCE_CACHE_KEY = '#sub-store-cached-resource'; export const RESOURCE_CACHE_KEY = '#sub-store-cached-resource';
export const CACHE_EXPIRATION_TIME_MS = 5 * 60 * 1000; export const CACHE_EXPIRATION_TIME_MS = 60 * 60 * 1000; // 1 hour

View File

@ -14,11 +14,15 @@ import registerSubscriptionRoutes from './subscriptions';
import registerCollectionRoutes from './collections'; import registerCollectionRoutes from './collections';
import registerArtifactRoutes from './artifacts'; import registerArtifactRoutes from './artifacts';
import registerDownloadRoutes from './download'; import registerDownloadRoutes from './download';
import registerSettingRoutes from './settings'; import registerSettingRoutes, {
updateArtifactStore,
updateGitHubAvatar,
} from './settings';
import registerPreviewRoutes from './preview'; import registerPreviewRoutes from './preview';
import registerSortingRoutes from './sort'; import registerSortingRoutes from './sort';
import { failed, success } from '@/restful/response'; import { failed, success } from '@/restful/response';
import { InternalServerError, RequestInvalidError } from '@/restful/errors'; import { InternalServerError, RequestInvalidError } from '@/restful/errors';
import resourceCache from '@/utils/resource-cache';
export default function serve() { export default function serve() {
const $app = express({ substore: $ }); const $app = express({ substore: $ });
@ -36,6 +40,7 @@ export default function serve() {
$app.get('/api/utils/IP_API/:server', IP_API); // IP-API reverse proxy $app.get('/api/utils/IP_API/:server', IP_API); // IP-API reverse proxy
$app.get('/api/utils/env', getEnv); // get runtime environment $app.get('/api/utils/env', getEnv); // get runtime environment
$app.get('/api/utils/backup', gistBackup); // gist backup actions $app.get('/api/utils/backup', gistBackup); // gist backup actions
$app.get('/api/utils/refresh', refresh);
// Storage management // Storage management
$app.route('/api/storage') $app.route('/api/storage')
@ -84,6 +89,16 @@ function getEnv(req, res) {
}); });
} }
async function refresh(_, res) {
// 1. get GitHub avatar and artifact store
await updateGitHubAvatar();
await updateArtifactStore();
// 2. clear resource cache
resourceCache.revokeAll();
success(res);
}
async function gistBackup(req, res) { async function gistBackup(req, res) {
const { action } = req.query; const { action } = req.query;
// read token // read token

View File

@ -28,7 +28,7 @@ async function updateSettings(req, res) {
success(res, newSettings); success(res, newSettings);
} }
async function updateGitHubAvatar() { export async function updateGitHubAvatar() {
const settings = $.read(SETTINGS_KEY); const settings = $.read(SETTINGS_KEY);
const username = settings.githubUser; const username = settings.githubUser;
if (username) { if (username) {
@ -50,7 +50,7 @@ async function updateGitHubAvatar() {
} }
} }
async function updateArtifactStore() { export async function updateArtifactStore() {
$.log('Updating artifact store'); $.log('Updating artifact store');
const settings = $.read(SETTINGS_KEY); const settings = $.read(SETTINGS_KEY);
const { githubUser, gistToken } = settings; const { githubUser, gistToken } = settings;

View File

@ -25,6 +25,14 @@ class ResourceCache {
if (clear) this._persist(); if (clear) this._persist();
} }
revokeAll() {
Object.keys(this.resourceCache).forEach((id) => {
$.delete(`#${id}`);
});
this.resourceCache = {};
this._persist();
}
_persist() { _persist() {
$.write(JSON.stringify(this.resourceCache), RESOURCE_CACHE_KEY); $.write(JSON.stringify(this.resourceCache), RESOURCE_CACHE_KEY);
} }

File diff suppressed because one or more lines are too long