Add CORS configuration and tile handling improvements

This commit is contained in:
Oleksandr Bezdieniezhnykh
2026-03-26 00:34:42 +02:00
parent 604f0e070d
commit f08058ea9c
9 changed files with 159 additions and 25 deletions
+88 -2
View File
@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using SatelliteProvider.DataAccess;
@@ -8,6 +9,7 @@ using SatelliteProvider.DataAccess.Repositories;
using SatelliteProvider.Common.Configs;
using SatelliteProvider.Common.DTO;
using SatelliteProvider.Common.Interfaces;
using SatelliteProvider.Common.Utils;
using SatelliteProvider.Services;
using Serilog;
@@ -28,9 +30,22 @@ builder.Services.AddSingleton<IRegionRepository>(sp => new RegionRepository(conn
builder.Services.AddSingleton<IRouteRepository>(sp => new RouteRepository(connectionString, sp.GetRequiredService<ILogger<RouteRepository>>()));
builder.Services.AddHttpClient();
builder.Services.AddMemoryCache();
builder.Services.AddSingleton<GoogleMapsDownloaderV2>();
builder.Services.AddSingleton<ITileService, TileService>();
var allowedOrigins = builder.Configuration.GetSection("CorsConfig:AllowedOrigins").Get<string[]>() ?? Array.Empty<string>();
builder.Services.AddCors(options =>
{
options.AddPolicy("TilesCors", policy =>
{
if (allowedOrigins.Length > 0)
policy.WithOrigins(allowedOrigins).AllowAnyHeader().AllowAnyMethod();
else
policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
});
});
var processingConfig = builder.Configuration.GetSection("ProcessingConfig").Get<ProcessingConfig>() ?? new ProcessingConfig();
builder.Services.AddSingleton<IRegionRequestQueue>(sp =>
{
@@ -93,6 +108,10 @@ if (app.Environment.IsDevelopment())
}
app.UseHttpsRedirection();
app.UseCors("TilesCors");
app.MapGet("/tiles/{z:int}/{x:int}/{y:int}", ServeTile)
.WithOpenApi(op => new(op) { Summary = "Get satellite tile image by z/x/y coordinates (Slippy Map tile server)" });
app.MapGet("/api/satellite/tiles/latlon", GetTileByLatLon)
.WithOpenApi(op => new(op) { Summary = "Get satellite tile by latitude and longitude coordinates" });
@@ -119,6 +138,71 @@ app.MapGet("/api/satellite/route/{id:guid}", GetRoute)
app.Run();
async Task<IResult> ServeTile(int z, int x, int y, HttpContext httpContext, ITileRepository tileRepository, GoogleMapsDownloaderV2 downloader, IMemoryCache cache, ILogger<Program> logger)
{
var cacheKey = $"tile_{z}_{x}_{y}";
try
{
if (cache.TryGetValue(cacheKey, out byte[]? cachedBytes) && cachedBytes != null)
{
httpContext.Response.Headers.CacheControl = "public, max-age=86400";
httpContext.Response.Headers.ETag = $"\"{z}_{x}_{y}\"";
return Results.Bytes(cachedBytes, "image/jpeg");
}
string? filePath = null;
var tile = await tileRepository.GetByTileCoordinatesAsync(z, x, y);
if (tile != null && File.Exists(tile.FilePath))
{
filePath = tile.FilePath;
}
else
{
var tileCenter = GeoUtils.TileToWorldPos(x, y, z);
var downloadedTile = await downloader.DownloadSingleTileAsync(tileCenter.Lat, tileCenter.Lon, z);
var now = DateTime.UtcNow;
var tileEntity = new TileEntity
{
Id = Guid.NewGuid(),
TileZoom = z,
TileX = downloadedTile.X,
TileY = downloadedTile.Y,
Latitude = downloadedTile.CenterLatitude,
Longitude = downloadedTile.CenterLongitude,
TileSizeMeters = downloadedTile.TileSizeMeters,
TileSizePixels = 256,
ImageType = "jpg",
MapsVersion = $"downloaded_{now:yyyy-MM-dd}",
Version = now.Year,
FilePath = downloadedTile.FilePath,
CreatedAt = now,
UpdatedAt = now
};
await tileRepository.InsertAsync(tileEntity);
filePath = tileEntity.FilePath;
}
var bytes = await File.ReadAllBytesAsync(filePath);
cache.Set(cacheKey, bytes, new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1),
SlidingExpiration = TimeSpan.FromMinutes(30)
});
httpContext.Response.Headers.CacheControl = "public, max-age=86400";
httpContext.Response.Headers.ETag = $"\"{z}_{x}_{y}\"";
return Results.Bytes(bytes, "image/jpeg");
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to serve tile {Z}/{X}/{Y}", z, x, y);
return Results.Problem(detail: ex.Message, statusCode: 500);
}
}
async Task<IResult> GetTileByLatLon([FromQuery] double Latitude, [FromQuery] double Longitude, [FromQuery] int ZoomLevel, GoogleMapsDownloaderV2 downloader, ITileRepository tileRepository, ILogger<Program> logger)
{
try
@@ -133,7 +217,9 @@ async Task<IResult> GetTileByLatLon([FromQuery] double Latitude, [FromQuery] dou
var tileEntity = new TileEntity
{
Id = Guid.NewGuid(),
ZoomLevel = downloadedTile.ZoomLevel,
TileZoom = downloadedTile.ZoomLevel,
TileX = downloadedTile.X,
TileY = downloadedTile.Y,
Latitude = downloadedTile.CenterLatitude,
Longitude = downloadedTile.CenterLongitude,
TileSizeMeters = downloadedTile.TileSizeMeters,
@@ -151,7 +237,7 @@ async Task<IResult> GetTileByLatLon([FromQuery] double Latitude, [FromQuery] dou
var response = new DownloadTileResponse
{
Id = tileEntity.Id,
ZoomLevel = tileEntity.ZoomLevel,
ZoomLevel = tileEntity.TileZoom,
Latitude = tileEntity.Latitude,
Longitude = tileEntity.Longitude,
TileSizeMeters = tileEntity.TileSizeMeters,
+3
View File
@@ -38,5 +38,8 @@
"QueueCapacity": 1000,
"DelayBetweenRequestsMs": 50,
"SessionTokenReuseCount": 100
},
"CorsConfig": {
"AllowedOrigins": []
}
}