Skip to content

Commit

Permalink
fix: disabling redis TLS does not work. (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
flxxyz authored Jul 20, 2023
1 parent 62c48dd commit 90aea4e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
16 changes: 10 additions & 6 deletions src/cache/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,26 @@ export const getCacheConfig = (): RedisConnectOptions => ({
tls: Config.REDIS_TLS,
})

let instance: CacheClient | undefined = undefined
let mainInstance: CacheClient
let cacheInstance: CacheClient

export const getCacheClient = async (): Promise<CacheClient> => {
if (!instance) {
export const getCacheClient = async (useCache?: boolean): Promise<CacheClient> => {
if (!mainInstance) {
const config = getCacheConfig()
const { password: _, ...loggableConfig } = config
debug('config: %o', loggableConfig)
if (config.hostname) {
instance = await connect(config)
console.log('🚀 Connecting to Cache Client...')
mainInstance = await connect(config)
cacheInstance = await connect(config)
console.log('🚀 Connected to Cache Client.')
}
}

return instance
return useCache ? cacheInstance : mainInstance
}

export async function publish(channel: string, message: RedisValue) {
const client = await getCacheClient()
const client = await getCacheClient(true)
return client.publish(channel, message)
}
8 changes: 7 additions & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ const Config = {
REDIS_DB: Number(Deno.env.get('REDIS_DB') || 0),
REDIS_USER: Deno.env.get('REDIS_USER'),
REDIS_PASS: Deno.env.get('REDIS_PASS'),
REDIS_TLS: Boolean(Deno.env.get('REDIS_TLS')),
REDIS_TLS: (() => {
const tlsEnable = Deno.env.get('REDIS_TLS') as unknown as string
if (tlsEnable && ['true', 'false'].includes(tlsEnable)) {
return tlsEnable === 'true'
}
return false
})(),
}

export default Config
2 changes: 1 addition & 1 deletion src/database/DatabaseWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class DatabaseWatcher extends EventEmitter {
this.changeStream.on('change', this.onChangeStreamChange)
this.changeStream.on('error', this.onChangeStreamError)

console.log('Using change streams')
console.log('Using change streams')
} catch (err: unknown) {
console.error(err, 'Change stream error')

Expand Down
12 changes: 6 additions & 6 deletions src/database/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createLogger } from '../factories/logger-factory.ts'
const getMasterConfig = () => {
const mongoUri = Config.MONGO_URI
if (!mongoUri) {
console.error('mongoUri not exit please export MONGO_URI env')
console.error('🚨 You may have entered an incorrect MONGO_URI environment variable.')
return Deno.exit(1)
}

Expand Down Expand Up @@ -36,10 +36,10 @@ export const getMasterDbClient = () => {
retryWrites: true,
})
writeClient.on('open', () => {
console.log('Connected to database. selected db of', writeClient.name)
console.log('🚀 Connecting to database. selected db of', writeClient.name)
})
writeClient.on('error', () => {
console.log('Unable to connect to database')
console.log('💥 Unable to connect to database')
})
}

Expand All @@ -49,7 +49,7 @@ export const getMasterDbClient = () => {
const getReadReplicaConfig = () => {
const mongoUri = Config.MONGO_URI
if (!mongoUri) {
console.error('mongoUri not exit please export MONGO_URI env')
console.error('🚨 You may have entered an incorrect MONGO_URI environment variable.')
return Deno.exit(1)
}

Expand Down Expand Up @@ -83,10 +83,10 @@ export const getReadReplicaDbClient = () => {
retryReads: true,
})
readClient.on('open', () => {
console.log('Connected to secondary database. selected db of', readClient.name)
console.log('🚀 Connected to secondary database. selected db of', readClient.name)
})
readClient.on('error', () => {
console.log('Unable to connect to secondary database')
console.log('💥 Unable to connect to secondary database')
})
}

Expand Down

0 comments on commit 90aea4e

Please sign in to comment.