refactor: feature/redis

This commit is contained in:
unitwk 2023-01-23 22:45:19 +08:00
parent 3672e016f9
commit e0a22251cf
4 changed files with 41 additions and 44 deletions

23
package-lock.json generated
View File

@ -221,14 +221,12 @@
"@redis/bloom": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@redis/bloom/-/bloom-1.1.0.tgz",
"integrity": "sha512-9QovlxmpRtvxVbN0UBcv8WfdSMudNZZTFqCsnBszcQXqaZb/TVe30ScgGEO7u1EAIacTPAo7/oCYjYAxiHLanQ==",
"optional": true
"integrity": "sha512-9QovlxmpRtvxVbN0UBcv8WfdSMudNZZTFqCsnBszcQXqaZb/TVe30ScgGEO7u1EAIacTPAo7/oCYjYAxiHLanQ=="
},
"@redis/client": {
"version": "1.4.2",
"resolved": "https://registry.npmjs.org/@redis/client/-/client-1.4.2.tgz",
"integrity": "sha512-oUdEjE0I7JS5AyaAjkD3aOXn9NhO7XKyPyXEyrgFDu++VrVBHUPnV6dgEya9TcMuj5nIJRuCzCm8ZP+c9zCHPw==",
"optional": true,
"requires": {
"cluster-key-slot": "1.1.1",
"generic-pool": "3.9.0",
@ -238,26 +236,22 @@
"@redis/graph": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@redis/graph/-/graph-1.1.0.tgz",
"integrity": "sha512-16yZWngxyXPd+MJxeSr0dqh2AIOi8j9yXKcKCwVaKDbH3HTuETpDVPcLujhFYVPtYrngSco31BUcSa9TH31Gqg==",
"optional": true
"integrity": "sha512-16yZWngxyXPd+MJxeSr0dqh2AIOi8j9yXKcKCwVaKDbH3HTuETpDVPcLujhFYVPtYrngSco31BUcSa9TH31Gqg=="
},
"@redis/json": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/@redis/json/-/json-1.0.4.tgz",
"integrity": "sha512-LUZE2Gdrhg0Rx7AN+cZkb1e6HjoSKaeeW8rYnt89Tly13GBI5eP4CwDVr+MY8BAYfCg4/N15OUrtLoona9uSgw==",
"optional": true
"integrity": "sha512-LUZE2Gdrhg0Rx7AN+cZkb1e6HjoSKaeeW8rYnt89Tly13GBI5eP4CwDVr+MY8BAYfCg4/N15OUrtLoona9uSgw=="
},
"@redis/search": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@redis/search/-/search-1.1.0.tgz",
"integrity": "sha512-NyFZEVnxIJEybpy+YskjgOJRNsfTYqaPbK/Buv6W2kmFNaRk85JiqjJZA5QkRmWvGbyQYwoO5QfDi2wHskKrQQ==",
"optional": true
"integrity": "sha512-NyFZEVnxIJEybpy+YskjgOJRNsfTYqaPbK/Buv6W2kmFNaRk85JiqjJZA5QkRmWvGbyQYwoO5QfDi2wHskKrQQ=="
},
"@redis/time-series": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/@redis/time-series/-/time-series-1.0.4.tgz",
"integrity": "sha512-ThUIgo2U/g7cCuZavucQTQzA9g9JbDDY2f64u3AbAoz/8vE2lt2U37LamDUVChhaDA3IRT9R6VvJwqnUfTJzng==",
"optional": true
"integrity": "sha512-ThUIgo2U/g7cCuZavucQTQzA9g9JbDDY2f64u3AbAoz/8vE2lt2U37LamDUVChhaDA3IRT9R6VvJwqnUfTJzng=="
},
"@socket.io/component-emitter": {
"version": "3.1.0",
@ -1103,8 +1097,7 @@
"cluster-key-slot": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/cluster-key-slot/-/cluster-key-slot-1.1.1.tgz",
"integrity": "sha512-rwHwUfXL40Chm1r08yrhU3qpUvdVlgkKNeyeGPOxnW8/SyVDvgRaed/Uz54AqWNaTCAThlj6QAs3TZcKI0xDEw==",
"optional": true
"integrity": "sha512-rwHwUfXL40Chm1r08yrhU3qpUvdVlgkKNeyeGPOxnW8/SyVDvgRaed/Uz54AqWNaTCAThlj6QAs3TZcKI0xDEw=="
},
"co": {
"version": "4.6.0",
@ -1759,8 +1752,7 @@
"generic-pool": {
"version": "3.9.0",
"resolved": "https://registry.npmjs.org/generic-pool/-/generic-pool-3.9.0.tgz",
"integrity": "sha512-hymDOu5B53XvN4QT9dBmZxPX4CWhBPPLguTZ9MMFeFa/Kg0xWVfylOVNlJji/E7yTZWFd/q9GO5TxDLq156D7g==",
"optional": true
"integrity": "sha512-hymDOu5B53XvN4QT9dBmZxPX4CWhBPPLguTZ9MMFeFa/Kg0xWVfylOVNlJji/E7yTZWFd/q9GO5TxDLq156D7g=="
},
"get-intrinsic": {
"version": "1.1.3",
@ -2795,7 +2787,6 @@
"version": "4.5.1",
"resolved": "https://registry.npmjs.org/redis/-/redis-4.5.1.tgz",
"integrity": "sha512-oxXSoIqMJCQVBTfxP6BNTCtDMyh9G6Vi5wjdPdV/sRKkufyZslDqCScSGcOr6XGR/reAWZefz7E4leM31RgdBA==",
"optional": true,
"requires": {
"@redis/bloom": "1.1.0",
"@redis/client": "1.4.2",

View File

@ -32,7 +32,7 @@ import { middleware as protocolMiddleware } from "./app/middleware/protocol";
// Routes
import { index } from "./app/index";
function startUp(koaApp: Koa, port: number, host?: string) {
function setupHttp(koaApp: Koa, port: number, host?: string) {
const httpServer = http.createServer(koaApp.callback());
httpServer.on("error", (err) => {
@ -54,12 +54,10 @@ function startUp(koaApp: Koa, port: number, host?: string) {
logger.info("==================================");
if (os.platform() == "win32") {
open(`http://localhost:${port}/`).then(() => {
});
open(`http://localhost:${port}/`).then(() => {});
}
}
async function processExit() {
try {
console.log("");
@ -72,13 +70,25 @@ async function processExit() {
}
}
(async () => {
["SIGTERM", "SIGINT", "SIGQUIT"].forEach(function (sig) {
process.on(sig, () => {
logger.warn(`${sig} close process signal detected.`);
processExit();
});
});
process.stdin.on("data", (v) => {
const command = v.toString().replace("\n", "").replace("\r", "").trim().toLowerCase();
if (command === "exit") processExit();
});
async function main() {
// load global configuration file
initSystemConfig();
if (systemConfig.redisUrl.length != 0) {
await RedisStorage.initialize(systemConfig.redisUrl);
Storage.setStorageType(1);
Storage.setStorageType(Storage.TYPE.REDIS);
}
initVersionManager();
@ -161,7 +171,7 @@ _ / / / / /___ ____/ /_ / / / / /_/ /_ / / / /_/ /_ /_/ // __/ /
index(app);
// Error reporting
process.on("uncaughtException", function(err) {
process.on("uncaughtException", function (err) {
logger.error(`ERROR (uncaughtException):`, err);
});
@ -170,19 +180,11 @@ _ / / / / /___ ____/ /_ / / / / /_/ /_ / / / /_/ /_ /_/ // __/ /
logger.error(`ERROR (unhandledRejection):`, reason, p);
});
// start the HTTP service
// start the HTTP service
setupHttp(app, systemConfig.httpPort, systemConfig.httpIp);
}
startUp(app, systemConfig.httpPort, systemConfig.httpIp);
})();
["SIGTERM", "SIGINT", "SIGQUIT"].forEach(function(sig) {
process.on(sig, () => {
logger.warn(`${sig} close process signal detected.`);
processExit();
});
});
process.stdin.on("data", (v) => {
const command = v.toString().replace("\n", "").replace("\r", "").trim().toLowerCase();
if (command === "exit") processExit();
main().catch((err) => {
logger.error("main() error:", err);
process.exit(0);
});

View File

@ -3,17 +3,21 @@ import file_storage from "./file_storage";
import redis_storage from "./redis_storage";
class SystemStorage {
private exclusions = ["Config", "SystemConfig"]; // Config is from daemon and SystemConfig is from panel
private storageSystem: IStorage = file_storage;
public readonly TYPE = {
FILE: 0,
REDIS: 1
};
public setStorageType(type: number) {
switch (type) {
case 0: {
case this.TYPE.FILE: {
// Using fs-based storage
this.storageSystem = file_storage;
break;
}
case 1: {
case this.TYPE.REDIS: {
// Using redis-based storage
this.storageSystem = redis_storage;
break;
@ -30,4 +34,4 @@ class SystemStorage {
}
}
export default new SystemStorage();
export default new SystemStorage();

View File

@ -16,12 +16,12 @@ class RemoteServiceSubsystem extends UniversalRemoteSubsystem<RemoteService> {
async initialize() {
// If it is the first startup, it will automatically try to connect to "LocalHost",
// otherwise it will automatically read from the configuration file and connect to all remote services.
for (const uuid of (await Storage.getStorage().list("RemoteServiceConfig"))) {
const config = await Storage.getStorage().load(
for (const uuid of await Storage.getStorage().list("RemoteServiceConfig")) {
const config = (await Storage.getStorage().load(
"RemoteServiceConfig",
RemoteServiceConfig,
uuid
) as RemoteServiceConfig;
)) as RemoteServiceConfig;
const newService = new RemoteService(uuid, config);
this.setInstance(uuid, newService);
newService.connect();