Commit 5ef6efeb authored by Valeriy Panov's avatar Valeriy Panov Committed by Matija Obreza
Browse files

/robots.txt response depends on runtime ALLOW_ROBOTS env variable

parent e2af6a0f
Pipeline #1578 passed with stages
in 3 minutes and 19 seconds
......@@ -8,6 +8,7 @@ services:
- "CLIENT_SECRET=${CATALOG_CLIENT_SECRET}"
- "CATALOG_API_URL=https://catalog-master.review.genesys-pgr.org"
- "SSR=true"
- "ALLOW_ROBOTS=true"
networks:
- traefik-net
deploy:
......
let server = require('./server').default;
let server = require('./src/server').default;
const PORT = process.env.PORT || 3000;
......
import * as express from 'express';
import * as proxy from 'express-http-proxy';
import * as React from 'react';
import {renderToString} from 'react-dom/server';
import {RouterContext, match} from 'react-router';
import {createLocation} from 'history';
import routes from './assets/components/routes';
import {Provider} from 'react-redux';
import rootReducer from './assets/reducers';
import fetchComponentData from './assets/middleware/fetchComponentData';
import * as serialize from 'serialize-javascript';
import {browserHistory} from 'react-router';
import {routerMiddleware} from 'react-router-redux';
import {
createStore,
applyMiddleware,
} from 'redux';
import 'path';
import * as path from 'path';
import * as fs from 'fs';
import thunk from 'redux-thunk';
import * as cookieParser from 'cookie-parser';
import config from './config/config';
import checkAuthToken from './assets/middleware/checkAuthToken';
const app = express();
const html = fs.readFileSync(__dirname + '/react.html', {encoding: 'utf8'});
app.use('/proxy', proxy(config.apiUrl, {
filter: function(req, res) {
if (req.url.indexOf('/api') === 0) {
console.log(req.headers['authorization']);
return typeof req.headers['authorization'] !== 'undefined';
}
return true;
},
proxyReqPathResolver(req) {
let path = req.url;
if (path.indexOf('/oauth/token') === 0) {
const grantType = req.query['grant_type'];
if(grantType === 'client_credentials' || grantType === 'password') {
path = `${path}&client_id=${config.clientId}&client_secret=${config.clientSecret}`;
}
} else if (path.indexOf('/google/verify-token') === 0) {
path = `${path}&clientId=${config.clientId}`;
}
return path;
},
}));
app.use(express.static(path.join(__dirname)));
app.use(cookieParser());
app.use((req, res) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
const location = createLocation(req.url);
const store = applyMiddleware(thunk, routerMiddleware(browserHistory))(createStore)(rootReducer);
match({routes, location}, (err, redirectLocation, renderProps) => {
if (err) {
console.error(err);
return res.status(500).end('Internal server error');
}
if (!renderProps) {
return res.status(404).end('Not found');
}
if (redirectLocation) {
// todo
}
function renderView() {
const InitialView = (
<Provider store={ store }>
<RouterContext { ...renderProps } />
</ Provider >
);
const componentHTML = renderToString(InitialView);
const initialState = store.getState();
return html.replace('SERVER_RENDERED_HTML', componentHTML)
.replace('SERVER_RENDERED_STATE', serialize(initialState, {isJSON: true}));
}
checkAuthToken(req, res, store.dispatch)
.then(() => fetchComponentData(store.dispatch, renderProps.components, renderProps.params))
.then(renderView)
.then((template) => res.end(template))
.catch((err) => res.end(err.message));
});
});
export default app;
import * as proxy from 'express-http-proxy';
import config from '../../config/config';
const httpProxy = proxy(config.apiUrl, {
filter: function(req, res) {
if (req.url.indexOf('/api') === 0) {
console.log(req.headers['authorization']);
return typeof req.headers['authorization'] !== 'undefined';
}
return true;
},
proxyReqPathResolver(req) {
let path = req.url;
if (path.indexOf('/oauth/token') === 0) {
const grantType = req.query['grant_type'];
if(grantType === 'client_credentials' || grantType === 'password') {
path = `${path}&client_id=${config.clientId}&client_secret=${config.clientSecret}`;
}
} else if (path.indexOf('/google/verify-token') === 0) {
path = `${path}&clientId=${config.clientId}`;
}
return path;
},
});
export default httpProxy;
import * as React from 'react';
import {renderToString} from 'react-dom/server';
import {RouterContext, match} from 'react-router';
import {createLocation} from 'history';
import {Provider} from 'react-redux';
import * as serialize from 'serialize-javascript';
import {browserHistory} from 'react-router';
import {routerMiddleware} from 'react-router-redux';
import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import routes from '../../assets/components/routes';
import rootReducer from '../../assets/reducers';
import fetchComponentData from '../../assets/middleware/fetchComponentData';
import checkAuthToken from '../../assets/middleware/checkAuthToken';
const prerenderer = (html) => (req, res) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
const location = createLocation(req.url);
const store = applyMiddleware(thunk, routerMiddleware(browserHistory))(createStore)(rootReducer);
match({routes, location}, (err, redirectLocation, renderProps) => {
if (err) {
console.error(err);
return res.status(500).end('Internal server error');
}
if (!renderProps) {
return res.status(404).end('Not found');
}
if (redirectLocation) {
// todo
}
function renderView() {
const InitialView = (
<Provider store={ store }>
<RouterContext { ...renderProps } />
</ Provider >
);
const componentHTML = renderToString(InitialView);
const initialState = store.getState();
return html.replace('SERVER_RENDERED_HTML', componentHTML)
.replace('SERVER_RENDERED_STATE', serialize(initialState, {isJSON: true}));
}
checkAuthToken(req, res, store.dispatch)
.then(() => fetchComponentData(store.dispatch, renderProps.components, renderProps.params))
.then(renderView)
.then((template) => res.end(template))
.catch((err) => res.end(err.message));
});
};
export default prerenderer;
\ No newline at end of file
import * as express from 'express';
import * as path from 'path';
import * as cookieParser from 'cookie-parser';
import {readFileSync} from 'fs';
import prerenderer from './middlewares/prerenderer';
import httpProxy from './middlewares/httpProxy';
import robots from './utils/robots';
__dirname = __dirname + '/../';
const app = express();
const html = readFileSync(__dirname + '/react.html', {encoding: 'utf8'});
app.use('/proxy', httpProxy);
app.get('/robots.txt', robots);
app.use(express.static(path.join(__dirname)));
app.use(cookieParser());
app.use(prerenderer(html));
export default app;
const robots = (req, res) => {
const allow = process.env.ALLOW_ROBOTS === 'true' ? 'Allow: /': 'Disallow: /';
res.type('text/plain');
res.send('User-Agent: *\n' + allow);
};
export default robots;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment