| | |
| | const debug = require('debug')('hyperion'); |
| | import 'raf/polyfill'; |
| | debug('Hyperion starting...'); |
| | debug('logging with debug enabled'); |
| | require('isomorphic-fetch'); |
| | import fs from 'fs'; |
| | import statsd from 'shared/middlewares/statsd'; |
| | import express from 'express'; |
| | import Loadable from 'react-loadable'; |
| | import path from 'path'; |
| | |
| | |
| | import { getUserById } from 'shared/db/queries/user'; |
| | import Raven from 'shared/raven'; |
| | import toobusy from 'shared/middlewares/toobusy'; |
| | import addSecurityMiddleware from 'shared/middlewares/security'; |
| | import { createProxyMiddleware } from 'http-proxy-middleware'; |
| |
|
| | const PORT = process.env.PORT || 3006; |
| | const ONE_HOUR = 3600; |
| |
|
| | const app = express(); |
| |
|
| | |
| | app.use(statsd); |
| |
|
| | |
| | app.set('trust proxy', true); |
| |
|
| | app.use(toobusy); |
| |
|
| | |
| | addSecurityMiddleware(app, { enableNonce: true, enableCSP: true }); |
| |
|
| | app.use( |
| | ['/api', '/api/**'], |
| | createProxyMiddleware({ |
| | target: 'https://api.spectrum.chat', |
| | changeOrigin: true, |
| | }) |
| | ); |
| |
|
| | app.use( |
| | ['/auth', '/auth/**'], |
| | createProxyMiddleware({ |
| | target: 'https://api.spectrum.chat', |
| | changeOrigin: true, |
| | }) |
| | ); |
| |
|
| | app.use( |
| | ['/websocket', '/websocket/**'], |
| | createProxyMiddleware({ |
| | target: 'https://api.spectrum.chat', |
| | changeOrigin: true, |
| | ws: true, |
| | }) |
| | ); |
| |
|
| | |
| | app.use( |
| | express.static( |
| | process.env.NODE_ENV === 'production' |
| | ? './build' |
| | : path.join(__dirname, '../build/'), |
| | { |
| | index: false, |
| | setHeaders: (res, path) => { |
| | |
| | if (path.indexOf('sw.js') > -1) { |
| | res.setHeader('Cache-Control', 'no-store, no-cache'); |
| | return; |
| | } |
| |
|
| | if (path.endsWith('.js')) { |
| | |
| | |
| | res.setHeader('Cache-Control', `s-maxage=${ONE_HOUR}`); |
| | } |
| | }, |
| | } |
| | ) |
| | ); |
| |
|
| | |
| | |
| | if (process.env.NODE_ENV === 'development') { |
| | app.use( |
| | express.static(path.resolve(__dirname, '..', 'public'), { index: false }) |
| | ); |
| | } |
| |
|
| | import bodyParser from 'body-parser'; |
| | app.use(bodyParser.json()); |
| |
|
| | if (process.env.NODE_ENV === 'development') { |
| | const logging = require('shared/middlewares/logging'); |
| | app.use(logging); |
| | } |
| |
|
| | if (process.env.NODE_ENV === 'production' && !process.env.FORCE_DEV) { |
| | |
| | const raven = require('shared/middlewares/raven').default; |
| | app.use(raven); |
| | } |
| |
|
| | |
| | import cors from 'shared/middlewares/cors'; |
| | app.use(cors); |
| |
|
| | |
| | |
| | if (process.env.NODE_ENV === 'development') { |
| | app.use('/sockjs-node', (req: express$Request, res: express$Response) => { |
| | res.redirect(301, `http://localhost:3000${req.path}`); |
| | }); |
| | } |
| |
|
| | import cookieParser from 'cookie-parser'; |
| | app.use(cookieParser()); |
| |
|
| | import session from 'shared/middlewares/session'; |
| | app.use(session); |
| |
|
| | import passport from 'passport'; |
| | |
| | passport.serializeUser((user, done) => { |
| | done(null, typeof user === 'string' ? user : JSON.stringify(user)); |
| | }); |
| |
|
| | |
| | |
| | |
| | passport.deserializeUser((data, done) => { |
| | |
| | try { |
| | const user = JSON.parse(data); |
| | |
| | |
| | if (user.id && user.createdAt) { |
| | return done(null, user); |
| | } |
| | |
| | } catch (err) {} |
| |
|
| | |
| | getUserById(data) |
| | .then(user => { |
| | done(null, user); |
| | }) |
| | .catch(err => { |
| | done(err); |
| | }); |
| | }); |
| | app.use(passport.initialize()); |
| | app.use(passport.session()); |
| |
|
| | |
| | import threadParamRedirect from 'shared/middlewares/thread-param'; |
| | app.use(threadParamRedirect); |
| |
|
| | app.get('*', (req: express$Request, res, next) => { |
| | |
| | if ( |
| | req.headers['user-agent'] && |
| | req.headers['user-agent'].indexOf('Electron') > -1 |
| | ) { |
| | return res.sendFile(path.resolve(__dirname, '../build/index.html')); |
| | } |
| | next(); |
| | }); |
| |
|
| | import renderer from './renderer'; |
| | app.get('*', renderer); |
| |
|
| | process.on('unhandledRejection', async err => { |
| | console.error('Unhandled rejection', err); |
| | try { |
| | await new Promise(res => Raven.captureException(err, res)); |
| | } catch (err) { |
| | console.error('Raven error', err); |
| | } finally { |
| | process.exit(1); |
| | } |
| | }); |
| |
|
| | process.on('uncaughtException', async err => { |
| | console.error('Uncaught exception', err); |
| | try { |
| | await new Promise(res => Raven.captureException(err, res)); |
| | } catch (err) { |
| | console.error('Raven error', err); |
| | } finally { |
| | process.exit(1); |
| | } |
| | }); |
| |
|
| | Loadable.preloadAll().then(() => { |
| | app.listen(PORT); |
| | debug( |
| | `Hyperion, the server-side renderer, running at http://localhost:${PORT}` |
| | ); |
| | }); |
| |
|