@@ -14,63 +14,51 @@ import toobusy from 'shared/middlewares/toobusy';
1414import addSecurityMiddleware from 'shared/middlewares/security' ;
1515import csrf from 'shared/middlewares/csrf' ;
1616import { init as initPassport } from './authentication.js' ;
17+ import apolloServer from './apollo-server' ;
18+ import { corsOptions } from 'shared/middlewares/cors' ;
19+ import errorHandler from 'shared/middlewares/error-handler' ;
20+ import middlewares from './routes/middlewares' ;
21+ import authRoutes from './routes/auth' ;
22+ import apiRoutes from './routes/api' ;
1723import type { DBUser } from 'shared/types' ;
24+ import type { Loader } from './loaders/types' ;
25+
26+ export type GraphQLContext = {
27+ user : DBUser ,
28+ updateCookieUserData : ( data : DBUser ) => Promise < void > ,
29+ loaders : {
30+ [ key : string ] : Loader ,
31+ } ,
32+ } ;
1833
1934const PORT = process . env . PORT ? parseInt ( process . env . PORT , 10 ) : 3001 ;
2035
21- // Initialize authentication
2236initPassport ( ) ;
2337
24- // API server
2538const app = express ( ) ;
2639
2740// Trust the now proxy
2841app . set ( 'trust proxy' , true ) ;
29-
30- // Return the request if the server is too busy
3142app . use ( toobusy ) ;
3243
3344// Security middleware.
3445addSecurityMiddleware ( app ) ;
35-
3646if ( process . env . NODE_ENV === 'production' && ! process . env . FORCE_DEV ) {
3747 app . use ( csrf ) ;
3848}
3949
40- // Send all responses as gzip
50+ // All other middlewares
4151app . use ( compression ( ) ) ;
42-
43- import middlewares from './routes/middlewares' ;
4452app . use ( middlewares ) ;
4553
46- import authRoutes from './routes/auth' ;
54+ // Routes
4755app . use ( '/auth' , authRoutes ) ;
48-
49- import apiRoutes from './routes/api' ;
5056app . use ( '/api' , apiRoutes ) ;
5157
52- // $FlowIssue
53- app . use (
54- (
55- err : Error ,
56- req : express$Request ,
57- res : express$Response ,
58- next : express$NextFunction
59- ) => {
60- if ( err ) {
61- console . error ( err ) ;
62- res
63- . status ( 500 )
64- . send (
65- 'Oops, something went wrong! Our engineers have been alerted and will fix this asap.'
66- ) ;
67- Raven . captureException ( err ) ;
68- } else {
69- return next ( ) ;
70- }
71- }
72- ) ;
58+ // GraphQL middleware
59+ apolloServer . applyMiddleware ( { app, path : '/api' , cors : corsOptions } ) ;
7360
61+ // Redirect a request to the root path to the main app
7462app . use ( '/' , ( req : express$Request , res : express$Response ) => {
7563 res . redirect (
7664 process . env . NODE_ENV === 'production' && ! process . env . FORCE_DEV
@@ -79,20 +67,12 @@ app.use('/', (req: express$Request, res: express$Response) => {
7967 ) ;
8068} ) ;
8169
82- import type { Loader } from './loaders/types' ;
83- export type GraphQLContext = {
84- user : DBUser ,
85- updateCookieUserData : ( data : DBUser ) => Promise < void > ,
86- loaders : {
87- [ key : string ] : Loader ,
88- } ,
89- } ;
90-
91- const server = createServer ( app ) ;
70+ // $FlowIssue
71+ app . use ( errorHandler ) ;
9272
93- // Create subscriptions server at /websocket
94- import createSubscriptionsServer from './routes/create-subscription-server' ;
95- const subscriptionsServer = createSubscriptionsServer ( server , '/websocket' ) ;
73+ // We need to create a separate HTTP server to handle GraphQL subscriptions via websockets
74+ const httpServer = createServer ( app ) ;
75+ apolloServer . installSubscriptionHandlers ( httpServer ) ;
9676
9777// Start API wrapped in Apollo Engine
9878const engine = new ApolloEngine ( {
@@ -119,10 +99,11 @@ const engine = new ApolloEngine({
11999
120100engine . listen ( {
121101 port : PORT ,
122- httpServer : server ,
102+ httpServer : httpServer ,
123103 graphqlPaths : [ '/api' ] ,
124104} ) ;
125- debug ( `GraphQL server running at http://localhost:${ PORT } /api` ) ;
105+
106+ debug ( `GraphQL API running at http://localhost:${ PORT } /api` ) ;
126107
127108process . on ( 'unhandledRejection' , async err => {
128109 console . error ( 'Unhandled rejection' , err ) ;
0 commit comments