Merge pull request #1224 from omnivore-app/fix/redis-tls

fix/redis tls
This commit is contained in:
Hongbo Wu 2022-09-22 11:56:35 +08:00 committed by GitHub
commit eae715820d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 5 deletions

View file

@ -181,7 +181,10 @@ export const textToSpeechStreamingHandler = Sentry.GCPFunction.wrapHttpFunction(
const ssml = `${startSsml(ssmlOptions)}${utteranceInput.text}${endSsml()}`
// hash ssml to get the cache key
const cacheKey = crypto.createHash('md5').update(ssml).digest('hex')
const redisClient = await createRedisClient()
const redisClient = await createRedisClient(
process.env.REDIS_URL,
process.env.REDIS_CERT
)
// find audio data in cache
const cacheResult = await redisClient.get(cacheKey)
if (cacheResult) {
@ -207,12 +210,12 @@ export const textToSpeechStreamingHandler = Sentry.GCPFunction.wrapHttpFunction(
return res.status(500).send({ errorCode: 'SYNTHESIZER_ERROR' })
}
const audioDataString = audioData.toString('hex')
// save audio data to cache for 1 hour
// save audio data to cache for 24 hours for mainly the newsletters
await redisClient.set(
cacheKey,
JSON.stringify({ audioDataString, speechMarks }),
{
EX: 3600, // in seconds
EX: 3600 * 24, // in seconds
NX: true,
}
)

View file

@ -1,11 +1,19 @@
import { createClient } from 'redis'
export const createRedisClient = async () => {
const redisClient = createClient({ url: process.env.REDIS_URL })
export const createRedisClient = async (url?: string, cert?: string) => {
const redisClient = createClient({
url,
socket: {
tls: url?.startsWith('rediss://'), // rediss:// is the protocol for TLS
cert: cert?.replace(/\\n/g, '\n'), // replace \n with new line
rejectUnauthorized: false, // for self-signed certs
},
})
redisClient.on('error', (err) => console.error('Redis Client Error', err))
await redisClient.connect()
console.log('Redis Client Connected:', url)
return redisClient
}