Skip to content

Commit

Permalink
Fixed FE-BE connection bug #251 (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
Corgam authored Jun 11, 2024
2 parents 787d543 + 3e04dac commit 64a9902
Show file tree
Hide file tree
Showing 22 changed files with 326 additions and 86 deletions.
10 changes: 6 additions & 4 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# The environment stage set for individual services
ENVIRONMENT_STAGE=development
# The tag used for pulling the docker images
DOCKER_COMPOSE_IMAGES_TAG=test
DOCKER_COMPOSE_IMAGES_TAG=latest

### HOSTS & PORTS ###

Expand All @@ -12,14 +12,16 @@ FRONTEND_HOST=frontend
FRONTEND_PORT=80

# Backend - API Gateway
BACKEND_API_GATEWAY_PORT=8082
API_GATEWAY_HOST=localhost
API_GATEWAY_PORT=8081

# Backend - API Composer
VITE_BACKEND_HOST=localhost
VITE_BACKEND_PORT=8081
API_COMPOSER_HOST=localhost
API_COMPOSER_PORT=8082

### METADATA DATABASE ###

METADATA_DATABASE_PORT=27018
MONGO_INITDB_ROOT_USERNAME=admin
MONGO_INITDB_ROOT_PASSWORD=secret

Expand Down
10 changes: 6 additions & 4 deletions .env.production
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# The environment stage set for individual services
ENVIRONMENT_STAGE=production
# The tag used for pulling the docker images
DOCKER_COMPOSE_IMAGES_TAG=production
DOCKER_COMPOSE_IMAGES_TAG=latest

### HOSTS & PORTS ###

Expand All @@ -12,14 +12,16 @@ FRONTEND_HOST=frontend
FRONTEND_PORT=80

# Backend - API Gateway
BACKEND_API_GATEWAY_PORT=8082
API_GATEWAY_HOST=prod.amos.b-ci.de
API_GATEWAY_PORT=8081

# Backend - API Composer
VITE_BACKEND_HOST=prod.amos.b-ci.de
VITE_BACKEND_PORT=8081
API_COMPOSER_HOST=prod.amos.b-ci.de
API_COMPOSER_PORT=8082

### METADATA DATABASE ###

METADATA_DATABASE_PORT=27018
MONGO_INITDB_ROOT_USERNAME=admin
MONGO_INITDB_ROOT_PASSWORD=secret

Expand Down
8 changes: 5 additions & 3 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ FRONTEND_HOST=frontend
FRONTEND_PORT=80

# Backend - API Gateway
BACKEND_API_GATEWAY_PORT=8082
API_GATEWAY_HOST=test.amos.b-ci.de
API_GATEWAY_PORT=8081

# Backend - API Composer
VITE_BACKEND_HOST=test.amos.b-ci.de
VITE_BACKEND_PORT=8081
API_COMPOSER_HOST=test.amos.b-ci.de
API_COMPOSER_PORT=8082

### METADATA DATABASE ###

METADATA_DATABASE_PORT=27018
MONGO_INITDB_ROOT_USERNAME=admin
MONGO_INITDB_ROOT_PASSWORD=secret

Expand Down
10 changes: 7 additions & 3 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ labels: 'business value feature'
assignees: ''

---
## Description


## User story
1. As a {}
Expand All @@ -18,9 +20,11 @@ assignees: ''
* ...

## Definition of done (DoD)
* Added only after week 5
* The same for all features
* Here goes the project specific part
* Component tests have been written and pass
* All CI tests passed for the pull request
* Code review has been completed and code has been merged into sprint-release branch
* All feature branches have been merged and closed
* New feature code has been documented

## DoD general criteria
* Feature has been fully implemented
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/deploy_production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ jobs:
type=ref,event=tag
type=sha
type=raw,value=${{ env.STAGE }}
latest
labels: |
stage=${{ env.STAGE }}
Expand Down
Binary file added Deliverables/sprint-06/build-documentation.pdf
Binary file not shown.
Binary file added Deliverables/sprint-06/design-documentation.pdf
Binary file not shown.
Binary file added Deliverables/sprint-06/feature-board.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
151 changes: 151 additions & 0 deletions Deliverables/sprint-06/feature-board.tsv

Large diffs are not rendered by default.

Binary file added Deliverables/sprint-06/imp-squared-backlog.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions Deliverables/sprint-06/imp-squared-backlog.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Title Assignees Status Priority Estimate Size Iteration
Stay on topic in Meetings Done
Documentation mainly on GitHub Wiki Done
Read user story and acceptance during sprint review Done
Smaller, atomic issues Done
Frontend/Backend team communication Done
Avoid pushing decsions to extra meetings Done
Small follow-up summary messages after meetings Done
Using our new milestones - epics - tickets structure Done
Earlier feature preparation for sprint Done
Plan mid-term release In Progress
Code review and code documentation In Progress
Better concept for pull request: Discuss in meeting In Progress
Discuss deliverables sooner In Progress
Binary file added Deliverables/sprint-06/planning-documents.pdf
Binary file not shown.
Binary file added Deliverables/sprint-06/user-documentation.pdf
Binary file not shown.
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
20 changes: 20 additions & 0 deletions backend/api-gateway/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ public void ConfigureServices(IServiceCollection services)
services.AddControllers();
services.AddHttpClient();

// Add CORS services to allow specific origins
services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigins",
builder =>
{
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();
});
});

// Add Swagger services
services.AddSwaggerGen(c =>
{
Expand Down Expand Up @@ -48,6 +65,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
c.RoutePrefix = string.Empty; // Set Swagger UI at the root
});

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

app.UseRouting();

