-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
150 lines (124 loc) · 4.12 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const throng = require('throng');
const WORKERS = process.env.WEB_CONCURRENCY || 1;
const port = process.env.PORT || 5000;
require('dotenv').config();
const app = require('express')();
const passport = require('passport');
const cors = require('cors');
const { buildContext } = require('graphql-passport');
const session = require('express-session');
app.use(cors({
origin: true,
methods: 'POST, GET, OPTIONS',
credentials: true
}));
/** Session middleware to create and parse cookies */
const sessionMiddleware = session({
secret: process.env.SESS_SECRET || "not really a secret",
resave: false,
saveUninitialized: false,
cookie: { secure: process.env.NODE_ENV === 'production' }
});
passport.use(require('./utils/localStrategy'));
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
done(null, user);
});
app.use(sessionMiddleware);
app.use(passport.initialize());
app.use(passport.session());
const { Subject } = require('rxjs');
const { ApolloServer } = require('apollo-server-express');
const { AuthenticationError } = require('apollo-server');
const { mergeSchemas, makeExecutableSchema } = require('graphql-tools');
let schemas = [];
let gitHubSchema = null;
let server = null;
/** Subject that will be used to notify the controller to update the schema
* @type {Subject} */
const subject = new Subject();
const GitHubSchemaController = require('./controllers/GitHubSchemaController').controller;
/** Instance of GitHubSchemaController to manage and create GitHub schema used for stitching */
const controller = new GitHubSchemaController('https://api.github.com/graphql', subject);
const multer = require('multer');
const fileFilter = require('./utils/fileFilter');
const upload = multer({ storage: require('./utils/diskStorage'), fileFilter });
const fs = require('fs');
app.post('/avatar', upload.single('avatar'), (req, res) => {
res.status(200).json({
status: 200,
message: 'File uploaded',
pictureUrl: req.file.filename
})
});
app.get('/avatar/:hex', (req, res) => {
const { hex } = req.params;
try {
res.setHeader('Content-Type', 'image/png');
res.sendFile(`${process.env.STORAGE_DIR}/avatars/${hex}`);
} catch {
res.setHeader('ContentType', 'application/json');
res.status(404).json({
status: 404,
message: 'File not found'
})
}
});
/** Initialization of server, will be called by throng
* @async
* @function start */
const start = async () => {
app.get('/', (req, res) => {
res.status(200).json({
status: 200,
message: 'Ok!'
})
});
/** Uses the GitHub schema controller to create it and stitch it with Electra's schema
* @async
* @function initSchemas */
const initSchemas = async () => {
if(server) server.stop();
gitHubSchema = await controller.createGHSchema('https://api.github.com/graphql', subject);
schemas = [];
const { typeDefs } = require('./schemas/electra.schema');
const electraResolvers = require('./resolvers/index').resolvers;
const { linkTypeDefs } = require('./schemas/extensions.schema');
const linkResolvers = require('./resolvers/gitHub').getResolvers(gitHubSchema);
const electraSchema = makeExecutableSchema({
typeDefs,
resolvers: electraResolvers
});
schemas.push(electraSchema);
schemas.push(gitHubSchema);
schemas.push(linkTypeDefs);
const mainSchema = mergeSchemas({
schemas,
...linkResolvers
});
server = new ApolloServer({
schema: mainSchema,
introspection: true,
playground: true,
debug: true,
tracing: true,
context: ({ req, res }) => ({
headers: req.headers,
...buildContext({ req, res })
}),
formatError: (err) => (err.message === 'Response not successful: Received status code 401' ? new AuthenticationError('GitHub token not valid.') : err)
});
server.applyMiddleware({ app, path: '/', cors: false });
}
subject.subscribe({
next: initSchemas
});
subject.next();
app.listen({ port }, () => { console.log(`Listening on port ${port}`) });
}
throng({
workers: WORKERS,
lifetime: Infinity
}, start);