diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 08e8987..bce4f8f 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -28,6 +28,7 @@ services: environment: SPRING_PROFILES_ACTIVE: prod SPRING_DATASOURCE_URL: jdbc:postgresql://${RDS_ENDPOINT}:5432/tinyurl_production_db + TINYURL_FRONTEND_URL: https://tinyurl.buffden.com expose: - "8080" healthcheck: diff --git a/tinyurl/src/main/java/com/tinyurl/config/AppProperties.java b/tinyurl/src/main/java/com/tinyurl/config/AppProperties.java index becf65e..c2dbacf 100644 --- a/tinyurl/src/main/java/com/tinyurl/config/AppProperties.java +++ b/tinyurl/src/main/java/com/tinyurl/config/AppProperties.java @@ -7,6 +7,7 @@ @ConfigurationProperties(prefix = "tinyurl") public record AppProperties( String baseUrl, + String frontendUrl, Integer defaultExpiryDays, Integer shortCodeMinLength, Cors cors diff --git a/tinyurl/src/main/java/com/tinyurl/controller/RedirectController.java b/tinyurl/src/main/java/com/tinyurl/controller/RedirectController.java index 0439be2..8f01877 100644 --- a/tinyurl/src/main/java/com/tinyurl/controller/RedirectController.java +++ b/tinyurl/src/main/java/com/tinyurl/controller/RedirectController.java @@ -1,11 +1,13 @@ package com.tinyurl.controller; +import com.tinyurl.config.AppProperties; import com.tinyurl.dto.UrlMapping; -import com.tinyurl.exception.NotFoundException; +import com.tinyurl.exception.GoneException; +import com.tinyurl.service.UrlService; import jakarta.validation.constraints.Pattern; import jakarta.validation.constraints.Size; -import com.tinyurl.service.UrlService; import java.net.URI; +import java.util.Optional; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -19,9 +21,11 @@ public class RedirectController { private final UrlService urlService; + private final AppProperties appProperties; - public RedirectController(UrlService urlService) { + public RedirectController(UrlService urlService, AppProperties appProperties) { this.urlService = urlService; + this.appProperties = appProperties; } @GetMapping("/{shortCode}") @@ -31,12 +35,26 @@ public ResponseEntity redirect( @Pattern(regexp = "^[0-9A-Za-z]+$", message = "INVALID_URL") String shortCode ) { - UrlMapping mapping = urlService.resolveCode(shortCode) - .orElseThrow(() -> new NotFoundException("No URL found for this short code.")); + String notFoundUrl = appProperties.frontendUrl() + "/not-found"; + + Optional mapping; + try { + mapping = urlService.resolveCode(shortCode); + } catch (GoneException e) { + HttpHeaders headers = new HttpHeaders(); + headers.setLocation(URI.create(notFoundUrl)); + return new ResponseEntity<>(headers, HttpStatus.FOUND); + } + + if (mapping.isEmpty()) { + HttpHeaders headers = new HttpHeaders(); + headers.setLocation(URI.create(notFoundUrl)); + return new ResponseEntity<>(headers, HttpStatus.FOUND); + } HttpHeaders headers = new HttpHeaders(); - headers.setLocation(URI.create(mapping.originalUrl())); - HttpStatus status = mapping.explicitExpiry() ? HttpStatus.FOUND : HttpStatus.MOVED_PERMANENTLY; + headers.setLocation(URI.create(mapping.get().originalUrl())); + HttpStatus status = mapping.get().explicitExpiry() ? HttpStatus.FOUND : HttpStatus.MOVED_PERMANENTLY; return new ResponseEntity<>(headers, status); } } diff --git a/tinyurl/src/main/resources/application.yaml b/tinyurl/src/main/resources/application.yaml index 6ef7ad9..eaa61eb 100644 --- a/tinyurl/src/main/resources/application.yaml +++ b/tinyurl/src/main/resources/application.yaml @@ -42,6 +42,7 @@ management: tinyurl: base-url: ${TINYURL_BASE_URL:http://localhost} + frontend-url: ${TINYURL_FRONTEND_URL:http://localhost:4200} default-expiry-days: ${TINYURL_DEFAULT_EXPIRY_DAYS:180} short-code-min-length: ${TINYURL_SHORT_CODE_MIN_LENGTH:6} cors: diff --git a/tinyurl/src/test/java/com/tinyurl/service/UrlServiceImplTest.java b/tinyurl/src/test/java/com/tinyurl/service/UrlServiceImplTest.java index e50cbd5..5622b7c 100644 --- a/tinyurl/src/test/java/com/tinyurl/service/UrlServiceImplTest.java +++ b/tinyurl/src/test/java/com/tinyurl/service/UrlServiceImplTest.java @@ -38,7 +38,7 @@ class UrlServiceImplTest { @BeforeEach void setUp() { - service = new UrlServiceImpl(urlRepository, base62Encoder, new AppProperties("http://localhost", 180, 6, null)); + service = new UrlServiceImpl(urlRepository, base62Encoder, new AppProperties("http://localhost", "http://localhost:4200", 180, 6, null)); } @Test