Commit b64519c8 authored by Iuri Matias's avatar Iuri Matias

refactor server code into a module

parent dab4a931
#!/usr/bin/env node #!/usr/bin/env node
require('../src/server'); const Server = require('../src/server');
const server = new Server()
server.start()
...@@ -5,27 +5,32 @@ const expressWs = require('express-ws') ...@@ -5,27 +5,32 @@ const expressWs = require('express-ws')
const Provider = require('./provider') const Provider = require('./provider')
const log = require('./utils/logs.js') const log = require('./utils/logs.js')
expressWs(app) class Server {
var provider = new Provider() constructor() {
this.provider = new Provider()
}
start(host, port) {
expressWs(app)
app.use(bodyParser.urlencoded({extended: true})) app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json()) app.use(bodyParser.json())
app.get('/', (req, res) => { app.get('/', (req, res) => {
res.send('Welcome to remix-simulator') res.send('Welcome to remix-simulator')
}) })
app.use((req, res) => { app.use((req, res) => {
provider.sendAsync(req.body, (err, jsonResponse) => { provider.sendAsync(req.body, (err, jsonResponse) => {
if (err) { if (err) {
return res.send(JSON.stringify({error: err})) return res.send(JSON.stringify({error: err}))
} }
res.send(jsonResponse) res.send(jsonResponse)
}) })
}) })
app.ws('/', (ws, req) => { app.ws('/', (ws, req) => {
ws.on('message', function (msg) { ws.on('message', function (msg) {
provider.sendAsync(JSON.parse(msg), (err, jsonResponse) => { provider.sendAsync(JSON.parse(msg), (err, jsonResponse) => {
if (err) { if (err) {
...@@ -34,6 +39,11 @@ app.ws('/', (ws, req) => { ...@@ -34,6 +39,11 @@ app.ws('/', (ws, req) => {
ws.send(JSON.stringify(jsonResponse)) ws.send(JSON.stringify(jsonResponse))
}) })
}) })
}) })
app.listen(8545, () => log('Remix Simulator listening on port 8545!'))
}
}
app.listen(8545, () => log('Remix Simulator listening on port 8545!')) module.exports = Server
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment