diff --git a/server/app.js b/server/app.js index 410599671..61605374c 100644 --- a/server/app.js +++ b/server/app.js @@ -2,7 +2,7 @@ /* This module imports */ require('dotenv/config') -import logger from './config/logger' +import logger from './logger' const globals = require('./globals') const sessions = require('./sessions') const express = require('express') @@ -10,7 +10,6 @@ const graphqlHTTP = require('express-graphql') const schema = require('./schema/schema') const mongoose = require('mongoose') const checkJwt = require('./auth/middleware/jwt') -const logger = require('./utils/logger') const wrongPreshared = require('./errors/notAllowed.json') const cors = require('cors') @@ -18,48 +17,54 @@ require('dotenv').config() const app = express() -// Checking preshared key -app.post('/graphql', (req, res, next) => { - if (req.headers.preshared === process.env.PRESHARED_GATEWAY_KEY) { - next() - } else { - res.send(wrongPreshared) - } -}) - -// Allow crosss-origin requests -app.use(cors()) - -// Database Connection -mongoose.connect(`mongodb://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@${process.env.DB_DOMAIN}`, { useNewUrlParser: true }) - -mongoose.connection.once('open', () => { - logger.info('Connected to the DB.') -}) - -/* Here we bind all requests to this endpoint to be procecced by the GraphQL Library. */ - -app.get('/graphql', graphqlHTTP({ - schema, - graphiql: true -})) - -app.post('/graphql', graphqlHTTP(req => { - return { - schema, - graphiql: false, - context: { - user: req.user, - userId: req.headers.userid +try { + // Checking preshared key + app.post('/graphql', (req, res, next) => { + if (req.headers.preshared === process.env.USERS_API_PRESHARED) { + next() + } else { + logger.error('Wrong Pre-Shared Received', req.headers.preshared) + res.send(wrongPreshared) } - } -})) + }) -app.post('/graphql', checkJwt, (err, req, res, next) => { - if (err) return res.status(401).send(`[Authenticate Token Error] ${err.message}`) - next() -}) + // Allow crosss-origin requests + app.use(cors()) + + // Database Connection + mongoose.connect(`mongodb://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@${process.env.DB_DOMAIN}`, { useNewUrlParser: true }) + + mongoose.connection.once('open', () => { + logger.info('Connected to the DB.') + }) + + /* Here we bind all requests to this endpoint to be procecced by the GraphQL Library. */ + + app.get('/graphql', graphqlHTTP({ + schema, + graphiql: true + })) + + app.post('/graphql', graphqlHTTP(req => { + return { + schema, + graphiql: false, + context: { + user: req.user, + userId: req.headers.userid + } + } + })) + + app.post('/graphql', checkJwt, (err, req, res, next) => { + if (err) return res.status(401).send(`[Authenticate Token Error] ${err.message}`) + next() + }) + + app.listen(4000, () => { + logger.info('Now listening for requests on port 4000') + }) +} catch (err) { + logger.error('Server Error', err) +} -app.listen(4000, () => { - logger.info('Now listening for requests on port 4000') -}) diff --git a/server/auth/middleware/jwt.js b/server/auth/middleware/jwt.js index f9c94b734..21c88e8c9 100644 --- a/server/auth/middleware/jwt.js +++ b/server/auth/middleware/jwt.js @@ -1,3 +1,4 @@ + const jwt = require('express-jwt') const jwksRsa = require('jwks-rsa') diff --git a/server/config/logger.js b/server/logger/index.js similarity index 100% rename from server/config/logger.js rename to server/logger/index.js