-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·78 lines (68 loc) · 1.88 KB
/
server.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
if (!process.env.HEROKU) require('dotenv/config')
var jalla = require('jalla')
var body = require('koa-body')
var dedent = require('dedent')
var compose = require('koa-compose')
var { get, post } = require('koa-route')
var unparsed = require('koa-body/unparsed')
var purge = require('./lib/purge')
var email = require('./lib/email')
var ENDPOINT =
'https://docs.google.com/forms/d/e/1FAIpQLSdM-T0zn8tIhIy4s4O9D61mGqaezMUvg2Io-dwkWLQe9dKvbg/formResponse'
var app = jalla('index.js', {
sw: 'sw.js',
serve: Boolean(process.env.HEROKU)
})
// proxy application form for Google Forms
app.use(
post(
'/ansok',
compose([
body({ includeUnparsed: true }),
async function (ctx, next) {
await fetch(ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: ctx.request.body[unparsed]
})
var fields = ctx.request.body.entry
// send an confirmation email
await email(fields).catch(console.error)
if (ctx.accepts('html')) {
ctx.redirect('/tack?contact=' + fields['1183121357'])
} else {
ctx.type = 'application/json'
ctx.body = {}
}
}
])
)
)
// disallow robots anywhere but live
app.use(
get('/robots.txt', function (ctx, next) {
ctx.type = 'text/plain'
ctx.body = dedent`
User-agent: *
Disallow: ${app.env === 'production' ? '' : '/'}
`
})
)
// set headers
app.use(function (ctx, next) {
if (!ctx.accepts('html')) return next()
ctx.state.ref = null
if (app.env !== 'development') {
ctx.set('Cache-Control', `s-maxage=${60 * 60 * 24 * 30}, max-age=0`)
}
return next()
})
app.listen(process.env.PORT || 8080, function () {
if (process.env.HEROKU && app.env === 'production') {
purge(['/sw.js'], function (err) {
if (err) app.emit('error', err)
})
}
})