-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (40 loc) · 1.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const fs = require('fs')
const path = require('path')
const express = require('express')
const http = require('http')
const socketio = require('socket.io')
const cheerio = require('cheerio')
const swaggerUI = require('swagger-ui-dist').absolutePath()
const swaggerLive = path.join(path.resolve(__dirname), 'dist')
function index () {
const $ = cheerio.load(fs.readFileSync(swaggerUI + '/index.html'))
$('script').last().remove()
$('body').append('<script src="./swagger-ui-live.js"> </script>')
$('body').append('<script src="./socket.io/socket.io.js"> </script>')
return $.html()
}
module.exports = function (spec, options = {}) {
options.port = options.port || 3000
options.host = options.host || '127.0.0.1'
const app = express()
app.get('/', (req, res) => res.send(index()))
app.use(express.static(swaggerUI, { index: false }))
app.use(express.static(swaggerLive))
const server = http.createServer(app)
const io = socketio(server)
io.on('connection', socket => {
socket.on('ready', data => {
socket.emit('update', spec)
})
})
server.listen(options.port, options.host)
return {
update: function (value) {
spec = value
io.sockets.emit('update', spec)
},
stop: function () {
server.stop()
}
}
}