This repository was archived by the owner on Jun 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathweb.js
More file actions
73 lines (59 loc) · 2.85 KB
/
Copy pathweb.js
File metadata and controls
73 lines (59 loc) · 2.85 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
'use strict';
/* This is the main file for the web service part of the demo application.
It allows users to link accounts via the Nylas Auth API, and listens for
incoming webhooks.
When new accounts are linked or webhooks are received, it queues jobs for
other worker processes.
*/
const express = require('express');
const request = require('request');
const path = require('path');
const QueueConnector = require('./src/queue-connector');
const app = express();
// Custom middleware that keeps the raw request body. This is necessary to checksum
// the data we receive in webhooks and ensure their authenticity.
app.use((req, res, next) => {
req.rawBody = '';
req.on('data', (chunk) => {
req.rawBody += chunk;
});
next();
});
const bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
// Import secret and other configuration
const config = require('./config');
for (const key of Object.keys(config)) {
process.env[key] = process.env[key] || config[key];
}
// Attach routes to Express
require('./routes/webhooks')(app);
require('./routes/authentication')(app);
// Configure views for Express
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
global.publicURLRoot = null;
QueueConnector.connect(() => {
// Setup ngrok settings to ensure everything works locally
request('http://localhost:4040/api/tunnels', (error, response, body) => {
if (error || response.statusCode !== 200) {
throw "It looks like ngrok isn't running! Make sure you've started that first with 'ngrok http 1234'";
}
if (!process.env.NYLAS_APP_SECRET) {
throw "Before running this example, edit ./config.js and add your Nylas App ID and Secret. See config.js.template for an example.";
}
global.publicURLRoot = JSON.parse(body).tunnels[1].public_url
const webhookURI = `${global.publicURLRoot}/webhook`.replace('http:', 'https:');
const callbackURI = `${global.publicURLRoot}/oauth/callback`.replace('http:', 'https:');
// Start the program
console.log(`Server running at http://${process.env.HOST}:${process.env.PORT}/`);
console.log(`\nIf you haven't already, follow these steps:`);
console.log(` - Visit https://developer.nylas.com and create a 'message.created' webhook with the URL: ${webhookURI}`);
console.log(` - Visit https://developer.nylas.com and create a callback with this url ${callbackURI}`);
console.log(` - Start one or more sync workers by running 'npm run worker' in another console.`);
console.log(` - Link an account to sync by visting http://${process.env.HOST}:${process.env.PORT}/!`);
console.log(`\nNot receiving webhooks? Make sure it hasn't been disabled on the dashboard. If it has, delete it and create it again.`);
app.listen(process.env.PORT);
});
});