From 7f6b4c2e0887bfc31e16a870184d99d91b13eeee Mon Sep 17 00:00:00 2001 From: Nils Beyer Date: Mon, 13 Nov 2023 10:46:45 +0100 Subject: [PATCH] Implement redirect for /api in docker deployment --- docker-compose.yml | 12 +----------- frontend/Dockerfile | 7 ++++++- frontend/docker-entrypoint.sh | 5 +++++ frontend/{nginx.conf => nginx.conf.template} | 2 +- frontend/src/app/app.module.ts | 4 ++-- frontend/src/environments/environment.ts | 2 +- 6 files changed, 16 insertions(+), 16 deletions(-) create mode 100644 frontend/docker-entrypoint.sh rename frontend/{nginx.conf => nginx.conf.template} (96%) diff --git a/docker-compose.yml b/docker-compose.yml index e99d135..60fd69e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -21,8 +21,6 @@ services: depends_on: - "db" restart: always - ports: - - "8080:8080" frontend: build: context: ./frontend @@ -35,12 +33,4 @@ services: ports: - "4200:80" environment: - # change this env-variable in prod - BACKEND_URL: http://backend/ - # alternative way manipulate the api.json: - #{ - # "api": "http://backend/" - #} - # bind-mount it read-only via: - #volumes: - # - ./api.json:/usr/share/nginx/html/assets/api.json:ro \ No newline at end of file + BACKEND_URL: http://backend:8080 diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 74b2d77..42b334f 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -12,7 +12,12 @@ RUN apt-get update RUN apt-get upgrade -y RUN apt-get install iputils-ping -y COPY --from=build /app/dist/frontend /usr/share/nginx/html -COPY nginx.conf /etc/nginx/nginx.conf +COPY nginx.conf.template /etc/nginx/nginx.conf.template +COPY docker-entrypoint.sh /app-entrypoint.sh + +RUN chmod +x /app-entrypoint.sh + +ENTRYPOINT [ "/app-entrypoint.sh" ] EXPOSE 80 CMD ["nginx", "-g", "daemon off;"] diff --git a/frontend/docker-entrypoint.sh b/frontend/docker-entrypoint.sh new file mode 100644 index 0000000..ced70a9 --- /dev/null +++ b/frontend/docker-entrypoint.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +cd /etc/nginx/ +envsubst '$BACKEND_URL' < nginx.conf.template > nginx.conf +exec "$@" diff --git a/frontend/nginx.conf b/frontend/nginx.conf.template similarity index 96% rename from frontend/nginx.conf rename to frontend/nginx.conf.template index 79fabd8..69925b9 100644 --- a/frontend/nginx.conf +++ b/frontend/nginx.conf.template @@ -34,7 +34,7 @@ http { add_header "Access-Control-Allow-Headers" 'X-Requested-With,Accept,Content-Type, Origin'; location /api { - proxy_pass http://backend/; + proxy_pass ${BACKEND_URL}; add_header "Access-Control-Allow-Origin" "*"; add_header "Access-Control-Allow-Methods" 'GET, POST, OPTIONS, PUT, DELETE'; add_header "Access-Control-Allow-Headers" 'X-Requested-With,Accept,Content-Type, Origin'; diff --git a/frontend/src/app/app.module.ts b/frontend/src/app/app.module.ts index f68fa17..807766c 100644 --- a/frontend/src/app/app.module.ts +++ b/frontend/src/app/app.module.ts @@ -6,7 +6,7 @@ import { AppComponent } from './app.component'; import {LoginComponent} from "./auth/login/login.component"; import {FormsModule, ReactiveFormsModule} from "@angular/forms"; import {HttpClient, HttpClientModule, HttpHandler} from "@angular/common/http"; -import {ApiModule, Configuration} from "../api"; +import {ApiModule, Configuration, ConfigurationParameters} from "../api"; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import {MatToolbarModule} from "@angular/material/toolbar"; import {MatIconModule} from "@angular/material/icon"; @@ -178,7 +178,7 @@ import {MatLegacySliderModule} from "@angular/material/legacy-slider"; // context "API_TOKEN": authService.getAccessToken.bind(authService) }, - //basePath: environment.api + basePath: environment.api, } ), deps: [AuthService], diff --git a/frontend/src/environments/environment.ts b/frontend/src/environments/environment.ts index bf9fcf4..43c1478 100644 --- a/frontend/src/environments/environment.ts +++ b/frontend/src/environments/environment.ts @@ -4,7 +4,7 @@ export const environment = { production: false, - api: "api" + api: "http://localhost:8080/api" }; /* -- 2.34.1