diff --git a/src/server/app.ts b/src/server/app.ts index 4a9283f..3d27299 100644 --- a/src/server/app.ts +++ b/src/server/app.ts @@ -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 diff --git a/src/server/config.ts b/src/server/config.ts index 38b319e..32ada47 100644 --- a/src/server/config.ts +++ b/src/server/config.ts @@ -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'}), } diff --git a/src/server/redis.ts b/src/server/redis.ts new file mode 100644 index 0000000..8855e65 --- /dev/null +++ b/src/server/redis.ts @@ -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, + }) +} diff --git a/src/server/socket.ts b/src/server/socket.ts index b1c6711..ea8a117 100644 --- a/src/server/socket.ts +++ b/src/server/socket.ts @@ -5,7 +5,7 @@ import { Store } from './store' const debug = _debug('peercalls:socket') -interface Stores { +export interface Stores { userIdBySocketId: Store socketIdByUserId: Store } diff --git a/src/server/store/factory.ts b/src/server/store/factory.ts deleted file mode 100644 index d85f3af..0000000 --- a/src/server/store/factory.ts +++ /dev/null @@ -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() - } -} diff --git a/src/server/store/index.ts b/src/server/store/index.ts index 0d8d528..1af8943 100644 --- a/src/server/store/index.ts +++ b/src/server/store/index.ts @@ -1,2 +1,3 @@ export * from './store' export * from './memory' +export * from './redis'