-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
75 lines (59 loc) · 1.9 KB
/
server.js
File metadata and controls
75 lines (59 loc) · 1.9 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
// core
var fs = require('fs')
// vendor
var express = require('express')
, morgan = require('morgan')
, bodyParser = require('body-parser')
, Mustache = require('mustache')
, xtend = require('xtend')
, send_request = require('request')
// config
var HTML_TEMPLATE = fs.readFileSync('./index.mustache').toString()
, WEBHOOK_TEMPLATE = fs.readFileSync('./EDIT-ME.webhook-format.mustache').toString()
, PORT = process.env.PORT || 5000
, BASE_URL = process.env.BASE_URL
? process.env.BASE_URL
: process.env.HEROKU_APP_NAME
? 'https://' + process.env.HEROKU_APP_NAME + '.herokuapp.com'
: 'http://localhost:' + PORT
, RAFFLE_ID = process.env.RAFFLE_ID // REQUIRED
, ACTION_ID = process.env.ACTION_ID // REQUIRED
var app = express()
// request logging
app.use(morgan('combined'))
//
// api endpoints
//
app.get('/', function (request, response) {
var html = Mustache.render(HTML_TEMPLATE, {
webhookUrl: BASE_URL + '/webhook'
, webhookTemplate: WEBHOOK_TEMPLATE
})
response.send(html)
})
app.post('/webhook', bodyParser.json(), function (request, response) {
var submission = JSON.parse(Mustache.render(WEBHOOK_TEMPLATE, request.body))
coerceTimeField(submission)
send_request({
method: 'POST'
, url: 'https://enter.rafflecopter.com/' + RAFFLE_ID + '/' + ACTION_ID
, json: submission
}, function (_err, resp, body) {
response.status(resp.statusCode).json(body)
})
})
app.listen(PORT, function() {
console.log('Node app is running');
console.log('Instructions available at', BASE_URL)
console.log('Send webhook requests to', BASE_URL + '/webhook')
});
// util
function coerceTimeField(submission) {
var datetime
// if time field is a string, assume it's iso8601
if (typeof submission.time === 'string') {
datetime = new Date(submission.time)
submission.time = +datetime
}
// otherwise, assume it's a millisecond unix timestamp and do nothing
}