diff --git a/redirect.tsv b/redirect.tsv new file mode 100644 index 00000000..e094a42d --- /dev/null +++ b/redirect.tsv @@ -0,0 +1,6 @@ +/document/statue-2013 https://static.shburg.org/art/uploads/ustav-shkoly-2013.pdf +/document/edu-license https://static.shburg.org/art/uploads/obrazovatelnaya-lizenziya.pdf +/document/gov-certificate https://static.shburg.org/art/uploads/svidetelstvo-na-pomeschenie.pdf +/document/VAT https://static.shburg.org/art/uploads/inn.pdf +/document/EGRYL1 https://static.shburg.org/art/uploads/egryul.pdf +/document/EGRYL2 https://static.shburg.org/art/uploads/egryul.pdf \ No newline at end of file diff --git a/src/config.js b/src/config.js index 9fb85c01..82b17573 100644 --- a/src/config.js +++ b/src/config.js @@ -37,4 +37,16 @@ export default Object.keys(privateData) .reduce((config, key) => { config[key] = privateData[key]; return config; - }, config); \ No newline at end of file + }, config); + +let redirectPath = path.join(__dirname, '../redirect.tsv'); +export const redirectionTable = fs.readFileSync(redirectPath, 'utf-8') + .split('\n') + .map(row => row.split(/\s+/)) + .map(row => { + let [from, to] = row; + return { + 'url': from, + 'redirect': to + } + }); \ No newline at end of file diff --git a/src/routes/redirect.js b/src/routes/redirect.js new file mode 100644 index 00000000..af35ff3a --- /dev/null +++ b/src/routes/redirect.js @@ -0,0 +1,19 @@ +/** + * + * @param redirectionTable + * [{String url, String redirect}, ...] + * @returns {*} + */ +export function redirect(redirectionTable) { + return function *(next) { + let path = this.request.path; + + let record = redirectionTable.find(i => i.url === path); + if(record){ + this.redirect(record.redirect); + return; + } + + yield next; + }; +} diff --git a/src/server.js b/src/server.js index 8356e493..1feb3b54 100644 --- a/src/server.js +++ b/src/server.js @@ -10,7 +10,9 @@ import helmet from 'koa-helmet'; import bodyParser from 'koa-bodyparser'; import config from './config'; +import {redirectionTable} from './config'; import {index, queryObject} from './routes'; +import {redirect} from './routes/redirect'; import sitemap from './routes/sitemap'; import schedule from './routes/schedule'; @@ -42,6 +44,7 @@ app.use(function *(next) { app.use(prerender(config.prerender)); app.use(serve(path.join(__dirname, '../public'))); app.use(serve(path.join(__dirname, 'templates'))); +app.use(redirect(redirectionTable)); app.use(helmet()); app.use(queryObject()); app.use(route.get('/', index()));