Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/react-server / src / app.tsx
Size: Mime:
// global & builtin
import { resolve as pathResolve } from 'path'
/**
 * @todo !!!!!!!!!!!!
 * import 'src/bootstrapper/setup'
 */
// external
import express from 'express'
import { Application } from 'express'
import compression from 'compression'
import cookieParser from 'cookie-parser'
import bodyParser from 'body-parser'
// local
import { config } from '@skava/di'
import { reactApplication } from './serverSideRender/reactApplication'
import security from './middleware/security'
import { clientBundle } from './middleware/clientBundle'
import { routingMiddleware } from './middleware/routing'
import errorHandlers from './middleware/errorHandlers'
import { runGraphqlServer } from './middleware/graphql'
import { emptyMiddleware } from './middleware/empty'
import { fsMiddleware } from './middleware/fsMiddleware'
import { cacheMiddleware } from './middleware/cacheMiddleware'
import { sessionPlugin, killer, fromReqResToDebug } from './deps'

if (process.env.LOG_TRACE_LOGS) {
  require('fliplog').trackConsole()
}

const apiBaseUrl = config.get('proxyUrl').replace('http://localhost:3000', '')
console.log('[appserver] apiBaseUrl', apiBaseUrl)

// Create our express based server.
const app = express()

const dynamic = {
  protection: emptyMiddleware,
}

// @lint @todo thiis just uses a lot of middleware
// eslint-disable-next-line
function decorateApp(expressApp: Application = app) {
  console.log('[appserver] decorateApp')

  // @note we use the get config because
  // 1. we build the node server with webpack still, unfortunately
  // 2. when we do that, we need to keep values dynamic & not replaced by env/define + shake/drop
  // const webPath = getConfigForEnv('bundles.client.webPath')
  const webPath = process.env.WEB_PATH

  // @todo may need to disable this in dev
  // hmrDecorator(expressApp)
  expressApp.use(fsMiddleware)

  // Configure serving of our client bundle.
  expressApp.use(webPath, clientBundle)

  // Configure static serving of our "public" root http path static files.
  // Note: these will be served off the root (i.e. '/') of our application.
  const mediaAssetsPath = pathResolve(
    config.get('appRootDir'),
    process.env.BUILD_CONFIG_CLIENT_ASSETS_PATH
  )
  const clientPath = pathResolve(
    config.get('appRootDir'),
    './dist/dist/bundled/client/'
  )
  expressApp.use(express.static(clientPath))
  expressApp.use(express.static(mediaAssetsPath))

  // Security middlewares.
  expressApp.use(...security)
  expressApp.use(cookieParser())
  expressApp.use(bodyParser.json())
  expressApp.use(
    bodyParser.urlencoded({
      extended: true,
    })
  )

  if (process.env.NODE_ENV === 'production') {
    app.use(compression)
  }

  // if (process.env.SHOULD_LOG_REQUEST_MORGAN) {
  //   expressApp.use(morgan('combined'))
  // }

  // sessions - this can just be a middleware not a plugin right??
  dynamic.protection = sessionPlugin(expressApp)
}

function setupAppSync(expressApp: Application = app) {
  /**
   * @description The React application middleware.
   * @note respondRedirect was used here
   */
  expressApp.get(
    '*',
    routingMiddleware,
    cacheMiddleware,
    dynamic.protection,
    reactApplication
  )

  // Error Handler middlewares.
  expressApp.use(...errorHandlers)
}

async function setupAppAsync(expressApp: Application = app) {
  console.log('[appserver] setupAppAsync')
  // if (isFromApp) {
  //   if (process.env.IS_DEV_BUILD) {
  //     return app
  //   }
  // }

  /**
   * @note !!! it wasn't waiting until the async was done, cause of an issue
   */
  await runGraphqlServer(expressApp)
  setupAppSync(expressApp)
}

function setupApp(expressApp: Application = app) {
  console.log('[appserver] setupApp')
  decorateApp(expressApp)
  setupAppAsync(expressApp)
}

/**
 * We export the listener as it will be handy for our development hot reloader,
 * or for exposing a general extension layer for application customisations.
 */
function withKiller(expressApp: Application = app) {
  const appWithKiller = killer(expressApp)
  return appWithKiller
}

function start(expressApp: Application = app) {
  const PORT = process.env.PORT || 3000
  const onListen = () => {
    console.log('[deverserver] running app on port ', PORT)
  }
  setupApp(expressApp)
  return expressApp.listen(PORT, onListen)
}

export { start, withKiller, decorateApp, setupApp, app }