-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
60 lines (52 loc) · 1.47 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
48
49
50
51
52
53
54
55
56
57
58
59
60
var nconf = require('nconf')
var bodyParser = require('body-parser')
var dataUriToBuffer = require('data-uri-to-buffer')
var io = require('socket.io-client')
var gm = require('gm')
var express = require('express')
var app = express()
var uuid = require('uuid')
var fingerprint = uuid.v4()
nconf.argv().env().file({ file: 'local.json'})
var meatSpacUrl = nconf.get('url')
var socket = io.connect(meatSpacUrl)
var throttle = require('tokenthrottle')({
rate: nconf.get('rate'),
burst: nconf.get('burst'),
window: nconf.get('window')
})
socket.on('connect', function() {
console.log('connected to socket at: ' + meatSpacUrl)
})
app.use(bodyParser.json({limit: '2mb'}))
app.use(express.static(__dirname + '/public'))
app.get('/', function(req, res) {
res.status(200).end()
})
var addChat = function(image) {
throttle.rateLimit('meatspac', function(err, limited) {
if (err || limited) {
return
}
socket.emit('message', {
apiKey: nconf.get('apiKey'),
message: nconf.get('message'),
picture: 'data:image/gif;base64,' + image.toString('base64'),
fingerPrint: fingerprint
})
})
}
app.post('/service', function(req, res) {
var imgBuff = dataUriToBuffer(req.body.content.data)
gm(imgBuff).resize(135, 101).toBuffer('GIF', function(err, image) {
if (err) {
console.log(err)
return
}
addChat(image)
})
res.json(req.body)
})
var port = nconf.get('port')
app.listen(port)
console.log('server running on port: ', port)