From 156e2439aa981f24729e8889407c187b8c8d51e9 Mon Sep 17 00:00:00 2001 From: "aditya.chandel" <> Date: Wed, 22 Jan 2025 18:30:28 -0700 Subject: [PATCH] Fix pdf viewer when opening in docker. Have envs for angular. --- Dockerfile | 2 +- .../com/adityachandel/booklore/service/BooksService.java | 2 +- booklore-api/src/main/resources/application-local.yaml | 2 +- booklore-ui/angular.json | 6 ++++++ booklore-ui/src/app/config/api-config.ts | 7 +++---- booklore-ui/src/environments/environment.prod.ts | 7 +++++++ booklore-ui/src/environments/environment.ts | 7 +++++++ docker-compose.yml | 4 ++-- nginx.conf | 9 +++++++++ 9 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 booklore-ui/src/environments/environment.prod.ts create mode 100644 booklore-ui/src/environments/environment.ts diff --git a/Dockerfile b/Dockerfile index b9ef8bafc..308c6422f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,7 @@ COPY ./booklore-ui/package.json ./booklore-ui/package-lock.json ./ RUN npm install COPY ./booklore-ui /angular-app/ -RUN npm run build --prod +RUN npm run build --configuration=production # Stage 2: Build the Spring Boot app with Gradle FROM gradle:jdk21-alpine AS springboot-build diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/BooksService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/BooksService.java index 9146d1fa9..833de90e9 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/BooksService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/BooksService.java @@ -102,7 +102,7 @@ public class BooksService { BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId)); byte[] pdfBytes = Files.readAllBytes(new File(FileUtils.getBookFullPath(bookEntity)).toPath()); return ResponseEntity.ok() - .header(HttpHeaders.CONTENT_TYPE, "application/pdf") + .header(HttpHeaders.CONTENT_TYPE, "text/plain") .body(pdfBytes); } diff --git a/booklore-api/src/main/resources/application-local.yaml b/booklore-api/src/main/resources/application-local.yaml index abb9d40f0..ae6b5eda0 100644 --- a/booklore-api/src/main/resources/application-local.yaml +++ b/booklore-api/src/main/resources/application-local.yaml @@ -15,7 +15,7 @@ spring: name: booklore-api datasource: driver-class-name: org.h2.Driver - url: jdbc:h2:~/my-library-temp/config/db/booklore + url: jdbc:h2:~/my-library-temp/config/local/db/booklore username: sa password: password jpa: diff --git a/booklore-ui/angular.json b/booklore-ui/angular.json index d875fdc35..cf755daf6 100644 --- a/booklore-ui/angular.json +++ b/booklore-ui/angular.json @@ -52,6 +52,12 @@ }, "configurations": { "production": { + "fileReplacements": [ + { + "replace": "src/environments/environment.ts", + "with": "src/environments/environment.prod.ts" + } + ], "budgets": [ { "type": "initial", diff --git a/booklore-ui/src/app/config/api-config.ts b/booklore-ui/src/app/config/api-config.ts index f15418b79..aadaef199 100644 --- a/booklore-ui/src/app/config/api-config.ts +++ b/booklore-ui/src/app/config/api-config.ts @@ -1,4 +1,3 @@ -export const API_CONFIG = { - BASE_URL: `http://localhost:${window.location.port || 8080}`, - BROKER_URL: `ws://localhost:${window.location.port || 8080}/ws` -}; +import {environment} from '../../environments/environment'; + +export const API_CONFIG = environment.API_CONFIG; diff --git a/booklore-ui/src/environments/environment.prod.ts b/booklore-ui/src/environments/environment.prod.ts new file mode 100644 index 000000000..d0465426d --- /dev/null +++ b/booklore-ui/src/environments/environment.prod.ts @@ -0,0 +1,7 @@ +export const environment = { + production: true, + API_CONFIG: { + BASE_URL: `http://localhost:${window.location.port}`, + BROKER_URL: `ws://localhost:${window.location.port}/ws`, + }, +}; diff --git a/booklore-ui/src/environments/environment.ts b/booklore-ui/src/environments/environment.ts new file mode 100644 index 000000000..d7a529f5d --- /dev/null +++ b/booklore-ui/src/environments/environment.ts @@ -0,0 +1,7 @@ +export const environment = { + production: false, + API_CONFIG: { + BASE_URL: 'http://localhost:8080', + BROKER_URL: 'ws://localhost:8080/ws', + }, +}; diff --git a/docker-compose.yml b/docker-compose.yml index 72e24e4e3..311deffb7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,9 +2,9 @@ version: '3.8' services: booklore-app: container_name: booklore - image: localhost:5000/booklore-app:v24 + image: localhost:5000/booklore-app:v33 ports: - "5050:6060" volumes: - - /Users/aditya.chandel/my-library-temp/config:/app/config + - /Users/aditya.chandel/my-library-temp/config/docker:/app/config - /Users/aditya.chandel/my-library-temp/books:/app/books \ No newline at end of file diff --git a/nginx.conf b/nginx.conf index 5c50be8c2..58078a8b1 100644 --- a/nginx.conf +++ b/nginx.conf @@ -17,6 +17,15 @@ http { # Serve Angular UI location / { try_files $uri $uri/ /index.html; # Fallback to index.html for Angular routing + + location ~* \.mjs$ { + # target only *.mjs files + # now we can safely override types since we are only + # targeting a single file extension. + types { + text/javascript mjs; + } + } } # Proxy API requests that start with /api/ to the backend