Skip to content

Commit

Permalink
Updated the CORS policy
Browse files Browse the repository at this point in the history
Signed-off-by: Emil Balitzki <[email protected]>
  • Loading branch information
Corgam committed Jun 11, 2024
1 parent df5cece commit 3e04dac
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
19 changes: 16 additions & 3 deletions backend/api-gateway/Controllers/APIGatewayController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public APIGatewayController(HttpClient httpClient, ILogger<APIGatewayController>
[ProducesResponseType(400)]
[ProducesResponseType(500)]
public async Task<IActionResult> GetDatasetViewportData(
[FromQuery, Required] int datasetID,
[FromQuery, Required] string datasetID,
[FromQuery] int zoomLevel,
[FromQuery, Required] float BottomLat,
[FromQuery, Required] float BottomLong,
Expand All @@ -49,8 +49,21 @@ public async Task<IActionResult> GetDatasetViewportData(
}

_logger.LogInformation($"Fetching data for DatasetID: {datasetID}, ZoomLevel: {zoomLevel}, Viewport: [{BottomLat}, {BottomLong}] to [{TopLat}, {TopLong}]");
// Here the port 80 is used not 8080. This is due to the docker containers using the interal ports to communicate and not the external ones.
var targetUrl = $"http://api-composer:80/api/v1.0/Dataset/1/data?ZoomLevel={zoomLevel}&BottomLat={BottomLat}&BottomLong={BottomLong}&TopLat={TopLat}&TopLong={TopLong}";

string targetUrl;
if (datasetID == "charging_stations")
{
targetUrl = $"http://api-composer:80/api/v1.0/Dataset/1/data?ZoomLevel={zoomLevel}&BottomLat={BottomLat}&BottomLong={BottomLong}&TopLat={TopLat}&TopLong={TopLong}";
}
else if (datasetID == "house_footprints")
{
targetUrl = $"http://api-composer:80/api/v1.0/Dataset/2/data?ZoomLevel={zoomLevel}&BottomLat={BottomLat}&BottomLong={BottomLong}&TopLat={TopLat}&TopLong={TopLong}";
}
else
{
_logger.LogError($"Unsupported dataset ID of {datasetID}");
return StatusCode(400, $"Unsupported dataset ID of {datasetID}");
}

try
{
Expand Down
13 changes: 9 additions & 4 deletions backend/api-gateway/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ public void ConfigureServices(IServiceCollection services)
services.AddControllers();
services.AddHttpClient();

// Add CORS services to allow all origins
// Add CORS services to allow specific origins
services.AddCors(options =>
{
options.AddPolicy("AllowAll",
options.AddPolicy("AllowSpecificOrigins",
builder =>
{
builder.AllowAnyOrigin()
builder.WithOrigins("http://localhost",
"http://test.amos.b-ci.de",
"http://prod.amos.b-ci.de",
"http://test.amos.b-ci.de:*",
"http://prod.amos.b-ci.de:*",
"http://localhost:*")
.AllowAnyHeader()
.AllowAnyMethod();
});
Expand Down Expand Up @@ -61,7 +66,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
});

// Enable CORS
app.UseCors("AllowAll");
app.UseCors("AllowSpecificOrigins");

app.UseRouting();

Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/MapView/DataFetch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const useGeoData = (
return "";
default:
return (
"http://" +
currentEnvironment.apiBaseHost +
":" +
currentEnvironment.apiBasePort +
Expand Down

0 comments on commit 3e04dac

Please sign in to comment.