app.UseEndpoints(endpoints =>
Expand Down
4 changes: 0 additions & 4 deletions backend/src/BIE.Core/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ RUN dotnet publish "BIE.Core.API.csproj" -c Release -o /app/publish
### DEPLOY ###
FROM base AS final

# Set the environment variables TO-DO
#ENV TRUSTED="true"
#ENV DB_TYPE="SQL"

WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "BIE.Core.API.dll"]
19 changes: 11 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ services:
image: ghcr.io/amosproj/amos2024ss04-building-information-enhancer-frontend:${DOCKER_COMPOSE_IMAGES_TAG}
environment:
- NODE_ENV=${ENVIRONMENT_STAGE}
- API_GATEWAY_HOST=${API_GATEWAY_HOST}
- API_GATEWAY_PORT=${API_GATEWAY_PORT}
networks:
- bie-network
ports:
Expand All @@ -23,7 +25,7 @@ services:
environment:
- ASPNETCORE_ENVIRONMENT=${ENVIRONMENT_STAGE}
ports:
- ${BACKEND_API_GATEWAY_PORT}:8080
- ${API_GATEWAY_PORT}:8080
networks:
- bie-network

Expand All @@ -43,7 +45,7 @@ services:
- DB_TYPE=${SQL_DB_TYPE}
- TRUSTED=${SQL_TRUSTED}
ports:
- ${VITE_BACKEND_PORT}:80
- ${API_COMPOSER_PORT}:80
networks:
- bie-network
depends_on:
Expand Down Expand Up @@ -80,6 +82,7 @@ services:
# volumes:
# - bie-mssqldata:/var/opt/mssql

## The database for storing datasets metadata, accessible by multiple services
metadata-database:
build:
context: ./backend/metadata-database
Expand All @@ -90,15 +93,15 @@ services:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
ports:
- "27018:27017"
- ${METADATA_DATABASE_PORT}:27017
networks:
- bie-network
volumes:
- bie-metadata:/data/db
# volumes:
# - bie-metadata:/data/db

volumes:
bie-metadata:
# bie-mssqldata:
# volumes:
# bie-metadata:
# bie-mssqldata:

networks:
bie-network:
Expand Down
11 changes: 0 additions & 11 deletions frontend/.env

This file was deleted.

29 changes: 18 additions & 11 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Dockerfile

# Load the node image from Docker Hub
FROM node:20-slim as builder

Expand All @@ -8,29 +10,34 @@ WORKDIR /usr/src/app
ARG STAGE
ENV VITE_STAGE=${STAGE}

### Install dependencies ###

# Copy package.json and package-lock.json to the working directory
# Install dependencies
COPY package.json ./
COPY package-lock.json ./

# Install dependencies
RUN npm ci

# Upload the files
ADD . .

RUN npm run build

### Deploy the server ###
# Deploy the Apache HTTP
FROM httpd:2.4-alpine

# Use Apache HTTP server
FROM httpd:2.4
# Install dos2unix to convert line endings
RUN apk add --no-cache dos2unix

COPY --from=builder /usr/src/app/dist /usr/local/apache2/htdocs/
COPY replace_api_url.sh /usr/local/apache2/htdocs/
COPY httpd.conf /usr/local/apache2/conf/httpd.conf

# Convert line endings and ensure the script is executable
RUN dos2unix /usr/local/apache2/htdocs/replace_api_url.sh && chmod +x /usr/local/apache2/htdocs/replace_api_url.sh

# Set default values for environment variables
ENV API_GATEWAY_HOST="localhost"
ENV API_GATEWAY_PORT="8081"

# Expose the port
EXPOSE "${PORT}"

# Start web server
CMD ["httpd-foreground"]
# Start web server after running the script
CMD sh /usr/local/apache2/htdocs/replace_api_url.sh "${API_GATEWAY_HOST}" "${API_GATEWAY_PORT}"
35 changes: 35 additions & 0 deletions frontend/httpd.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
ServerName localhost

# Load necessary modules
LoadModule mpm_event_module modules/mod_mpm_event.so
LoadModule headers_module modules/mod_headers.so
LoadModule unixd_module modules/mod_unixd.so
LoadModule dir_module modules/mod_dir.so
LoadModule mime_module modules/mod_mime.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule authz_host_module modules/mod_authz_host.so

# Set the port to listen on
Listen 80

# Enable CORS
<IfModule mod_headers.c>
Header Set Access-Control-Allow-Origin "*"
Header Set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
Header Set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept, Authorization"
</IfModule>

# Directory settings
<Directory "/usr/local/apache2/htdocs">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>

# Log settings
ErrorLog /usr/local/apache2/logs/error_log
CustomLog /usr/local/apache2/logs/access_log common

# Include other necessary configurations
Include conf/extra/httpd-default.conf
18 changes: 18 additions & 0 deletions frontend/replace_api_url.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# Variables for the new values
NEW_API_GATEWAY_HOST=$1
echo "Host to replace: $1"
NEW_API_GATEWAY_PORT=$2
echo "Port to replace: $2"

# Directory containing the .js files
DIRECTORY="."

# Find and replace the API_GATEWAY_HOST and API_GATEWAY_PORT in all .js files
find "$DIRECTORY" -type f -name "*.js" -exec sed -i \
-e "s|API_GATEWAY_HOST|$NEW_API_GATEWAY_HOST|g" \
-e "s|API_GATEWAY_PORT|$NEW_API_GATEWAY_PORT|g" {} \;

echo "API Gateway URL replacement completed, starting Apache HTTPD server..."
httpd-foreground
Loading

0 comments on commit 64a9902

Please sign in to comment.