Skip to content

Commit

Permalink
chore: Add option for redis (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanguoyu authored Jul 31, 2023
1 parent 7ad94c3 commit 27b69b6
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 4 deletions.
12 changes: 12 additions & 0 deletions packages/core/sample-projects/kuai.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { KuaiConfig } from '@ckb-js/kuai-core';

let redisOpt = undefined;
if (process.env.REDIS_OPT) {
try {
redisOpt = JSON.parse(process.env.REDIS_OPT);
} catch (error) {
//ignore error, if error redisOpt will be undefined
}
}

const config: KuaiConfig = {
port: 3000,
redisPort: process.env.REDIS_PORT ? +process.env.REDIS_PORT : undefined,
redisHost: process.env.REDIS_HOST,
redisOpt,
ckbChain: {
rpcUrl: 'http://127.0.0.1:8114',
prefix: 'ckt',
Expand Down
12 changes: 11 additions & 1 deletion packages/core/sample-projects/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { getGenesisScriptsConfig, initialKuai } from '@ckb-js/kuai-core';
import { KoaRouterAdapter, CoR } from '@ckb-js/kuai-io';
import AppController from './app.controller';
import './type-extends';
import { REDIS_HOST_SYMBOL, REDIS_PORT_SYMBOL, initiateResourceBindingManager, mqContainer } from '@ckb-js/kuai-models';
import {
REDIS_HOST_SYMBOL,
REDIS_OPT_SYMBOL,
REDIS_PORT_SYMBOL,
initiateResourceBindingManager,
mqContainer,
} from '@ckb-js/kuai-models';
import { config } from '@ckb-lumos/lumos';

async function bootstrap() {
Expand All @@ -19,6 +25,10 @@ async function bootstrap() {
mqContainer.bind(REDIS_HOST_SYMBOL).toConstantValue(kuaiEnv.config.redisHost);
}

if (kuaiEnv.config.redisOpt) {
mqContainer.bind(REDIS_OPT_SYMBOL).toConstantValue(kuaiEnv.config.redisOpt);
}

config.initializeConfig(
config.createConfig({
PREFIX: kuaiEnv.config.ckbChain.prefix,
Expand Down
4 changes: 3 additions & 1 deletion packages/core/sample-projects/src/type-extends.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import '@ckb-js/kuai-core';
import { Config } from '@ckb-lumos/config-manager';
import type { Config } from '@ckb-lumos/config-manager';
import type { RedisOptions } from 'ioredis';

declare module '@ckb-js/kuai-core' {
export interface KuaiConfig {
port?: number;
lumosConfig?: Config | 'aggron4' | 'lina';
redisPort?: number;
redisHost?: string;
redisOpt?: RedisOptions;
}
}
4 changes: 3 additions & 1 deletion packages/models/src/actor/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ActorRefEmptyException } from '../exceptions/actor'

export const REDIS_PORT_SYMBOL = Symbol('mq_redis_port')
export const REDIS_HOST_SYMBOL = Symbol('mq_redis_host')
export const REDIS_OPT_SYMBOL = Symbol('mq_redis_opt')

@injectable()
class MessageQueue {
Expand All @@ -20,8 +21,9 @@ class MessageQueue {
constructor(
@inject(REDIS_PORT_SYMBOL) @optional() port = 6379,
@inject(REDIS_HOST_SYMBOL) @optional() host = '127.0.0.1',
@inject(REDIS_OPT_SYMBOL) @optional() opt = {},
) {
this.instance = new Redis(port, host)
this.instance = new Redis(port, host, opt)
}
}

Expand Down
10 changes: 10 additions & 0 deletions packages/samples/mvp-dapp/kuai.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@

require('dotenv').config()

let redisOpt = undefined
if (process.env.REDIS_OPT) {
try {
redisOpt = JSON.parse(process.env.REDIS_OPT)
} catch (error) {
//ignore error, if error redisOpt will be undefined
}
}

module.exports = {
host: process.env.HOST,
port: process.env.PORT,
network: 'testnet',
redisPort: process.env.REDIS_PORT,
redisHost: process.env.REDIS_HOST,
redisOpt,
jest: {
preset: 'ts-jest',
testEnvironment: 'node',
Expand Down
12 changes: 11 additions & 1 deletion packages/samples/mvp-dapp/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import { config } from '@ckb-lumos/lumos'
import { KoaRouterAdapter, CoR } from '@ckb-js/kuai-io'
import cors from '@koa/cors'
import { router } from './app.controller'
import { mqContainer, REDIS_PORT_SYMBOL, REDIS_HOST_SYMBOL, initiateResourceBindingManager } from '@ckb-js/kuai-models'
import {
mqContainer,
REDIS_PORT_SYMBOL,
REDIS_HOST_SYMBOL,
initiateResourceBindingManager,
REDIS_OPT_SYMBOL,
} from '@ckb-js/kuai-models'
import { handleException } from './exception'

async function bootstrap() {
Expand All @@ -27,6 +33,10 @@ async function bootstrap() {
mqContainer.bind(REDIS_HOST_SYMBOL).toConstantValue(kuaiEnv.config.redisHost)
}

if (kuaiEnv.config.redisOpt) {
mqContainer.bind(REDIS_OPT_SYMBOL).toConstantValue(kuaiEnv.config.redisOpt)
}

config.initializeConfig(
config.createConfig({
PREFIX: kuaiEnv.config.ckbChain.prefix,
Expand Down
2 changes: 2 additions & 0 deletions packages/samples/mvp-dapp/src/type-extends.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import '@ckb-js/kuai-core'
import type { Config } from '@ckb-lumos/config-manager'
import type { RedisOptions } from 'ioredis'

declare module '@ckb-js/kuai-core' {
export interface KuaiConfig {
Expand All @@ -14,5 +15,6 @@ declare module '@ckb-js/kuai-core' {
lumosConfig?: Config | 'aggron4' | 'lina'
redisPort?: number
redisHost?: string
redisOpt?: RedisOptions
}
}

0 comments on commit 27b69b6

Please sign in to comment.