Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@ConfigurationProperties(prefix = "tinyurl")
public record AppProperties(
String baseUrl,
String frontendUrl,
Integer defaultExpiryDays,
Integer shortCodeMinLength,
Cors cors
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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}")
Expand All @@ -31,12 +35,26 @@ public ResponseEntity<Void> 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<UrlMapping> 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);
}
}
1 change: 1 addition & 0 deletions tinyurl/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading