Skip to content

Commit

Permalink
feat: add support for X-Forwarded-Proto, X-Forwarded-Host and X-Rewri…
Browse files Browse the repository at this point in the history
…te-URL headers
  • Loading branch information
stepankuzmin committed Sep 25, 2017
1 parent 095c45d commit 5f32ce2
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const url = require('url');
const path = require('path');
const compress = require('koa-compress');
const conditional = require('koa-conditional-get');
const cors = require('kcors');
Expand Down Expand Up @@ -60,11 +61,16 @@ module.exports = (config) => {
}

const { format } = metadata;
const tilesUrl = url.format({
protocol: ctx.protocol,
host: ctx.host,
pathname: tilePath.replace('{format}', format).replace(/\/+/g, '/')
});
const protocol = ctx.headers['x-forwarded-proto'] || ctx.protocol;
const host = ctx.headers['x-forwarded-host'] || ctx.headers.host;
const originalUrl = ctx.headers['x-rewrite-url'] || ctx.originalUrl;

const pathname = path.join(
path.dirname(originalUrl),
tilePath.replace('{format}', format).replace(/\/+/g, '/')
);

const tilesUrl = url.format({ protocol, host, pathname });

ctx.body = Object.assign({}, metadata, {
tiles: [tilesUrl],
Expand Down

0 comments on commit 5f32ce2

Please sign in to comment.