-
Notifications
You must be signed in to change notification settings - Fork 29
/
server.js
178 lines (156 loc) · 4.78 KB
/
server.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* eslint-disable no-console */
// Hapi server imports
import Hapi from 'hapi';
import Inert from 'inert';
import Vision from 'vision';
import swagger from 'hapi-swagger';
import jwt from 'hapi-auth-jwt2';
import api from './api';
import rooms from './rooms';
import issueToken from './issueToken';
// React imports
import React from 'react';
import { renderToString } from 'react-dom/server';
// React-router routes and history imports
import getRoutes from './routes';
import createHistory from 'history/lib/createMemoryHistory';
// Redux imports
import { Provider } from 'react-redux';
// Import Intl
import { IntlProvider } from 'react-intl';
import * as lang from './lang';
// Configure Redux Store
import configureStore from './store/configureStore';
// Redux router imports
import { ReduxRouter } from 'redux-router';
import { reduxReactRouter, match } from 'redux-router/server';
import qs from 'query-string';
// Import helpers
import ApiClient from './helpers/ApiClient';
import Html from './helpers/Html';
// Import config file
import config from './config';
// Start server function
export default function( callback ) {
// Create the Walmart Labs Hapi Server
const server = new Hapi.Server(); // No debug no production
// Configure connections
server.connection({
host: SERVER_HOST,
port: SERVER_PORT,
labels: [ 'api' ],
routes: {
cors: {
origin: [ PROTOCOL + SERVER_HOST + ':' + WS_PORT ]
}
}
});
server.connection({ host: SERVER_HOST, port: WS_PORT, labels: [ 'ws' ] });
// Set names for each connection
server.connections[0].name = 'API';
server.connections[1].name = 'WS';
const apiServer = server.select('api');
const wsServer = server.select('ws');
server.register([
{
register: Inert
}, {
register: Vision
}, {
register: jwt
}, {
register: issueToken
}, {
register: api,
routes: {
prefix: config.api.routes.path
}
}, {
register: rooms,
options: {
server: wsServer
}
}
], (err) => {
if (err) {
throw err;
}
server.register([
{
register: swagger,
options: {
documentationPath: config.api.swagger.documentationPath
}
}
], { select: [ 'api' ] }, ( error ) => {
if ( error ) {
return console.error( error );
}
/**
* Attempt to serve static requests from the public folder.
*/
apiServer.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: 'static'
}
}
});
/**
* Cookie Settings
*/
apiServer.state('USER_SESSION', {
ttl: null,
isSecure: !__DEVELOPMENT__,
isHttpOnly: false,
encoding: 'none',
clearInvalid: false, // remove invalid cookies
strictHeader: false // don't allow violations of RFC 6265
});
server.ext( 'onPreResponse', ( request, reply ) => {
if ( typeof request.response.statusCode !== 'undefined' ) {
return reply.continue();
}
const client = new ApiClient(request);
const store = configureStore(reduxReactRouter, getRoutes, createHistory, client);
const output = (
renderToString( <Html store={ store }/> )
);
const hydrateOnClient = () => {
reply( '<!doctype html>\n' + output ).code(500);
};
store.dispatch( match( request.path, ( error, redirectLocation, routerState ) => {
if ( redirectLocation ) {
reply.redirect( redirectLocation.pathname + redirectLocation.search );
} else if ( error || !routerState ) {
hydrateOnClient();
} else if ( routerState ) {
if (routerState.location.search && !routerState.location.query) {
routerState.location.query = qs.parse(routerState.location.search);
}
store.getState().router.then(() => {
const component = (
< Provider store={ store } key="provider">
<IntlProvider key={ store.getState().user.data.locale }
locale={ store.getState().user.data.locale }
messages={ lang[store.getState().user.data.locale] }>
<ReduxRouter/>
</IntlProvider>
</Provider>
);
const output = (
renderToString( <Html component={ component } store={ store } /> )
);
reply( '<!doctype html>\n' + output);
})
.catch(err => reply('error: ' + err));
}
}));
});
});
});
// Start Development Server
return server.start(() => callback( server ));
}