a minimal refactoring of the entrypoint

This commit is contained in:
Nikola Bjelo committed 2018-11-01 08:04:53 +01:00
1 parent faa235dc02
commit c2960123ee
4 files changed
+65 -55

No files matched your search

+40
View File
@@ -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;
+9
View File
@@ -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;
+1 -1
View File
@@ -4,7 +4,7 @@ class ClientError extends Error {
constructor(args) {
super(args);
Sentry.configureScope((scope) => {
scope.setLevel('info');
scope.setLevel('debug');
});
Sentry.captureException(this);
}
+15 -54
View File
@@ -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}`);