-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.ts
173 lines (149 loc) · 5.22 KB
/
index.ts
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { ApolloServer, makeExecutableSchema } from 'apollo-server-express';
import { DIRECTIVES } from '@graphql-codegen/typescript-mongodb';
import express from 'express';
import passport from 'passport';
import session from 'express-session';
import helmet from 'helmet';
import MongoStore, { MongoUrlOptions } from 'connect-mongo';
import gqlSchema from '../common/schema.graphql';
import { resolvers } from './resolvers';
import DB from './models';
import Context from './context';
import logger from './logger';
import { StrategyNames, registerAuthRoutes } from './auth';
import { UnsubscribeHandler } from './mail/handlers';
import { UserDbInterface } from './generated/graphql';
import { pullCalendar } from './events';
// import vakenConfig from '../../vaken.config';
import { serverPlugins, authPlugins } from './plugins';
const { SESSION_SECRET, PORT, CALENDARID, NODE_ENV } = process.env;
if (!SESSION_SECRET) throw new Error(`SESSION_SECRET not set`);
if (!PORT) throw new Error(`PORT not set`);
if (!CALENDARID) logger.info('CALENDARID not set; skipping ical integration');
logger.info(`Node env: ${NODE_ENV}`);
const app = express();
const pluginResolvers = serverPlugins.map(serverPlugin => {
return serverPlugin.resolvers as {};
});
const pluginTypeDefs = serverPlugins.map(serverPlugin => {
return serverPlugin.schema as {};
});
export const schema = makeExecutableSchema({
resolverValidationOptions: {
requireResolversForAllFields: true,
requireResolversForResolveType: false,
},
resolvers: [resolvers as {}, ...pluginResolvers],
typeDefs: [DIRECTIVES, gqlSchema, ...pluginTypeDefs],
});
(async () => {
const dbClient = new DB();
const models = await dbClient.collections;
app.use(helmet()); // sets good security defaults, see https://helmetjs.github.io/
// Register auth functions
app.use(
session({
secret: SESSION_SECRET,
store: new (MongoStore(session))(({
clientPromise: dbClient.client,
} as unknown) as MongoUrlOptions),
// resave: false,
saveUninitialized: true,
cookie: { maxAge: 365 * 24 * 60 * 60 * 1000 },
// cookie: {
// /*
// can't use secure cookies b/c only HTTP connection between dyno and Heroku servers,
// but don't need it as long as connection as Heroku servers and client are HTTPS
// */
// // secure: IS_PROD,
// // httpOnly: true, // protects against XSS attacks
// signed: true,
// sameSite: true,
// },
})
);
app.use(passport.initialize());
app.use(passport.session());
// passport.use('github', strategies.github(models));
// passport.use('google', strategies.google(models));
// passport.use('microsoft', strategies.microsoft(models));
// array to hold all oAuth strategies to be used with registering routes and working with passport
const oAuthStrategies: StrategyNames[] = [];
// iterate through config, pulling out oauth packages and generating their passport configuration
authPlugins.forEach(config => {
passport.use(config.name, config.strategy(models));
// Add this strategy to the oAuthStrategies array
oAuthStrategies.push({
name: config.name,
displayName: config.displayName,
scopes: config.scopes,
});
// console.error(config); // sanity check for auth plugin
});
// vakenConfig
// .filter(({ scopes }) => scopes.includes('oauth'))
// // could use map but map making other changes is less idomatic.
// .forEach(config => {
// passport.use(config.name, config.strategy(models));
// oAuthStrategies.push({
// name: config.name,
// svgPath: config.logo,
// displayName: config.displayName,
// });
// console.error(config);
// });
registerAuthRoutes(app, oAuthStrategies);
app.use((req, res, next) =>
passport.authenticate(
[
'session',
...oAuthStrategies.map(config => {
return config.name;
}),
],
(err, user) => {
if (err) return void next();
return void req.login(user, next);
}
)(req, res, next)
);
// Email unsubscribe link
app.use('/api/unsubscribe', UnsubscribeHandler(models));
// Pull events callback
app.use('/api/manage/events/pull', async (req, res) => {
const calendar = await pullCalendar(CALENDARID);
res.send(calendar);
});
const db = (await dbClient.client).db('vaken');
const server = new ApolloServer({
context: ({ req }): Context => ({
db,
models,
user: req.user as UserDbInterface | undefined,
}),
formatError: error => {
logger.error(error);
// give friendly error message to frontend, hide internal server details
return new Error(error.message);
},
// introspection: NODE_ENV !== 'production', // OFF by default in prod for security reasons
// playground: NODE_ENV !== 'production', // by DEFAULT, enabled when not in prod + disabled in prod
schema,
});
server.applyMiddleware({ app });
if (NODE_ENV !== 'development') {
logger.info('Setting routing to prod assets.');
// Serve front-end asset files in prod.
app.use(express.static('dist/src/server/app'));
// MUST BE LAST AS THIS WILL REROUTE ALL REMAINING TRAFFIC TO THE FRONTEND!
app.use((req, res) => {
res.sendFile('index.html', { root: 'dist/src/server/app' });
});
}
app.listen(
{ port: PORT },
() => void logger.info(`Server ready at http://localhost:${PORT}${server.graphqlPath}`)
);
})();
// for testing
export default app;