-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (65 loc) · 1.85 KB
/
index.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
require('dotenv').config()
const morgan = require('morgan')
const express = require('express')
const bodyParser = require('body-parser')
const errorhandler = require('errorhandler')
const { ApolloServer, gql } = require('apollo-server-express')
const requireGraphQLFile = require('require-graphql-file')
const Bearer = require('@bearer/node-agent')
const resolvers = require('./src/graphql/resolvers.js')
const logger = require('./src/config/logger')
const isDevelopment = process.env.NODE_ENV === 'development'
Bearer.init({
secretKey: process.env.BEARER_SH_API_KEY,
stripSensitiveData: true,
environment: process.env.BEARER_SH_ENV || process.env.NODE_ENV
})
const app = express()
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(morgan('combined'))
app.use('/assets', express.static('assets'))
if (isDevelopment) {
app.use(errorhandler())
}
// Error handlers & middlewares
if (isDevelopment) {
app.use((err, req, res, next) => {
res.status(err.status || 500)
res.send({
errors: {
message: err.message,
error: err
}
})
})
}
app.use((err, req, res, next) => {
res.status(err.status || 500)
res.send({
errors: {
message: err.message,
error: {}
}
})
})
const typeDefSchema = requireGraphQLFile('./src/graphql/typeDefs.graphql')
const typeDefs = gql(typeDefSchema)
const server = new ApolloServer({
typeDefs: typeDefs,
resolvers: resolvers,
context: ({ req, res }) => ({
...{ userContext: req.payload, ipAddress: getIpAdressFromRequest(req) }
})
})
const getIpAdressFromRequest = (req) => {
let ipAddr = req.headers['x-forwarded-for']
if (ipAddr) {
ipAddr = ipAddr.split(',')[0]
} else {
ipAddr = req.connection.remoteAddress || req.ip
}
return ipAddr
}
server.applyMiddleware({ app })
app.listen(process.env.PORT, () => logger.info(`Server running on http://localhost:${process.env.PORT}`))