-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
executable file
·85 lines (79 loc) · 2.73 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
// npm pacakages
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const http = require('http');
//const { resolve } = require('path');
const { ApolloServer } = require('apollo-server-express');
const { ApolloServerPluginDrainHttpServer } = require('apollo-server-core');
const { MongoClient } = require('mongodb');
// app imports
const rootRouter = require('./routers/root');
const cruisesRouter = require('./routers/cruises');
const bookingsRouter = require('./routers/bookings');
const customersRouter = require('./routers/customers');
const init = require('./utilities/init');
const logger = require('./utilities/loggers');
const typeDefs = fs.readFileSync('./data/schema.graphql',{encoding: 'utf-8'});
const resolvers = require('./routers/graphql');
//globals
app = express();
app.use(bodyParser.json());
app.use('/', rootRouter);
app.use('/cruises', cruisesRouter);
app.use('/bookings', bookingsRouter);
app.use('/customers', customersRouter);
// Set up the server
async function startServer(typeDefs, resolvers) {
const httpServer = http.createServer(app);
const server = new ApolloServer({
typeDefs,
resolvers,
graphiql: true,
csrfPrevention: true,
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
context: ({ req }) => {
const locale = req.headers.locale;
return { locale };
}
});
await server.start();
server.applyMiddleware({ app });
host = require('os').hostname();
port = 7777;
await new Promise(resolve => httpServer.listen({ port: port }, resolve));
logger.verbose(`Startup: NodeTours listening at http://${host}:${port}`);
}
// Set up the Mongo DB connection
init.setDBConnectionString(process);
const url = `mongodb://${dbhost}:${dbport}/`;
const dbName = 'nodetours';
const dbClient = new MongoClient(url);
async function connectToDatabase() {
try {
await dbClient.connect();
await dbClient.db("admin").command({ ping: 1});
logger.verbose(`Startup: Succesully connected to DB at mongodb://${dbhost}:${dbport}/${dbName}`);
db = dbClient.db(dbName);
init.initDB(db);
} catch (error) {
logger.error(`Startup: Database connection could not be established. The app will exit.`);
logger.error(` Error: ${error}`);
}
}
// Connect to the DB and start the server
connectToDatabase().catch(console.error);
startServer(typeDefs, resolvers);
// Close db connection when interrupted
// SIGINT e.g. Ctrl+C
process.on('SIGINT', () => {
logger.info(`Received SIGINT. Closing DB connections and exiting`);
dbClient.close();
process.exit();
});
//SIGTERM e.g. kill without -9
process.on('SIGTERM', () => {
logger.info(`Received SIGTERM. Closing DB connections and exiting`);
dbClient.close();
process.exit();
});