-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
67 lines (60 loc) · 1.61 KB
/
app.js
File metadata and controls
67 lines (60 loc) · 1.61 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
const path = require('path')
const config = require(path.join(__dirname, '.', 'config'))
const plugins = require(path.join(__dirname, '.', 'plugins'))
const Hapi = require('hapi')
const rp = require('request-promise')
const Boom = require('boom')
const logger = require('logger').getLogger()
const server = Hapi.server({
host: config.HOST,
port: config.PORT
})
async function register () {
await server.register(plugins)
}
server.route({
config: {
cors: { origin: config.CORS }
},
method: 'POST',
path: '/',
handler: async (request, h) => {
logger.debug(request.payload)
var options = {
uri: `https://www.google.com/recaptcha/api/siteverify?secret=${config.RECAPTCHA_SECRET_KEY}&response=${request.payload.recaptcha}`,
method: 'POST',
json: true
}
const source = request.payload.source
const receivers = config.RECEIVERS
// send request to google to verify recaptcha code
try {
let res = await rp(options)
if (res.success) {
request.server.methods.mailgun
.send(source, receivers, request.payload)
return { success: true }
} else {
return Boom.unauthorized('reCaptcha not valid')
}
} catch (err) {
logger.error(err)
return Boom.boomify(err)
}
}
})
// Start the server
async function start () {
try {
config.validate()
await register()
await server.start()
logger.info(`Server running at: ${server.info.uri}`)
} catch (err) {
logger.error('error', err)
process.exit(1)
}
};
module.exports.start = start
module.exports.register = register
module.exports.server = server