import bodyParser from 'body-parser';
import express from 'express';
import path from 'path';
import query from 'resolve-query';
import commandHandler from 'resolve-command';
import request from 'request';
import ssr from './render';

import eventStore from './event_store';
import config from '../configs/server.config.js';

const STATIC_PATH = '/static';
const rootDirectory = process.env.ROOT_DIR || '';

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

const executeQuery = query({
    eventStore,
    readModels: config.queries
});

const executeCommand = commandHandler({
    eventStore,
    aggregates: config.aggregates
});

if (config.gateways) {
    config.gateways.forEach(gateway => gateway({ executeQuery, executeCommand, eventStore }));
}

app.use((req, res, next) => {
    req.resolve = {
        executeQuery,
        executeCommand,
        eventStore
    };
    next();
});

try {
    config.extendExpress(app);
} catch (err) {}

app.get(`${rootDirectory}/api/queries/:queryName`, (req, res) => {
    const queryParameters = [req.params.queryName];
    if (req.query && req.query.graphql) {
        queryParameters.push(req.query.graphql);
        if (req.query.variables) {
            queryParameters.push(req.query.variables);
        }
    }

    executeQuery(...queryParameters).then(state => res.status(200).json(state)).catch((err) => {
        res.status(500).end('Query error: ' + err.message);
        // eslint-disable-next-line no-console
        console.log(err);
    });
});

app.post(`${rootDirectory}/api/commands`, (req, res) => {
    executeCommand(req.body).then(() => res.status(200).send('ok')).catch((err) => {
        res.status(500).end('Command error:' + err.message);
        // eslint-disable-next-line no-console
        console.log(err);
    });
});

const staticMiddleware = process.env.NODE_ENV === 'production'
    ? express.static(path.join(__dirname, '../../dist/static'))
    : (req, res) => {
        var newurl = 'http://localhost:3001' + req.path;
        request(newurl).pipe(res);
    };

app.use(`${rootDirectory}${STATIC_PATH}`, staticMiddleware);

app.get([`${rootDirectory}/*`, `${rootDirectory || '/'}`], async (req, res) => {
    try {
        const state = await config.initialState(executeQuery, {
            cookies: req.cookies,
            hostname: req.hostname,
            originalUrl: req.originalUrl,
            body: req.body,
            query: req.query
        });

        ssr(state, { req, res });
    } catch (err) {
        res.status(500).end('SSR error: ' + err.message);
        // eslint-disable-next-line no-console
        console.log(err);
    }
});

export default app;

@@ -1047,16 +1047,22 @@
 ryName%60,
+ async
  (req, r
@@ -1066,24 +1066,38 @@
 , res) =%3E %7B%0A
+    try %7B%0A    
     const qu
@@ -1136,24 +1136,28 @@
 yName%5D;%0A    
+   
+ 
 if (req.quer
@@ -1178,24 +1178,28 @@
 .graphql) %7B%0A
+    
         quer
@@ -1235,32 +1235,36 @@
 aphql);%0A        
+  
+  
 if (req.query.va
@@ -1278,32 +1278,36 @@
 ) %7B%0A            
+    
 queryParameters.
@@ -1303,32 +1303,43 @@
 Parameters.push(
+JSON.parse(
 req.query.variab
@@ -1342,18 +1342,23 @@
 riables)
+)
 ;%0A
+    
         
@@ -1367,14 +1367,41 @@
     
-%7D%0A%0A
    
+ %7D%0A        const state = await
  exe
@@ -1433,22 +1433,17 @@
 ers)
-.then(state =%3E
+;%0A       
  res
@@ -1466,32 +1466,35 @@
 n(state)
-).
+;%0A    %7D 
 catch
-(
+ 
 (err)
- =%3E
  %7B%0A     
@@ -1622,26 +1622,24 @@
 (err);%0A    %7D
-);
 %0A%7D);%0A%0Aapp.po

Fix parse variables (#180)