Files
satellite-provider/SatelliteProvider.Api/Program.cs
T
2025-10-28 11:07:07 +01:00

161 lines
5.6 KiB
C#

using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using SatelliteProvider.DataAccess;
using SatelliteProvider.DataAccess.Repositories;
using SatelliteProvider.Common.Configs;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection")
?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.Configure<MapConfig>(builder.Configuration.GetSection("MapConfig"));
builder.Services.Configure<StorageConfig>(builder.Configuration.GetSection("StorageConfig"));
builder.Services.Configure<ProcessingConfig>(builder.Configuration.GetSection("ProcessingConfig"));
builder.Services.AddSingleton<ITileRepository>(sp => new TileRepository(connectionString));
builder.Services.AddSingleton<IRegionRepository>(sp => new RegionRepository(connectionString));
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Satellite Provider API", Version = "v1" });
c.MapType<UploadImageRequest>(() => new OpenApiSchema
{
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>
{
["timestamp"] = new() { Type = "string", Format = "date-time", Description = "Image capture timestamp" },
["image"] = new() { Type = "string", Format = "binary", Description = "Image file to upload" },
["lat"] = new() { Type = "number", Format = "double", Description = "Latitude coordinate where image was captured" },
["lon"] = new() { Type = "number", Format = "double", Description = "Longitude coordinate where image was captured" },
["height"] = new() { Type = "number", Format = "double", Description = "Height/altitude in meters where image was captured" },
["focalLength"] = new() { Type = "number", Format = "double", Description = "Camera focal length in millimeters" },
["sensorWidth"] = new() { Type = "number", Format = "double", Description = "Camera sensor width in millimeters" },
["sensorHeight"] = new() { Type = "number", Format = "double", Description = "Camera sensor height in millimeters" }
},
Required = new HashSet<string> { "timestamp", "image", "lat", "lon", "height", "focalLength", "sensorWidth", "sensorHeight" }
});
c.OperationFilter<ParameterDescriptionFilter>();
});
var app = builder.Build();
var logger = app.Services.GetRequiredService<ILogger<Program>>();
var migrator = new DatabaseMigrator(connectionString, logger as ILogger<DatabaseMigrator>);
if (!migrator.RunMigrations())
{
throw new Exception("Database migration failed. Application cannot start.");
}
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.MapGet("/api/satellite/tiles/latlon", GetSatelliteTilesByLatLon)
.WithOpenApi(op => new(op) { Summary = "Get satellite tiles by latitude and longitude coordinates" });
app.MapGet("/api/satellite/tiles/mgrs", GetSatelliteTilesByMgrs)
.WithOpenApi(op => new(op) { Summary = "Get satellite tiles by MGRS coordinates" });
app.MapPost("/api/satellite/upload", UploadImage)
.Accepts<UploadImageRequest>("multipart/form-data")
.WithOpenApi(op => new(op) { Summary = "Upload image with metadata and save to /maps folder" })
.DisableAntiforgery();
app.Run();
IResult GetSatelliteTilesByLatLon(double lat, double lon, double squareSideMeters)
{
return Results.Ok(new GetSatelliteTilesResponse());
}
IResult GetSatelliteTilesByMgrs(string mgrs, double squareSideMeters)
{
return Results.Ok(new GetSatelliteTilesResponse());
}
IResult UploadImage([FromForm] UploadImageRequest request)
{
return Results.Ok(new SaveResult { Success = false });
}
public record GetSatelliteTilesResponse
{
public List<SatelliteTile> Tiles { get; set; } = new();
}
public record SatelliteTile
{
public string TileId { get; set; } = string.Empty;
public byte[] ImageData { get; set; } = Array.Empty<byte>();
public double Lat { get; set; }
public double Lon { get; set; }
public int ZoomLevel { get; set; }
}
public record UploadImageRequest
{
[Required]
public DateTime Timestamp { get; set; }
[Required]
public IFormFile? Image { get; set; }
[Required]
public double Lat { get; set; }
[Required]
public double Lon { get; set; }
[Required]
public double Height { get; set; }
[Required]
public double FocalLength { get; set; }
[Required]
public double SensorWidth { get; set; }
[Required]
public double SensorHeight { get; set; }
}
public record SaveResult
{
public bool Success { get; set; }
public string? Exception { get; set; }
}
public class ParameterDescriptionFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (operation.Parameters == null) return;
var parameterDescriptions = new Dictionary<string, string>
{
["lat"] = "Latitude coordinate where image was captured",
["lon"] = "Longitude coordinate where image was captured",
["mgrs"] = "MGRS coordinate string",
["squareSideMeters"] = "Square side size in meters"
};
foreach (var parameter in operation.Parameters)
{
if (parameterDescriptions.TryGetValue(parameter.Name, out var description))
{
parameter.Description = description;
}
}
}
}