2019-01-04 22:44:48 +03:00
|
|
|
var path = require('path');
|
|
|
|
var express = require('express');
|
|
|
|
var morgan = require('morgan');
|
|
|
|
var bodyParser = require('body-parser');
|
|
|
|
var cors = require('cors');
|
|
|
|
var graphiqlMiddleware = require('./middleware/graphiql');
|
|
|
|
var graphqlMiddleware = require('./middleware/graphql');
|
|
|
|
|
|
|
|
var app = express.Router();
|
|
|
|
|
|
|
|
function serveUmdModule(name) {
|
2019-01-10 21:51:14 +03:00
|
|
|
app.use(
|
|
|
|
express.static(
|
|
|
|
require.resolve(name).match(/.*\/(node_modules|packages)\/[^/]+\//)[0] +
|
|
|
|
'umd'
|
|
|
|
)
|
|
|
|
);
|
2019-01-04 22:44:48 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
function routes(options, store, scServer) {
|
2019-01-04 22:44:48 +03:00
|
|
|
var limit = options.maxRequestBody;
|
|
|
|
var logHTTPRequests = options.logHTTPRequests;
|
2019-01-10 21:51:14 +03:00
|
|
|
|
2019-01-04 22:44:48 +03:00
|
|
|
if (logHTTPRequests) {
|
2019-01-10 21:51:14 +03:00
|
|
|
if (typeof logHTTPRequests === 'object')
|
|
|
|
app.use(morgan('combined', logHTTPRequests));
|
2019-01-04 22:44:48 +03:00
|
|
|
else app.use(morgan('combined'));
|
|
|
|
}
|
|
|
|
|
|
|
|
app.use('/graphiql', graphiqlMiddleware);
|
|
|
|
|
|
|
|
serveUmdModule('react');
|
|
|
|
serveUmdModule('react-dom');
|
|
|
|
serveUmdModule('redux-devtools-core');
|
|
|
|
|
2019-01-10 21:51:14 +03:00
|
|
|
app.get('/port.js', function(req, res) {
|
2019-01-04 22:44:48 +03:00
|
|
|
res.send('reduxDevToolsPort = ' + options.port);
|
|
|
|
});
|
2019-01-10 21:51:14 +03:00
|
|
|
app.get('*', function(req, res) {
|
2019-01-04 22:44:48 +03:00
|
|
|
res.sendFile(path.join(__dirname, '../app/index.html'));
|
|
|
|
});
|
|
|
|
|
2019-01-10 21:51:14 +03:00
|
|
|
app.use(cors({ methods: 'POST' }));
|
|
|
|
app.use(bodyParser.json({ limit: limit }));
|
|
|
|
app.use(bodyParser.urlencoded({ limit: limit, extended: false }));
|
2019-01-04 22:44:48 +03:00
|
|
|
|
|
|
|
app.use('/graphql', graphqlMiddleware(store));
|
|
|
|
|
2019-01-10 21:51:14 +03:00
|
|
|
app.post('/', function(req, res) {
|
2019-01-04 22:44:48 +03:00
|
|
|
if (!req.body) return res.status(404).end();
|
|
|
|
switch (req.body.op) {
|
|
|
|
case 'get':
|
2019-01-10 21:51:14 +03:00
|
|
|
store
|
|
|
|
.get(req.body.id)
|
|
|
|
.then(function(r) {
|
|
|
|
res.send(r || {});
|
|
|
|
})
|
|
|
|
.catch(function(error) {
|
|
|
|
console.error(error); // eslint-disable-line no-console
|
|
|
|
res.sendStatus(500);
|
|
|
|
});
|
2019-01-04 22:44:48 +03:00
|
|
|
break;
|
|
|
|
case 'list':
|
2019-01-10 21:51:14 +03:00
|
|
|
store
|
|
|
|
.list(req.body.query, req.body.fields)
|
|
|
|
.then(function(r) {
|
|
|
|
res.send(r);
|
|
|
|
})
|
|
|
|
.catch(function(error) {
|
|
|
|
console.error(error); // eslint-disable-line no-console
|
|
|
|
res.sendStatus(500);
|
|
|
|
});
|
2019-01-04 22:44:48 +03:00
|
|
|
break;
|
|
|
|
default:
|
2019-01-10 21:51:14 +03:00
|
|
|
store
|
|
|
|
.add(req.body)
|
|
|
|
.then(function(r) {
|
|
|
|
res.send({ id: r.id, error: r.error });
|
|
|
|
scServer.exchange.publish('report', {
|
|
|
|
type: 'add',
|
|
|
|
data: r
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(function(error) {
|
|
|
|
console.error(error); // eslint-disable-line no-console
|
|
|
|
res.status(500).send({});
|
2019-01-04 22:44:48 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = routes;
|