* * Data in KV will be, * `name:routing` -> `{ "paths": { "/*": { "originId": "marzgentc-dev-alb-origin", "originType": "alb" } } }`
(event)
| 7 | */ |
| 8 | |
| 9 | async function handler(event) { |
| 10 | // This is added at deploy time and is immutable after the function is created |
| 11 | const kvName = `marzgentc:routing` |
| 12 | // Get request details |
| 13 | var request = event.request |
| 14 | var uri = request.uri |
| 15 | |
| 16 | // Try to find a matching route in the KV store |
| 17 | var routeData = null |
| 18 | try { |
| 19 | const kvsHandle = cf.kvs() |
| 20 | routeData = await kvsHandle.get(kvName, { format: 'json' }) |
| 21 | } catch (e) { |
| 22 | // KV lookup failed, fallback to default |
| 23 | console.log('KV lookup error: ' + e.message) |
| 24 | } |
| 25 | |
| 26 | if (routeData && routeData.paths) { |
| 27 | const matchingPath = findMatchingPath(uri, routeData.paths) |
| 28 | if (matchingPath) { |
| 29 | updateOrigin(request, routeData.paths[matchingPath]) |
| 30 | } |
| 31 | } |
| 32 | return request |
| 33 | } |
| 34 | |
| 35 | // Find the path pattern that matches the request URI |
| 36 | function findMatchingPath(uri, paths) { |
no test coverage detected
searching dependent graphs…