@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 137 lines 4.31 KB
Raw
1 import * as GeoMaps from "@alchemy.run/aws/GeoMaps";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
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 = (JSON.parse(text) as { version?: number }).version ?? null;
86 } catch {
87 version = null;
88 }
89 return yield* HttpServerResponse.json({
90 bytes: result.Blob?.length ?? 0,
91 contentType: result.ContentType ?? null,
92 version,
93 });
94 }
95
96 if (request.method === "GET" && pathname === "/sprites") {
97 const result = yield* getSprites({
98 FileName: "sprites.png",
99 Style: "Standard",
100 ColorScheme: "Light",
101 Variant: "Default",
102 });
103 return yield* HttpServerResponse.json({
104 bytes: result.Blob?.length ?? 0,
105 contentType: result.ContentType ?? null,
106 });
107 }
108
109 if (request.method === "GET" && pathname === "/glyphs") {
110 const result = yield* getGlyphs({
111 FontStack: "Amazon Ember Regular",
112 FontUnicodeRange: "0-255.pbf",
113 });
114 return yield* HttpServerResponse.json({
115 bytes: result.Blob?.length ?? 0,
116 contentType: result.ContentType ?? null,
117 });
118 }
119
120 return yield* HttpServerResponse.json(
121 { error: "Not found", method: request.method, pathname },
122 { status: 404 },
123 );
124 }).pipe(Effect.orDie),
125 };
126 }).pipe(
127 Effect.provide(
128 Layer.mergeAll(
129 GeoMaps.GetStaticMapHttp,
130 GeoMaps.GetTileHttp,
131 GeoMaps.GetStyleDescriptorHttp,
132 GeoMaps.GetSpritesHttp,
133 GeoMaps.GetGlyphsHttp,
134 ),
135 ),
136 ),
137 );