-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
53 lines (46 loc) · 1.47 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
require('dotenv').config();
const { setupSentry } = require('./utils/sentry');
const { connectToDb } = require('./utils/mongoose');
const express = require('express');
const graphqlHTTP = require('express-graphql');
const { getGraphqlCORS, getStatusCORS } = require('./utils/cors');
const socket = require('socket.io');
const ConversationSocket = require('./socket/ConversationSocket');
const StatusSocket = require('./socket/StatusSocket');
const { httpRequestAuth } = require('./middleware/authentication');
/** Sentry **/
setupSentry();
/** Connect to the database **/
connectToDb();
/** Express app **/
const app = express();
/** graphQL setup **/
//Graphql Schema
const schema = require('./graphql/schema');
//Add it to the express app as a middleware
app.use(
'/graphql',
getGraphqlCORS(),
httpRequestAuth,
graphqlHTTP( req => ({
schema,
context: {
user: req.user,
error: req.error
},
graphiql: true
}))
);
/** Server status endpoint **/
app.use('/status', getStatusCORS(), function (req, res) {
res.status(200).send("200 OK: Server is up and running!");
});
/** Start server **/
const port = process.env.MAIN_SERVER_PORT ? process.env.MAIN_SERVER_PORT : 3000;
const server = app.listen(port, function(){
console.info('Server is successfully launched and can be reached on port:' + port);
});
/** Setup socket **/
const io = socket(server);
new ConversationSocket(io).listen();
new StatusSocket(io).listen();