You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.7 KiB
67 lines
1.7 KiB
7 years ago
|
const express = require('express')
|
||
5 years ago
|
const cors = require('cors')
|
||
7 years ago
|
const bodyParser = require('body-parser')
|
||
|
const app = express()
|
||
6 years ago
|
const expressWs = require('express-ws')
|
||
7 years ago
|
const Provider = require('./provider')
|
||
7 years ago
|
const log = require('./utils/logs.js')
|
||
7 years ago
|
|
||
6 years ago
|
class Server {
|
||
6 years ago
|
constructor (options) {
|
||
|
this.provider = new Provider(options)
|
||
5 years ago
|
this.provider.init().then(() => {
|
||
|
log('Provider initiated')
|
||
|
}).catch((error) => {
|
||
|
log(error)
|
||
|
})
|
||
5 years ago
|
this.rpcOnly = options.rpc
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
start (host, port) {
|
||
6 years ago
|
expressWs(app)
|
||
|
|
||
5 years ago
|
app.use(cors())
|
||
6 years ago
|
app.use(bodyParser.urlencoded({extended: true}))
|
||
|
app.use(bodyParser.json())
|
||
|
|
||
|
app.get('/', (req, res) => {
|
||
|
res.send('Welcome to remix-simulator')
|
||
|
})
|
||
|
|
||
6 years ago
|
if (this.rpcOnly) {
|
||
|
app.use((req, res) => {
|
||
|
this.provider.sendAsync(req.body, (err, jsonResponse) => {
|
||
6 years ago
|
if (err) {
|
||
6 years ago
|
return res.send(JSON.stringify({ error: err }))
|
||
6 years ago
|
}
|
||
6 years ago
|
res.send(jsonResponse)
|
||
6 years ago
|
})
|
||
|
})
|
||
6 years ago
|
} else {
|
||
|
app.ws('/', (ws, req) => {
|
||
|
ws.on('message', (msg) => {
|
||
|
this.provider.sendAsync(JSON.parse(msg), (err, jsonResponse) => {
|
||
|
if (err) {
|
||
|
return ws.send(JSON.stringify({ error: err }))
|
||
|
}
|
||
|
ws.send(JSON.stringify(jsonResponse))
|
||
|
})
|
||
|
})
|
||
5 years ago
|
|
||
|
this.provider.on('data', (result) => {
|
||
5 years ago
|
ws.send(JSON.stringify(result))
|
||
5 years ago
|
})
|
||
6 years ago
|
})
|
||
|
}
|
||
6 years ago
|
|
||
5 years ago
|
app.listen(port, host, () => {
|
||
|
log('Remix Simulator listening on ws://' + host + ':' + port)
|
||
|
if (!this.rpcOnly) {
|
||
|
log('http json-rpc is deprecated and disabled by default. To enable it use --rpc')
|
||
|
}
|
||
|
})
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = Server
|