From 760f1ab9e84998497f32ce2430a6abfa93073bb7 Mon Sep 17 00:00:00 2001 From: jubnl Date: Sat, 11 Jul 2026 20:01:52 +0800 Subject: [PATCH] feat: support separate AMAP_JS_API_KEY for JS SDK proxy - AmapProxyController now reads AMAP_JS_API_KEY first, falls back to AMAP_WEB_SERVICE_KEY - Update .env.example to document both keys --- server/.env.example | 3 ++- server/src/nest/maps/amap-proxy.controller.ts | 12 ++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/server/.env.example b/server/.env.example index 7bb5a096..2e09e052 100644 --- a/server/.env.example +++ b/server/.env.example @@ -45,7 +45,8 @@ DEMO_MODE=false # Demo mode - resets data hourly # UNSPLASH_ACCESS_KEY= # Optional Unsplash Access Key for trip-cover and place-image search. Without one, TREK uses Unsplash's unauthenticated endpoint, which some datacenter/VPS IPs get blocked from (#1449). Get a free key at unsplash.com/developers. Overrides any key set per-admin in Admin > Settings. Can also be configured there instead of here. -# AMAP_WEB_SERVICE_KEY= # Required. Amap (Gaode Maps) Web Service API key for maps, places, routing, and transit services. Get one at https://console.amap.com/dev/ +# AMAP_WEB_SERVICE_KEY= # Required. Amap (Gaode Maps) Web Service API key for place search, routing, transit, and geocoding. Get one at https://console.amap.com/dev/ +# AMAP_JS_API_KEY= # Required for the Amap JS map component. Falls back to AMAP_WEB_SERVICE_KEY if not set. Apply for a "JS API" key at https://console.amap.com/dev/ # OVERPASS_URL= # Deprecated — maps service uses Amap instead. Ignored when AMAP_WEB_SERVICE_KEY is set. diff --git a/server/src/nest/maps/amap-proxy.controller.ts b/server/src/nest/maps/amap-proxy.controller.ts index 9c0a46c6..49d4ef7b 100644 --- a/server/src/nest/maps/amap-proxy.controller.ts +++ b/server/src/nest/maps/amap-proxy.controller.ts @@ -5,6 +5,10 @@ const AMAP_JS_BASE = 'https://webapi.amap.com'; const AMAP_JS_PATH = '/maps'; const CACHE_MAX_AGE = 24 * 60 * 60; +function getJsKey(): string | undefined { + return process.env.AMAP_JS_API_KEY?.trim() || process.env.AMAP_WEB_SERVICE_KEY?.trim(); +} + @Controller('api/maps') export class AmapProxyController { private jsCache: { body: string; contentType: string } | null = null; @@ -12,9 +16,9 @@ export class AmapProxyController { @Get('amap-js') async serveAmapJs(@Query() query: Record, @Res() res: Response) { - const key = process.env.AMAP_WEB_SERVICE_KEY?.trim(); + const key = getJsKey(); if (!key) { - res.status(503).json({ error: 'AMap service not configured' }); + res.status(503).json({ error: 'AMap JS API key not configured' }); return; } @@ -61,9 +65,9 @@ export class AmapProxyController { @All('amap-proxy/*') async proxyAmap(@Req() req: Request, @Res() res: Response) { - const key = process.env.AMAP_WEB_SERVICE_KEY?.trim(); + const key = getJsKey(); if (!key) { - res.status(503).json({ error: 'AMap service not configured' }); + res.status(503).json({ error: 'AMap JS API key not configured' }); return; }