2026-04-15-2 dashboard convert to html without vite and react
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import { Controller, Get, Query, UseGuards } from '@nestjs/common';
|
||||
import { Controller, Get, Query, UseGuards, Res } from '@nestjs/common';
|
||||
import { ApiTags, ApiOperation } from '@nestjs/swagger';
|
||||
import type { Response } from 'express';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { MapsService } from './maps.service';
|
||||
import { ApiKeyGuard } from '../common/guards/api-key.guard';
|
||||
|
||||
@@ -7,13 +10,72 @@ import { ApiKeyGuard } from '../common/guards/api-key.guard';
|
||||
@Controller('maps')
|
||||
@UseGuards(ApiKeyGuard)
|
||||
export class MapsController {
|
||||
constructor(private readonly mapsService: MapsService) {}
|
||||
constructor(private readonly mapsService: MapsService) { }
|
||||
|
||||
@Get('style.json')
|
||||
@ApiOperation({ summary: 'Get MapLibre style JSON 🎨' })
|
||||
async getStyleJson(@Query('theme') theme: string, @Res() res: Response) {
|
||||
// Determine filenames based on theme
|
||||
const isDark = theme === 'obsidian';
|
||||
const filename = isDark ? 'style-dark.json' : 'style.json';
|
||||
const fallbackFilename = 'style.json';
|
||||
|
||||
// Paths to check
|
||||
const pathsToCheck = [
|
||||
path.join('/data', filename),
|
||||
path.join(process.cwd(), '../../', filename),
|
||||
path.join(process.cwd(), filename),
|
||||
// Fallbacks to light style if dark is missing
|
||||
path.join('/data', fallbackFilename),
|
||||
path.join(process.cwd(), '../../', fallbackFilename),
|
||||
path.join(process.cwd(), fallbackFilename),
|
||||
];
|
||||
|
||||
let stylePath = '';
|
||||
for (const p of pathsToCheck) {
|
||||
if (fs.existsSync(p)) {
|
||||
stylePath = p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!stylePath) {
|
||||
return res.status(404).send('Style not found');
|
||||
}
|
||||
|
||||
try {
|
||||
const styleRaw = fs.readFileSync(stylePath, 'utf8');
|
||||
const styleObj = JSON.parse(styleRaw);
|
||||
|
||||
// Dynamic Theme support (Safety overrides or fine-tuning)
|
||||
if (theme === 'light') {
|
||||
styleObj.layers.forEach((layer: any) => {
|
||||
if (layer.id === 'background') {
|
||||
layer.paint['background-color'] = '#FFFFFF';
|
||||
}
|
||||
});
|
||||
} else if (theme === 'obsidian') {
|
||||
// If we found style-dark.json, we don't strictly need this,
|
||||
// but keeping it as a helper or if it fell back to style.json
|
||||
styleObj.layers.forEach((layer: any) => {
|
||||
if (layer.id === 'background') {
|
||||
layer.paint['background-color'] = '#101014'; // Dark tone
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.send(styleObj);
|
||||
} catch (e) {
|
||||
res.status(500).send('Error parsing style.json');
|
||||
}
|
||||
}
|
||||
|
||||
@Get('route')
|
||||
@ApiOperation({ summary: 'Calculate a route with dynamic waypoints 🚗' })
|
||||
async getRoute(@Query() query: any) {
|
||||
const waypoints: [number, number][] = [];
|
||||
|
||||
|
||||
// 1. Extract Origin (fromLat, fromLng)
|
||||
if (query.fromLat && query.fromLng) {
|
||||
waypoints.push([parseFloat(query.fromLat), parseFloat(query.fromLng)]);
|
||||
@@ -45,7 +107,7 @@ export class MapsController {
|
||||
const profile = query.profile || 'car';
|
||||
const steps = query.steps === 'true';
|
||||
const locale = query.locale || 'en';
|
||||
|
||||
|
||||
return this.mapsService.getRoute(waypoints, profile, steps, locale);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user