diff --git a/README-frontend.md b/README.md similarity index 67% rename from README-frontend.md rename to README.md index c70dc1f8..bc683aa5 100644 --- a/README-frontend.md +++ b/README.md @@ -10,5 +10,12 @@ $ API=http://localhost:3001 nodemon server.js $ node-sass -w sass/application.scss public/css/application.css ``` +To run the server as a daemon that will be re-run if it crashes, you can +use the forever node package. +``` +$ npm install -g forever +$ forever start server.js +``` + Run the metamaps api in another terminal using `$ rails s -p 3001` \ No newline at end of file diff --git a/apiProxyMiddleware.js b/apiProxyMiddleware.js index f0c34fc4..a38ee1d1 100644 --- a/apiProxyMiddleware.js +++ b/apiProxyMiddleware.js @@ -4,6 +4,7 @@ function apiProxyMiddleware (req, res, next) { if (!req.xhr) { return next() } + console.log('xhr request', req.originalUrl) const method = req.method.toLowerCase() req.pipe( request[method](process.env.API + req.originalUrl, { @@ -14,6 +15,7 @@ function apiProxyMiddleware (req, res, next) { followRedirect: false }) ) + .on('error', console.log) .pipe(res) } diff --git a/realtime/README.md b/realtime/README.md deleted file mode 100644 index 95bb2278..00000000 --- a/realtime/README.md +++ /dev/null @@ -1,17 +0,0 @@ -## Node.js realtime server - -To run the server, you need to install the dependencies and run the server. -Please ensure you have followed the OS-specific instructions in doc/ to -install NodeJS. Once you have node, then you can proceed to install the -node packages for the realtime server: - - npm install #creates node_modules directory - node realtime/realtime-server.js - -That's it! - -To run the server as a daemon that will be re-run if it crashes, you can -use the forever node package. - - sudo npm install -g forever - forever start realtime/realtime-server.js diff --git a/realtime/global.js b/realtime/global.js index f7d372e6..901305c1 100644 --- a/realtime/global.js +++ b/realtime/global.js @@ -7,7 +7,7 @@ const { LEAVE_CALL, JOIN_MAP, LEAVE_MAP -} = require('../frontend/src/Metamaps/Realtime/events') +} = require('../src/Metamaps/Realtime/events') module.exports = function(io, store) { store.subscribe(() => { diff --git a/realtime/index.js b/realtime/index.js new file mode 100644 index 00000000..cc4ecdd0 --- /dev/null +++ b/realtime/index.js @@ -0,0 +1,16 @@ +const signalling = require('./signal') +const junto = require('./junto') +const map = require('./map') +const global = require('./global') +const stunservers = [{'url': 'stun:stun.l.google.com:19302'}] + +const { createStore } = require('redux') +const reducer = require('./reducer') + +module.exports = (io) => { + let store = createStore(reducer) + global(io, store) + signalling(io, stunservers, store) + junto(io, store) + map(io, store) +} diff --git a/realtime/junto.js b/realtime/junto.js index a361c1e3..2516419d 100644 --- a/realtime/junto.js +++ b/realtime/junto.js @@ -17,7 +17,7 @@ const { INVITE_A_CALL, JOIN_CALL, LEAVE_CALL -} = require('../frontend/src/Metamaps/Realtime/events') +} = require('../src/Metamaps/Realtime/events') const { mapRoom, userMapRoom } = require('./rooms') diff --git a/realtime/map.js b/realtime/map.js index 9d8a1582..e62ca6bf 100644 --- a/realtime/map.js +++ b/realtime/map.js @@ -10,7 +10,7 @@ const { SEND_COORDS, SEND_MAPPER_INFO, DRAG_TOPIC -} = require('../frontend/src/Metamaps/Realtime/events') +} = require('../src/Metamaps/Realtime/events') const { mapRoom, userMapRoom } = require('./rooms') diff --git a/realtime/realtime-server.js b/realtime/realtime-server.js deleted file mode 100644 index 680e6a60..00000000 --- a/realtime/realtime-server.js +++ /dev/null @@ -1,19 +0,0 @@ -const io = require('socket.io')() -const signalling = require('./signal') -const junto = require('./junto') -const map = require('./map') -const global = require('./global') -const stunservers = [{'url': 'stun:stun.l.google.com:19302'}] - -const { createStore } = require('redux') -const reducer = require('./reducer') - -let store = createStore(reducer) - -global(io, store) -signalling(io, stunservers, store) -junto(io, store) -map(io, store) - -io.listen(parseInt(process.env.NODE_REALTIME_PORT) || 5000) -console.log('booting up', process.env.NODE_REALTIME_PORT || 5000) diff --git a/realtime/reducer.js b/realtime/reducer.js index 9b50d3a0..87039049 100644 --- a/realtime/reducer.js +++ b/realtime/reducer.js @@ -4,7 +4,7 @@ const { LEAVE_MAP, JOIN_CALL, LEAVE_CALL -} = require('../frontend/src/Metamaps/Realtime/events') +} = require('../src/Metamaps/Realtime/events') const NOT_IN_CONVERSATION = 0 const IN_CONVERSATION = 1 diff --git a/server.js b/server.js index 38f890d6..f76ff8e7 100644 --- a/server.js +++ b/server.js @@ -1,29 +1,43 @@ const path = require('path') const express = require('express') const webpack = require('webpack') +const socketio = require('socket.io') +const { createServer } = require('http') const webpackDevMiddleware = require('webpack-dev-middleware') const apiProxyMiddleware = require('./apiProxyMiddleware') +const realtime = require('./realtime') const port = process.env.PORT || 3000 -const app = express(); +const app = express() +const server = createServer(app) +const io = socketio(server) +realtime(io) // sets up the socketio event listeners +// serve the whole public folder as static files app.use(express.static(path.join(__dirname, 'public'))) -const config = require('./webpack.config.js'); -const compiler = webpack(config); +const config = require('./webpack.config.js') +const compiler = webpack(config) // Tell express to use the webpack-dev-middleware and use the webpack.config.js // configuration file as a base. app.use(webpackDevMiddleware(compiler, { publicPath: config.output.publicPath })) +// pass XMLHttpRequests +// through to the API +// anything which is the UI wanting to make requests to the API app.use(apiProxyMiddleware) +// for any normal route app.get('*', function (req, res) { - res.sendFile(path.join(__dirname, 'public/index.html')) + // respond with the same html file + // since the whole UI technically boots + // from the React app and the javascript + res.sendFile(path.join(__dirname, 'public/index.html')) }) // Serve the files on set port or port 3000. -app.listen(port, function () { +server.listen(port, function () { console.log('Metamaps listening on port ' + port + '\n') }); \ No newline at end of file