| 1 | import * as GeoMaps from "@/AWS/GeoMaps"; |
| 2 | import * as Lambda from "@/AWS/Lambda"; |
| 3 | import * as Duration from "effect/Duration"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 7 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 8 | import path from "pathe"; |
| 9 | |
| 10 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 11 | |
| 12 | export class GeoMapsTestFunction extends Lambda.Function<Lambda.Function>()( |
| 13 | "GeoMapsTestFunction", |
| 14 | ) {} |
| 15 | |
| 16 | export default GeoMapsTestFunction.make( |
| 17 | { |
| 18 | main, |
| 19 | functionUrl: true, |
| 20 | // Map renders / tile fetches can take a few seconds; the AWS default |
| 21 | // 3s Lambda timeout is too tight. |
| 22 | timeout: Duration.seconds(30), |
| 23 | }, |
| 24 | Effect.gen(function* () { |
| 25 | const getStaticMap = yield* GeoMaps.GetStaticMap(); |
| 26 | const getTile = yield* GeoMaps.GetTile(); |
| 27 | const getStyleDescriptor = yield* GeoMaps.GetStyleDescriptor(); |
| 28 | const getSprites = yield* GeoMaps.GetSprites(); |
| 29 | const getGlyphs = yield* GeoMaps.GetGlyphs(); |
| 30 | |
| 31 | const bound = { |
| 32 | getStaticMap, |
| 33 | getTile, |
| 34 | getStyleDescriptor, |
| 35 | getSprites, |
| 36 | getGlyphs, |
| 37 | }; |
| 38 | |
| 39 | return { |
| 40 | fetch: Effect.gen(function* () { |
| 41 | const request = yield* HttpServerRequest; |
| 42 | const url = new URL(request.originalUrl); |
| 43 | const pathname = url.pathname; |
| 44 | |
| 45 | if (request.method === "GET" && pathname === "/bindings") { |
| 46 | return yield* HttpServerResponse.json({ |
| 47 | bound: Object.keys(bound), |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | if (request.method === "GET" && pathname === "/static-map") { |
| 52 | const result = yield* getStaticMap({ |
| 53 | FileName: "map", |
| 54 | Center: "-122.3493,47.6205", |
| 55 | Zoom: 12, |
| 56 | Width: 400, |
| 57 | Height: 300, |
| 58 | }); |
| 59 | return yield* HttpServerResponse.json({ |
| 60 | bytes: result.Blob?.length ?? 0, |
| 61 | contentType: result.ContentType ?? null, |
| 62 | pricingBucket: result.PricingBucket, |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | if (request.method === "GET" && pathname === "/tile") { |
| 67 | const result = yield* getTile({ |
| 68 | Tileset: "vector.basemap", |
| 69 | Z: "0", |
| 70 | X: "0", |
| 71 | Y: "0", |
| 72 | }); |
| 73 | return yield* HttpServerResponse.json({ |
| 74 | bytes: result.Blob?.length ?? 0, |
| 75 | contentType: result.ContentType ?? null, |
| 76 | pricingBucket: result.PricingBucket, |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | if (request.method === "GET" && pathname === "/style-descriptor") { |
| 81 | const result = yield* getStyleDescriptor({ Style: "Standard" }); |
| 82 | const text = result.Blob ? new TextDecoder().decode(result.Blob) : ""; |
| 83 | let version: number | null = null; |
| 84 | try { |
| 85 | version = |
| 86 | (JSON.parse(text) as { version?: number }).version ?? null; |
| 87 | } catch { |
| 88 | version = null; |
| 89 | } |
| 90 | return yield* HttpServerResponse.json({ |
| 91 | bytes: result.Blob?.length ?? 0, |
| 92 | contentType: result.ContentType ?? null, |
| 93 | version, |
| 94 | }); |
| 95 | } |
| 96 | |
| 97 | if (request.method === "GET" && pathname === "/sprites") { |
| 98 | const result = yield* getSprites({ |
| 99 | FileName: "sprites.png", |
| 100 | Style: "Standard", |
| 101 | ColorScheme: "Light", |
| 102 | Variant: "Default", |
| 103 | }); |
| 104 | return yield* HttpServerResponse.json({ |
| 105 | bytes: result.Blob?.length ?? 0, |
| 106 | contentType: result.ContentType ?? null, |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | if (request.method === "GET" && pathname === "/glyphs") { |
| 111 | const result = yield* getGlyphs({ |
| 112 | FontStack: "Amazon Ember Regular", |
| 113 | FontUnicodeRange: "0-255.pbf", |
| 114 | }); |
| 115 | return yield* HttpServerResponse.json({ |
| 116 | bytes: result.Blob?.length ?? 0, |
| 117 | contentType: result.ContentType ?? null, |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | return yield* HttpServerResponse.json( |
| 122 | { error: "Not found", method: request.method, pathname }, |
| 123 | { status: 404 }, |
| 124 | ); |
| 125 | }).pipe(Effect.orDie), |
| 126 | }; |
| 127 | }).pipe( |
| 128 | Effect.provide( |
| 129 | Layer.mergeAll( |
| 130 | GeoMaps.GetStaticMapHttp, |
| 131 | GeoMaps.GetTileHttp, |
| 132 | GeoMaps.GetStyleDescriptorHttp, |
| 133 | GeoMaps.GetSpritesHttp, |
| 134 | GeoMaps.GetGlyphsHttp, |
| 135 | ), |
| 136 | ), |
| 137 | ), |
| 138 | ); |