forked from kilianc/rtail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtail-server.js
More file actions
executable file
·144 lines (128 loc) · 3.57 KB
/
rtail-server.js
File metadata and controls
executable file
·144 lines (128 loc) · 3.57 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/sh
":" //# comment; exec /usr/bin/env node --harmony "$0" "$@"
/*!
* server.js
* Created by Kilian Ciuffolo on Oct 26, 2014
* (c) 2014-2015
*/
'use strict'
const dgram = require('dgram')
const app = require('express')()
const serve = require('express').static
const http = require('http').Server(app)
const io = require('socket.io')()
const yargs = require('yargs')
const debug = require('debug')('rtail:server')
const webapp = require('./lib/webapp')
const updateNotifier = require('update-notifier')
const pkg = require('../package')
/*!
* inform the user of updates
*/
updateNotifier({
packageName: pkg.name,
packageVersion: pkg.version
}).notify()
/*!
* parsing argv
*/
let argv = yargs
.usage('Usage: rtail-server [OPTIONS]')
.example('rtail-server --web-port 8080', 'Use custom HTTP port')
.example('rtail-server --udp-port 8080', 'Use custom UDP port')
.example('rtail-server --web-version stable', 'Always uses latest stable webapp')
.example('rtail-server --web-version unstable', 'Always uses latest develop webapp')
.example('rtail-server --web-version 0.1.3', 'Use webapp v0.1.3')
.option('udp-host', {
alias: 'uh',
default: '127.0.0.1',
describe: 'The listening UDP hostname'
})
.option('udp-port', {
alias: 'up',
default: 9999,
describe: 'The listening UDP port'
})
.option('web-host', {
alias: 'wh',
default: '127.0.0.1',
describe: 'The listening HTTP hostname'
})
.option('web-port', {
alias: 'wp',
default: 8888,
describe: 'The listening HTTP port'
})
.option('web-version', {
type: 'string',
describe: 'Define web app version to serve'
})
.help('help')
.alias('help', 'h')
.version(pkg.version, 'version')
.alias('version', 'v')
.strict()
.argv
/*!
* UDP sockets setup
*/
let streams = {}
let socket = dgram.createSocket('udp4')
socket.on('message', function (data, remote) {
// try to decode JSON
try { data = JSON.parse(data) }
catch (err) { return debug('invalid data sent') }
if (!streams[data.id]) {
streams[data.id] = []
io.sockets.emit('streams', Object.keys(streams))
}
let message = {
timestamp: data.timestamp,
streamid: data.id,
host: remote.address,
port: remote.port,
content: data.content,
type: typeof data.content
}
// limit backlog to 100 lines
streams[data.id].length >= 100 && streams[data.id].shift()
streams[data.id].push(message)
debug(JSON.stringify(message))
io.sockets.to(data.id).emit('line', message)
})
/*!
* socket.io
*/
io.on('connection', function (socket) {
socket.emit('streams', Object.keys(streams))
socket.on('select stream', function (stream) {
socket.leave(socket.rooms[0])
if (!stream) return
socket.join(stream)
socket.emit('backlog', streams[stream])
})
})
/*!
* serve static webapp from S3
*/
if (!argv.webVersion) {
app.use(serve(__dirname + '/../dist'))
} else if ('development' === argv.webVersion) {
app.use('/app', serve(__dirname + '/../app'))
app.use('/node_modules', serve(__dirname + '/../node_modules'))
io.path('/app/socket.io')
} else {
app.use(webapp({
s3: 'http://rtail.s3-website-us-east-1.amazonaws.com/' + argv.webVersion,
ttl: 1000 * 60 * 60 * 6 // 6H
}))
debug('serving webapp from: http://rtail.s3-website-us-east-1.amazonaws.com/%s', argv.webVersion)
}
/*!
* listen!
*/
io.attach(http, { serveClient: false })
socket.bind(argv.udpPort, argv.udpHost)
http.listen(argv.webPort, argv.webHost)
debug('UDP server listening: %s:%s', argv.udpHost, argv.udpPort)
debug('HTTP server listening: http://%s:%s', argv.webHost, argv.webPort)