Good changes

This commit is contained in:
Andras Schmelczer 2026-02-10 22:09:46 +00:00
parent d39d1b15fd
commit 1f68ca0512
23 changed files with 670 additions and 289 deletions

View file

@ -17,39 +17,37 @@ export class ScreenshotCache {
/**
* Build a cache key by quantizing view params and hashing.
* - lat/lng quantized to 2 decimal places
* - lat/lon quantized to 2 decimal places
* - zoom quantized to integer
* - filters sorted alphabetically
* - filters and POI categories sorted alphabetically
*/
buildKey(params: Record<string, string>): string {
buildKey(params: URLSearchParams): string {
const normalized: Record<string, string> = {};
// Parse and quantize the view param (lat,lng,zoom)
if (params.v) {
const parts = params.v.split(',');
if (parts.length === 3) {
const lat = parseFloat(parts[0]).toFixed(2);
const lng = parseFloat(parts[1]).toFixed(2);
const zoom = Math.round(parseFloat(parts[2])).toString();
normalized.v = `${lat},${lng},${zoom}`;
} else {
normalized.v = params.v;
}
// Quantize lat/lon/zoom
const lat = params.get('lat');
const lon = params.get('lon');
const zoom = params.get('zoom');
if (lat && lon && zoom) {
normalized.lat = parseFloat(lat).toFixed(2);
normalized.lon = parseFloat(lon).toFixed(2);
normalized.zoom = Math.round(parseFloat(zoom)).toString();
}
// Sort filters
if (params.f) {
const segments = params.f.split(',').sort();
normalized.f = segments.join(',');
const filters = params.getAll('filter').sort();
if (filters.length > 0) {
normalized.filters = filters.join(',');
}
if (params.poi) {
const cats = params.poi.split(',').sort();
normalized.poi = cats.join(',');
// Sort POI categories
const pois = params.getAll('poi').sort();
if (pois.length > 0) {
normalized.poi = pois.join(',');
}
if (params.tab) {
normalized.tab = params.tab;
if (params.get('tab')) {
normalized.tab = params.get('tab')!;
}
const input = JSON.stringify(normalized);

View file

@ -3,8 +3,18 @@ import { ScreenshotCache } from './cache.js';
import { takeScreenshot, checkWebGL, closeBrowser } from './screenshot.js';
const PORT = parseInt(process.env.PORT || '8002', 10);
const APP_URL = process.env.APP_URL || 'http://localhost:8001';
const CACHE_DIR = process.env.CACHE_DIR || '/cache';
const APP_URL = process.env.APP_URL;
const CACHE_DIR = process.env.CACHE_DIR;
if (!APP_URL) {
console.error('Error: APP_URL environment variable is required');
process.exit(1);
}
if (!CACHE_DIR) {
console.error('Error: CACHE_DIR environment variable is required');
process.exit(1);
}
const cache = new ScreenshotCache(CACHE_DIR);
const app = express();
@ -24,15 +34,26 @@ app.get('/debug', async (_req, res) => {
app.get('/screenshot', async (req, res) => {
try {
const params: Record<string, string> = {};
for (const key of ['v', 'f', 'poi', 'tab', 'og']) {
const qs = new URLSearchParams();
for (const key of ['lat', 'lon', 'zoom', 'tab', 'og']) {
const val = req.query[key];
if (typeof val === 'string' && val) {
params[key] = val;
qs.set(key, val);
}
}
// Repeated params: filter, poi
for (const key of ['filter', 'poi']) {
const val = req.query[key];
if (typeof val === 'string' && val) {
qs.append(key, val);
} else if (Array.isArray(val)) {
for (const v of val) {
if (typeof v === 'string' && v) qs.append(key, v);
}
}
}
const cacheKey = cache.buildKey(params);
const cacheKey = cache.buildKey(qs);
// Check cache first
const cached = cache.get(cacheKey);
@ -45,7 +66,6 @@ app.get('/screenshot', async (req, res) => {
}
// Build the URL for the frontend in screenshot mode
const qs = new URLSearchParams(params);
qs.set('screenshot', '1');
const url = `${APP_URL}/?${qs}`;