Skip to content

Commit

Permalink
feat(server): create server route and tileset path function
Browse files Browse the repository at this point in the history
  • Loading branch information
tawera-manaena committed Sep 30, 2024
1 parent a420f57 commit d4546fb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/server/src/route.link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { BasemapsConfigProvider, getAllImagery, } from '@basemaps/config';

Check failure on line 1 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-containers

Delete `,`

Check failure on line 1 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Delete `,`

Check failure on line 1 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-deploy

Delete `,`
import { Epsg } from '@basemaps/geo';
import { getPreviewUrl } from '@basemaps/shared';

/**
* If a tileset layer of the given id and projection exists, this function returns a Basemaps URL
* path that is already zoomed to the extent of the tileset. Otherwise, this function returns null.
*

Check failure on line 8 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-containers

Delete `·`

Check failure on line 8 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Delete `·`

Check failure on line 8 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-deploy

Delete `·`
* @param id - The id of the tileset.
* @example "ashburton-2023-0.1m"
*

Check failure on line 11 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-containers

Delete `·`

Check failure on line 11 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Delete `·`

Check failure on line 11 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-deploy

Delete `·`
* @param projection - The projection from the tileset's layers to consider.
* @example Epsg.Google = 3857
*/
export async function getTileSetPath(cfg: BasemapsConfigProvider, id: string, projection: Epsg): Promise<string | null> {

Check failure on line 15 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-containers

Replace `cfg:·BasemapsConfigProvider,·id:·string,·projection:·Epsg` with `⏎··cfg:·BasemapsConfigProvider,⏎··id:·string,⏎··projection:·Epsg,⏎`

Check failure on line 15 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Replace `cfg:·BasemapsConfigProvider,·id:·string,·projection:·Epsg` with `⏎··cfg:·BasemapsConfigProvider,⏎··id:·string,⏎··projection:·Epsg,⏎`

Check failure on line 15 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-deploy

Replace `cfg:·BasemapsConfigProvider,·id:·string,·projection:·Epsg` with `⏎··cfg:·BasemapsConfigProvider,⏎··id:·string,⏎··projection:·Epsg,⏎`
const tileSet = await cfg.TileSet.get(id)

Check failure on line 16 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-containers

Replace `····const·tileSet·=·await·cfg.TileSet.get(id)` with `··const·tileSet·=·await·cfg.TileSet.get(id);`

Check failure on line 16 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Replace `····const·tileSet·=·await·cfg.TileSet.get(id)` with `··const·tileSet·=·await·cfg.TileSet.get(id);`

Check failure on line 16 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-deploy

Replace `····const·tileSet·=·await·cfg.TileSet.get(id)` with `··const·tileSet·=·await·cfg.TileSet.get(id);`
if (!tileSet) return null

Check failure on line 17 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-containers

Replace `··if·(!tileSet)·return·null` with `if·(!tileSet)·return·null;`

Check failure on line 17 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Replace `··if·(!tileSet)·return·null` with `if·(!tileSet)·return·null;`

Check failure on line 17 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-deploy

Replace `··if·(!tileSet)·return·null` with `if·(!tileSet)·return·null;`

const imageryMap = await getAllImagery(cfg, tileSet.layers, [projection]);

Check failure on line 19 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-containers

Replace `····` with `··`

Check failure on line 19 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Replace `····` with `··`

Check failure on line 19 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-deploy

Replace `····` with `··`

const [_, imagery] = Array.from(imageryMap)[0]

Check failure on line 21 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-containers

Replace `··const·[_,·imagery]·=·Array.from(imageryMap)[0]` with `const·[_,·imagery]·=·Array.from(imageryMap)[0];`

Check failure on line 21 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-containers

'_' is assigned a value but never used

Check failure on line 21 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Replace `··const·[_,·imagery]·=·Array.from(imageryMap)[0]` with `const·[_,·imagery]·=·Array.from(imageryMap)[0];`

Check failure on line 21 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

'_' is assigned a value but never used

Check failure on line 21 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-deploy

Replace `··const·[_,·imagery]·=·Array.from(imageryMap)[0]` with `const·[_,·imagery]·=·Array.from(imageryMap)[0];`

Check failure on line 21 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-deploy

'_' is assigned a value but never used
if (!imagery) return null

Check failure on line 22 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-containers

Replace `····if·(!imagery)·return·null` with `··if·(!imagery)·return·null;`

Check failure on line 22 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Replace `····if·(!imagery)·return·null` with `··if·(!imagery)·return·null;`

Check failure on line 22 in packages/server/src/route.link.ts

View workflow job for this annotation

GitHub Actions / build-deploy

Replace `····if·(!imagery)·return·null` with `··if·(!imagery)·return·null;`

const url = getPreviewUrl({ imagery })

return (`${url.slug}?i=${url.name}`)
}
23 changes: 23 additions & 0 deletions packages/server/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Fastfiy uses a lot of floating promises
/* eslint-disable @typescript-eslint/no-floating-promises */
import { Epsg } from '@basemaps/geo';
import { handler } from '@basemaps/lambda-tiler';
import { Env, fsa, getDefaultConfig, LogType, setDefaultConfig } from '@basemaps/shared';
import formBodyPlugin from '@fastify/formbody';
Expand All @@ -14,6 +15,7 @@ import { URL } from 'url';

import { loadConfig, ServerOptions } from './config.js';
import { createLayersHtml } from './route.layers.js';
import { getTileSetPath } from './route.link.js';

const instanceId = ulid.ulid();

Expand Down Expand Up @@ -95,5 +97,26 @@ export async function createServer(opts: ServerOptions, logger: LogType): Promis
res.send(doc);
});

/**
* @route GET /link/:tileSetId
*
* @example /link/ashburton-2023-0.1m
*
* @description If a tileset layer of the given id and 3857 projection exists, the server redirects the client to a Basemaps
* URL that is already zoomed to the extent of the layer. Otherwise, the server returns an 'HTTP 404 Not found' status code.
*
*/
BasemapsServer.get('/link/:tileSetId', async (req: FastifyRequest<{ Params: { tileSetId: string } }>, res: FastifyReply) => {
const { tileSetId } = req.params

const path = await getTileSetPath(cfg, tileSetId, Epsg.Google);

if (path) {
res.redirect(`/${path}`)
} else {
res.status(404)
}
});

return BasemapsServer;
}

0 comments on commit d4546fb

Please sign in to comment.