-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
54 lines (46 loc) · 1.57 KB
/
index.coffee
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
redis = require('redis')
restify = require('restify')
sanitize = require('validator').sanitize
boot = ->
db.select process.env['REDIS_DB'] || 0, (err, status) ->
if (err || status != 'OK')
throw new Error(err)
console.log('db selected')
if process.env['NODE_ENV'] == 'production'
port = process.env['REDIS_PORT']
host = process.env['REDIS_HOST']
db = redis.createClient(port, host)
db.auth process.env['REDIS_PASSWORD'], (err, status) ->
if (err || status != 'OK')
throw new Error(err)
console.info('Connected to redis!')
boot()
else
console.info('connecting to local redis instance')
db = redis.createClient()
boot()
app = restify.createServer()
app.use restify.queryParser()
app.use restify.bodyParser()
app.use restify.acceptParser(app.acceptable)
app.post '/register', (req, res, next) ->
userDetails = sanitize(req.params?.details || '').xss().trim()
if not userDetails?
return res.send(new restify.InvalidContentError())
if /^(.+)@(.+)$/.test(userDetails)
db.sadd "emails", userDetails, (err, result) ->
if (err)
return next(new restify.InternalError())
console.info('Added email:', userDetails)
return res.send(200)
else if /^@[A-Za-z0-9-_]+$/.test(userDetails)
db.sadd "twitters", userDetails, (err, result) ->
if (err)
return next(new restify.InternalError())
console.info('Added email:', userDetails)
return res.send(200)
else
console.info('Didn\'t handle:', userDetails)
return next(new restify.MissingParameterError())
module.exports = app
app.db = db