This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
/
server.js
executable file
·109 lines (84 loc) · 2.67 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import path from 'path';
import express from 'express';
import bodyParser from 'body-parser';
import { graphqlExpress, graphiqlExpress } from 'graphql-server-express';
import { createServer } from 'http';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import cors from 'cors';
import settings from './settings';
import { connectToMongo } from './data/connectors';
import { subscriptionManager } from './data/subscription-manager';
import schema from './data/schema';
import { markCustomerAsNotActive } from './data/utils';
// connect to mongo database
connectToMongo();
const GRAPHQL_PORT = settings.GRAPHQL_PORT;
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const corsOptions = {
origin(origin, callback) {
// origin is white listed
callback(null, settings.ALLOWED_DOMAINS.includes(origin));
},
credentials: true,
};
app.use(cors(corsOptions));
app.use('/graphql', graphqlExpress(() =>
({
schema,
}),
));
// set the view engine to ejs
app.set('view engine', 'ejs');
// Express Middleware for serving static files
app.use('/build', express.static(path.join(__dirname, '../static')));
// routes ===
app.get('/inapp', (req, res) => {
res.render('widget', { type: 'inApp' });
});
app.get('/chat', (req, res) => {
res.render('widget', { type: 'chat' });
});
app.get('/form', (req, res) => {
res.render('widget', { type: 'form' });
});
app.get('/test', (req, res) => {
res.render('widget-test', { type: req.query.type });
});
// graphiql ==
app.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
}));
app.listen(GRAPHQL_PORT);
// websocket server
const WS_PORT = settings.WS_PORT;
const httpServer = createServer((request, response) => {
response.writeHead(404);
response.end();
});
httpServer.listen(WS_PORT, () => console.log( // eslint-disable-line no-console
`Websocket Server is now running on http://localhost:${WS_PORT}`,
));
// subscription server
const server = new SubscriptionServer( // eslint-disable-line no-unused-vars
{ subscriptionManager },
httpServer,
);
// receive inAppConnected message and save integrationId, customerId in
// connection
server.wsServer.on('connect', (connection) => {
connection.on('message', (message) => {
const parsedMessage = JSON.parse(message.utf8Data);
if (parsedMessage.type === 'inAppConnected') {
connection.inAppData = parsedMessage.value; // eslint-disable-line no-param-reassign
}
});
});
// mark customer as not active when connection close
server.wsServer.on('close', (connection) => {
const inAppData = connection.inAppData;
if (inAppData) {
markCustomerAsNotActive(inAppData.customerId);
}
});