Make redis configurable
This commit is contained in:
parent
170c52eefa
commit
d6104bae14
@ -1,18 +1,15 @@
|
||||
import { config } from './config'
|
||||
import _debug from 'debug'
|
||||
import bodyParser from 'body-parser'
|
||||
import _debug from 'debug'
|
||||
import ejs from 'ejs'
|
||||
import express from 'express'
|
||||
import handleSocket from './socket'
|
||||
import path from 'path'
|
||||
import { createServer } from './server'
|
||||
import SocketIO from 'socket.io'
|
||||
import { config } from './config'
|
||||
import { configureStores } from './redis'
|
||||
import call from './routes/call'
|
||||
import index from './routes/index'
|
||||
import ejs from 'ejs'
|
||||
// import { MemoryStore } from './store'
|
||||
import Redis from 'ioredis'
|
||||
import redisAdapter from 'socket.io-redis'
|
||||
import { RedisStore } from './store/redis'
|
||||
import { createServer } from './server'
|
||||
import handleSocket from './socket'
|
||||
|
||||
const debug = _debug('peercalls')
|
||||
const logRequest = _debug('peercalls:requests')
|
||||
@ -25,19 +22,6 @@ debug(`WebSocket URL: ${SOCKET_URL}`)
|
||||
const app = express()
|
||||
const server = createServer(config, app)
|
||||
export const io = SocketIO(server, { path: SOCKET_URL })
|
||||
const pubClient = new Redis({
|
||||
host: '127.0.0.1',
|
||||
port: 6379,
|
||||
})
|
||||
const subClient = new Redis({
|
||||
host: '127.0.0.1',
|
||||
port: 6379,
|
||||
})
|
||||
io.adapter(redisAdapter({
|
||||
key: 'peercalls',
|
||||
pubClient,
|
||||
subClient,
|
||||
}))
|
||||
|
||||
app.set('x-powered-by', false)
|
||||
app.locals.version = require('../../package.json').version
|
||||
@ -65,10 +49,7 @@ router.use('/call', call)
|
||||
router.use('/', index)
|
||||
app.use(BASE_URL, router)
|
||||
|
||||
const stores = {
|
||||
socketIdByUserId: new RedisStore(pubClient, 'peercalls:socketIdByUserId'),
|
||||
userIdBySocketId: new RedisStore(pubClient, 'peercalls:userIdBySocketId'),
|
||||
}
|
||||
const stores = configureStores(io, config.store)
|
||||
io.on('connection', socket => handleSocket(socket, io, stores))
|
||||
|
||||
export default server
|
||||
|
||||
@ -24,19 +24,24 @@ export interface Config {
|
||||
store?: StoreConfig
|
||||
}
|
||||
|
||||
export type StoreConfig = {
|
||||
export interface StoreRedisConfig {
|
||||
host: string
|
||||
port: number
|
||||
prefix: string
|
||||
type: 'redis'
|
||||
} | {
|
||||
}
|
||||
|
||||
export interface StoreMemoryConfig {
|
||||
type: 'memory'
|
||||
}
|
||||
|
||||
export type StoreConfig = StoreRedisConfig | StoreMemoryConfig
|
||||
|
||||
const cfg = readConfig()
|
||||
|
||||
export const config: Config = {
|
||||
baseUrl: cfg.get('baseUrl', ''),
|
||||
iceServers: cfg.get('iceServers'),
|
||||
ssl: cfg.get('ssl', undefined),
|
||||
store: cfg.get('store', {type: 'memory'}),
|
||||
}
|
||||
|
||||
55
src/server/redis.ts
Normal file
55
src/server/redis.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import _debug from 'debug'
|
||||
import Redis from 'ioredis'
|
||||
import redisAdapter from 'socket.io-redis'
|
||||
import { StoreConfig, StoreRedisConfig } from './config'
|
||||
import { Stores } from './socket'
|
||||
import { MemoryStore, RedisStore } from './store'
|
||||
|
||||
const debug = _debug('peercalls')
|
||||
|
||||
export function configureStores(
|
||||
io: SocketIO.Server,
|
||||
config: StoreConfig = { type: 'memory'},
|
||||
): Stores {
|
||||
switch (config.type) {
|
||||
case 'redis':
|
||||
debug('Using redis store: %s:%s', config.host, config.port)
|
||||
configureRedis(io, config)
|
||||
return {
|
||||
socketIdByUserId: new RedisStore(
|
||||
createRedisClient(config),
|
||||
[config.prefix, 'socketIdByUserId'].join(':'),
|
||||
),
|
||||
userIdBySocketId: new RedisStore(
|
||||
createRedisClient(config),
|
||||
[config.prefix, 'socketIdByUserId'].join(':'),
|
||||
),
|
||||
}
|
||||
default:
|
||||
debug('Using in-memory store')
|
||||
return {
|
||||
socketIdByUserId: new MemoryStore(),
|
||||
userIdBySocketId: new MemoryStore(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function configureRedis(
|
||||
io: SocketIO.Server,
|
||||
config: StoreRedisConfig,
|
||||
) {
|
||||
const pubClient = createRedisClient(config)
|
||||
const subClient = createRedisClient(config)
|
||||
io.adapter(redisAdapter({
|
||||
key: 'peercalls',
|
||||
pubClient,
|
||||
subClient,
|
||||
}))
|
||||
}
|
||||
|
||||
function createRedisClient(config: StoreRedisConfig) {
|
||||
return new Redis({
|
||||
host: config.host,
|
||||
port: config.port,
|
||||
})
|
||||
}
|
||||
@ -5,7 +5,7 @@ import { Store } from './store'
|
||||
|
||||
const debug = _debug('peercalls:socket')
|
||||
|
||||
interface Stores {
|
||||
export interface Stores {
|
||||
userIdBySocketId: Store
|
||||
socketIdByUserId: Store
|
||||
}
|
||||
|
||||
@ -1,17 +0,0 @@
|
||||
import { MemoryStore } from './memory'
|
||||
import { RedisStore } from './redis'
|
||||
import Redis from 'ioredis'
|
||||
import { StoreConfig } from '../config'
|
||||
import { Store } from './store'
|
||||
|
||||
export function createStore(config: StoreConfig = { type: 'memory'}): Store {
|
||||
switch (config.type) {
|
||||
case 'redis':
|
||||
return new RedisStore(new Redis({
|
||||
host: config.host,
|
||||
port: config.port,
|
||||
}), config.prefix)
|
||||
default:
|
||||
return new MemoryStore()
|
||||
}
|
||||
}
|
||||
@ -1,2 +1,3 @@
|
||||
export * from './store'
|
||||
export * from './memory'
|
||||
export * from './redis'
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user