-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
119 lines (104 loc) · 2.94 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const { ApolloServer, PubSub } = require('apollo-server-express')
const expressPlayground = require('graphql-playground-middleware-express')
.default
const express = require('express')
const path = require('path')
const cors = require('cors')
const session = require('express-session')
const { readFileSync } = require('fs')
const { createServer } = require('http')
const { MongoClient } = require('mongodb')
const MongoDBStore = require('connect-mongodb-session')(session)
require('dotenv').config()
const typeDefs = readFileSync('./typeDefs.graphql', 'UTF-8')
const resolvers = require('./resolvers')
async function start() {
const app = express()
var store = new MongoDBStore({
uri: process.env.MONGO_SESSION_STORAGE,
collection: 'mySessions',
})
store.on('error', function (error) {
console.log(error)
})
app.set('trust proxy', 1)
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7,
httpOnly: true,
sameSite: 'none',
secure: true,
},
store: store,
saveUninitialized: false,
})
)
const MONGO_DB = process.env.DB_HOST
const client = await MongoClient.connect(MONGO_DB, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
const db = client.db()
const pubsub = new PubSub()
const context = async ({ req, connection }) => {
let users = db.collection('users')
let studentData = db.collection('studentData')
let lessonData = db.collection('lessonData')
let assignmentData = db.collection('assignmentData')
let unitData = db.collection('unitData')
let classPeriodData = db.collection('classPeriodData')
let generalInfo = db.collection('generalInfo')
return {
users,
studentData,
lessonData,
assignmentData,
classPeriodData,
unitData,
generalInfo,
db,
req,
pubsub,
}
}
const server = new ApolloServer({
typeDefs,
resolvers,
context,
introspection: true,
playground: true,
engine: {
apiKey: process.env.ENGINE_API_KEY,
},
})
server.applyMiddleware({
app,
cors: {
credentials: true,
origin: [
'http://localhost:3000',
'https://mrwetherall.org',
'https://mrwetherall-website.herokuapp.com',
],
},
})
app.get('/', (req, res) => res.end(`Teacher's Aide API`))
app.get('/playground', expressPlayground({ endpoint: '/graphql' }))
const httpServer = createServer(app)
server.installSubscriptionHandlers(httpServer)
// httpServer.listen({ port: process.env.PORT || 4000 }),
// () => {
// console.log(`🚀 Server ready at ${port}`)
// }
const PORT = process.env.PORT || 4000
httpServer.listen({ port: PORT }, () => {
console.log(`🚀 Server ready at ${server.graphqlPath}`)
console.log(
`🚀 Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`
)
})
}
start()