Skip to content

Commit

Permalink
allow setting Mongo location using MONGO_URI
Browse files Browse the repository at this point in the history
  • Loading branch information
darthfiddler committed Mar 1, 2018
1 parent 557b66c commit 65cefdf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/server/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class Mongo {
*/
constructor({ url, dbname, cacheSize, mockClient }) {
if (cacheSize === undefined) cacheSize = 1000;
if (dbname === undefined) dbname = 'bgio';

this.client = mockClient || MongoClient;
this.url = url;
Expand Down
3 changes: 2 additions & 1 deletion src/server/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ test('Mongo', async () => {
}

{
const db = new Mongo({ url: 'a' });
const db = new Mongo({ url: 'a', dbname: 'test' });
expect(db.client).toBeDefined();
expect(db.dbname).toBe('test');
}
});
9 changes: 7 additions & 2 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
const Koa = require('koa');
const IO = require('koa-socket');
const Redux = require('redux');
import { InMemory } from './db';
import { InMemory, Mongo } from './db';
import { createGameReducer } from '../core/reducer';
const PING_TIMEOUT = 20 * 1e3;
const PING_INTERVAL = 10 * 1e3;
Expand All @@ -26,7 +26,11 @@ export function Server({ games, db, _clientInfo, _roomInfo }) {
io.attach(app);

if (db === undefined) {
db = new InMemory();
if (process.env.MONGO_URI) {
db = new Mongo({ url: process.env.MONGO_URI });
} else {
db = new InMemory();
}
}

const clientInfo = _clientInfo || new Map();
Expand Down Expand Up @@ -131,6 +135,7 @@ export function Server({ games, db, _clientInfo, _roomInfo }) {

return {
app,
db,
run: async (port, callback) => {
await db.connect();
app.listen(port, callback);
Expand Down
11 changes: 11 additions & 0 deletions src/server/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import Game from '../core/game';
import * as ActionCreators from '../core/action-creators';
import * as Redux from 'redux';

beforeEach(() => {
jest.resetModules();
});

jest.mock('koa-socket', () => {
class MockSocket {
constructor() {
Expand Down Expand Up @@ -278,3 +282,10 @@ test('custom db implementation', async () => {
await io.socket.receive('sync', 'gameID');
expect(getId).toBe('gameID');
});

test('MONGO_URI', () => {
process.env.MONGO_URI = 'test';
const server = Server({ games: [game] });
expect(server.db.url).toBe('test');
delete process.env.MONGO_URI;
});

0 comments on commit 65cefdf

Please sign in to comment.