Skip to content

Commit

Permalink
feat(lambda-xyz): add WMTSCapabilities and webp support to cli/serve
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoff Jacobsen committed May 11, 2020
1 parent da78cb9 commit bf2f8c5
Showing 1 changed file with 47 additions and 5 deletions.
52 changes: 47 additions & 5 deletions packages/lambda-xyz/src/cli/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,26 @@ export class TileSetLocal extends TileSet {
}

async function main(): Promise<void> {
if (Env.get(Env.PublicUrlBase) == '') {
process.env[Env.PublicUrlBase] = `http://localhost:${port}`;
}
const filePath = process.argv[2];
if (filePath != null) {
const tileSet = new TileSetLocal('aerial', EPSG.Google, filePath);
let tileSet = new TileSetLocal('aerial', EPSG.Google, filePath);
TileSets.set(tileSet.id, tileSet);
tileSet = new TileSetLocal('aerial@beta', EPSG.Google, filePath);
TileSets.set(tileSet.id, tileSet);
}

app.get('/v1/tiles/:imageryName/:projection/:z/:x/:y.png', async (req: express.Request, res: express.Response) => {
app.get('/v1/tiles/:imageryName/:projection/:z/:x/:y.:ext', async (req: express.Request, res: express.Response) => {
const startTime = Date.now();
const requestId = ulid.ulid();
const logger = LogConfig.get().child({ id: requestId });
const { x, y, z, imageryName, projection } = req.params;
const { x, y, z, ext, imageryName, projection } = req.params;
const ctx = new LambdaContext(
{
httpMethod: 'get',
path: `/v1/tiles/${imageryName}/${projection}/${z}/${x}/${y}.png`,
path: `/v1/tiles/${imageryName}/${projection}/${z}/${x}/${y}.${ext}`,
} as any,
logger,
);
Expand All @@ -95,9 +100,46 @@ async function main(): Promise<void> {
}
});

app.get(
'/v1/tiles/:imageryName/:projection/WMTSCapabilities.xml',
async (req: express.Request, res: express.Response) => {
const startTime = Date.now();
const requestId = ulid.ulid();
const logger = LogConfig.get().child({ id: requestId });
const { imageryName, projection } = req.params;
const ctx = new LambdaContext(
{
httpMethod: 'get',
path: `/v1/tiles/${imageryName}/${projection}/WMTSCapabilities.xml`,
} as any,
logger,
);
try {
const data = await lambda.handleRequest(ctx);
res.status(data.status);
if (data.headers) {
for (const [header, value] of data.headers) {
res.header(header, String(value));
}
}
if (data.status < 299 && data.status > 199) {
res.end(Buffer.from(data.getBody() ?? '', 'base64'));
} else {
res.end();
}
const duration = Date.now() - startTime;
logger.info({ ...ctx.logContext, status: data.status, duration }, 'Done');
} catch (e) {
logger.fatal({ ...ctx.logContext, err: e }, 'FailedToRender');
res.status(500);
res.end();
}
},
);

app.use(express.static('../landing/static/'));
await new Promise((resolve) => app.listen(port, resolve));
console.log('Listen', `http://localhost:${port}`);
console.log('Listen', Env.get(Env.PublicUrlBase));
}

main().catch((e) => console.error(e));

0 comments on commit bf2f8c5

Please sign in to comment.