From c2960123eeb27fffbb6d8f5b1e67f30cc4a8d26d Mon Sep 17 00:00:00 2001 From: Nikola Bjelo Date: Thu, 1 Nov 2018 08:04:53 +0100 Subject: [PATCH] a minimal refactoring of the entrypoint --- server/src/app.js | 40 ++++++++++++++++++++++ server/src/db.js | 9 +++++ server/src/errors/index.js | 2 +- server/src/index.js | 69 +++++++++----------------------------- 4 files changed, 65 insertions(+), 55 deletions(-) create mode 100644 server/src/app.js create mode 100644 server/src/db.js diff --git a/server/src/app.js b/server/src/app.js new file mode 100644 index 000000000..b36f54aef --- /dev/null +++ b/server/src/app.js @@ -0,0 +1,40 @@ +import 'dotenv/config'; +import express from 'express'; +import cors from 'cors'; +import graphqlHTTP from 'express-graphql'; +import expressPlayground from 'graphql-playground-middleware-express'; +import * as Sentry from '@sentry/node'; +import schema from './schema'; + +const app = express(); + +// Initialising sentry +Sentry.init({ dsn: process.env.SENTRY_DNS }); +app.use(Sentry.Handlers.requestHandler()); + +// authorising CORS +app.use(cors()); + +// Starting the graphql server +app.use('/graphql', graphqlHTTP(req => ({ + schema, + context: { + userId: req.headers.userid, + authorization: req.headers.authorization, + }, + formatError(err) { + return { + message: err.message, + code: err.originalError && err.originalError.code, + path: err.path, + }; + }, +}))); + +// Starting the graphql server +app.get('/playground', expressPlayground({ endpoint: '/graphql' })); + +// Sentry error handling middleware if anything pass through +app.use(Sentry.Handlers.errorHandler()); + +export default app; diff --git a/server/src/db.js b/server/src/db.js new file mode 100644 index 000000000..4492e884f --- /dev/null +++ b/server/src/db.js @@ -0,0 +1,9 @@ +import 'dotenv/config'; +import mongoose from 'mongoose'; + +mongoose.connect(`mongodb://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@${process.env.DB_DOMAIN}`, { useNewUrlParser: true }); +mongoose.Promise = global.Promise; + +const db = mongoose.connection; + +export default db; diff --git a/server/src/errors/index.js b/server/src/errors/index.js index 15b0635a1..16c3974f1 100644 --- a/server/src/errors/index.js +++ b/server/src/errors/index.js @@ -4,7 +4,7 @@ class ClientError extends Error { constructor(args) { super(args); Sentry.configureScope((scope) => { - scope.setLevel('info'); + scope.setLevel('debug'); }); Sentry.captureException(this); } diff --git a/server/src/index.js b/server/src/index.js index ee5d09cfe..f9b2c65f3 100644 --- a/server/src/index.js +++ b/server/src/index.js @@ -1,60 +1,21 @@ /* eslint-disable no-console */ -import express from 'express'; -import 'dotenv/config'; -import cors from 'cors'; -import mongoose from 'mongoose'; -import graphqlHTTP from 'express-graphql'; -import expressPlayground from 'graphql-playground-middleware-express'; import * as Sentry from '@sentry/node'; +import db from './db'; +import app from './app'; -import schema from './schema'; - -// Initialise express -const app = express(); - -// Initialising sentry -Sentry.init({ dsn: process.env.SENTRY_DNS }); -app.use(Sentry.Handlers.requestHandler()); - -// Allow cross origing request -app.use(cors()); -/* ------------------------------------------------- ----------------- Database setup -------------------- -------------------------------------------------- */ -// Database Connection -mongoose.connect(`mongodb://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@${process.env.DB_DOMAIN}`, { useNewUrlParser: true }); - -// Get Mongoose to use the global promise library -mongoose.Promise = global.Promise; - -// Get the default connection -const db = mongoose.connection; - -// bind error and success to console log -db.on('error', () => { console.error.bind(console, 'MongoDB connection error:'); }); -db.once('open', () => { console.log('Connected to the DB.'); }); - -/* ------------------------------------------------- ------------------ Graphql setup -------------------- -------------------------------------------------- */ -app.use('/graphql', graphqlHTTP(req => ({ - schema, - context: { - userId: req.headers.userid, - authorization: req.headers.authorization, - }, - formatError(err) { - return { - message: err.message, - code: err.originalError && err.originalError.code, - path: err.path, - }; - }, -}))); -app.get('/playground', expressPlayground({ endpoint: '/graphql' })); - -// Sentry error handling middleware -app.use(Sentry.Handlers.errorHandler()); +// Bind server success and error to sentry +db.on('error', (err) => { + Sentry.configureScope((scope) => { + scope.setLevel('error'); + }); + Sentry.captureException(err); +}); +db.once('open', () => { + Sentry.configureScope((scope) => { + scope.setLevel('info'); + }); + Sentry.captureMessage('Connected to the DB.'); +}); app.listen(process.env.PORT, () => { console.log(`Listening on port ${process.env.PORT}`);