import { UserApp } from '@/module/get-user-app.ts'; import { app } from '../../app.ts'; import { redis } from '@/module/redis/redis.ts'; import fs from 'fs'; import { fileStore } from '../../module/config.ts'; app .route({ path: 'app', key: 'auth-admin', }) .define(async (ctx) => { const { user } = ctx.query; if (user !== 'admin') { ctx.throw('Not Found'); } }) .addTo(app); app .route({ path: 'app', key: 'list', middleware: ['auth-admin'], }) .define(async (ctx) => { const keys = await redis.keys('user:app:*'); // const keys = await redis.keys('user:app:exist:*'); // const data = await redis.mget(...keys); ctx.body = { // data: data, keys, }; }) .addTo(app); app .route({ path: 'app', key: 'delete', middleware: ['auth-admin'], }) .define(async (ctx) => { const { user, app } = ctx.query; try { const userApp = new UserApp({ user, app }); await userApp.clearCacheData(); } catch (error) { console.error(error); ctx.throw('删除失败'); } ctx.body = 'successfully'; }) .addTo(app); app .route({ path: 'app', key: 'deleteAll', }) .define(async (ctx) => { const keys = await redis.keys('user:app:*'); for (const key of keys) { await redis.set(key, '', 'EX', 1); } ctx.body = { keys, }; }) .addTo(app); app .route({ path: 'app', key: 'clear', }) .define(async (ctx) => { const keys = await redis.keys('user:app:*'); if (keys.length > 0) { await redis.del(...keys); } fs.rmSync(fileStore, { recursive: true }); ctx.body = { keys, }; }) .addTo(app); app .route({ path: 'app', key: 'get', middleware: ['auth-admin'], }) .define(async (ctx) => { const { user, app } = ctx.query; if (!user || !app) { if (!user) { ctx.throw('user is required'); } if (!app) { ctx.throw('app is required'); } } const userApp = new UserApp({ user, app }); const cache = await userApp.getCache(); if (!cache) { ctx.throw('Not Found App'); } ctx.body = cache; }) .addTo(app); app .route({ path: 'app', key: 'status', middleware: ['auth-admin'], }) .define(async (ctx) => { const { user, app } = ctx.query; const userApp = new UserApp({ user, app }); const status = await userApp.getLoaded(); ctx.body = status; }) .addTo(app);