-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.ts
118 lines (94 loc) · 4.32 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { FsaCache, FsaLog, LogConfig, LogStorage } from '@basemaps/shared';
import { LambdaHttpRequest, LambdaHttpResponse, lf } from '@linzjs/lambda';
import { tileAttributionGet } from './routes/attribution.js';
import { configImageryGet, configTileSetGet } from './routes/config.js';
import { fontGet, fontList } from './routes/fonts.js';
import { healthGet } from './routes/health.js';
import { imageryGet } from './routes/imagery.js';
import { linkGet } from './routes/link.js';
import { pingGet } from './routes/ping.js';
import { previewIndexGet } from './routes/preview.index.js';
import { tilePreviewGet } from './routes/preview.js';
import { spriteGet } from './routes/sprites.js';
import { tileJsonGet } from './routes/tile.json.js';
import { styleJsonGet } from './routes/tile.style.json.js';
import { wmtsCapabilitiesGet } from './routes/tile.wmts.js';
import { tileXyzGet } from './routes/tile.xyz.js';
import { versionGet } from './routes/version.js';
import { NotFound } from './util/response.js';
export const handler = lf.http(LogConfig.get());
/** If the request takes too long, respond with a 408 timeout when there is approx 1 second remaining */
handler.router.timeoutEarlyMs = 1_000;
function randomTrace(req: LambdaHttpRequest): void {
// If the env is set to trace level always trace requests
if (process.env['TRACE']) {
req.log.level = 'trace';
return;
}
// Set the logging level based off a percent
const rand = Math.random();
// 1% trace
if (rand < 0.01) req.log.level = 'trace';
// 25% debug
else if (rand < 0.25) req.log.level = 'debug';
// everything else info
else req.log.level = 'info';
}
handler.router.hook('request', (req) => {
LogStorage.enterWith({ log: req.log });
FsaLog.reset();
randomTrace(req);
req.set('name', 'LambdaTiler');
});
handler.router.hook('response', (req, res) => {
req.set('fetchCount', FsaLog.count);
req.set('fetches', FsaLog.requests);
req.set('cacheSize', FsaCache.size);
// Force access-control-allow-origin to everything
res.header('access-control-allow-origin', '*');
});
// CORS is handled by response hook so just return ok if the route exists
handler.router.options('*', (req) => {
const route = handler.router.router.find('GET', req.path);
if (route == null) return NotFound();
return LambdaHttpResponse.ok();
});
// TODO some internal health checks hit these routes, we should change them all to point at /v1/
handler.router.get('/ping', pingGet);
handler.router.get('/health', healthGet);
handler.router.get('/version', versionGet);
handler.router.get('/v1/ping', pingGet);
handler.router.get('/v1/health', healthGet);
handler.router.get('/v1/version', versionGet);
// Image Metadata
handler.router.get('/v1/imagery/:imageryId/:fileName', imageryGet);
// Config
handler.router.get('/v1/config/:tileSet.json', configTileSetGet);
handler.router.get('/v1/config/:tileSet/:imageryId.json', configImageryGet);
// Sprites
handler.router.get('/v1/sprites/:spriteName', spriteGet);
// Fonts
handler.router.get('/v1/fonts.json', fontList);
handler.router.get('/v1/fonts/:fontStack/:range.pbf', fontGet);
// StyleJSON
handler.router.get('/v1/styles/:styleName.json', styleJsonGet);
/** @deprecated 2022-07-22 all styles should be being served from /v1/styles/:styleName.json */
handler.router.get('/v1/tiles/:tileSet/:tileMatrix/style/:styleName.json', styleJsonGet);
// TileJSON
handler.router.get('/v1/tiles/:tileSet/:tileMatrix/tile.json', tileJsonGet);
// Tiles
handler.router.get('/v1/tiles/:tileSet/:tileMatrix/:z/:x/:y.:tileType', tileXyzGet);
// Preview
handler.router.get('/v1/preview/:tileSet/:tileMatrix/:z/:lon/:lat', tilePreviewGet);
handler.router.get('/v1/preview/:tileSet/:tileMatrix/:z/:lon/:lat/:outputType', tilePreviewGet);
handler.router.get('/v1/@:location', previewIndexGet);
handler.router.get('/@:location', previewIndexGet);
// Link
handler.router.get('/v1/link/:tileSet', linkGet);
// Attribution
handler.router.get('/v1/tiles/:tileSet/:tileMatrix/attribution.json', tileAttributionGet);
handler.router.get('/v1/attribution/:tileSet/:tileMatrix/summary.json', tileAttributionGet);
// WMTS Capabilities
handler.router.get('/v1/tiles/:tileSet/:tileMatrix/WMTSCapabilities.xml', wmtsCapabilitiesGet);
handler.router.get('/v1/tiles/:tileSet/WMTSCapabilities.xml', wmtsCapabilitiesGet);
handler.router.get('/v1/tiles/WMTSCapabilities.xml', wmtsCapabilitiesGet);