diff --git a/src/main/java/io/github/daengdaenglee/mangurl/config/properties/MangurlProperties.java b/src/main/java/io/github/daengdaenglee/mangurl/config/properties/MangurlProperties.java index 6f615f7..0dd6e2b 100644 --- a/src/main/java/io/github/daengdaenglee/mangurl/config/properties/MangurlProperties.java +++ b/src/main/java/io/github/daengdaenglee/mangurl/config/properties/MangurlProperties.java @@ -126,74 +126,3 @@ public Optional getEndpoint() { } } } - -//public record MangurlProperties( -// @NotNull -// String origin, -// @NotNull -// @NestedConfigurationProperty -// RepositoryProperties repository, -// @NestedConfigurationProperty -// AwsProperties aws) { -//// @ConstructorBinding -// public MangurlProperties( -// String origin, -// RepositoryProperties repository, -// AwsProperties aws) { -// this.origin = origin == null ? "" : origin; -// this.repository = repository; -// this.aws = aws == null ? new AwsProperties() : aws; -// } -// -// @ConfigurationPropertiesBinding -// public record RepositoryProperties( -// @NotNull Type type, -// Optional tableNamePrefix, -// Set ddlAuto) { -// public enum Type { -// IN_MEMORY, -// DYNAMODB -// } -// -// public enum DdlAuto { -// DROP, -// CREATE, -// VALIDATE -// } -// } -// -// @ConfigurationPropertiesBinding -// public record AwsProperties( -// @NotNull Region region, -// Optional credentials, -// Optional dynamoDb) { -// @ConstructorBinding -// public AwsProperties( -// String region, -// CredentialsProperties credentials, -// DynamoDbProperties dynamoDb) { -// this( -// Region.regions() -// .stream() -// .filter(r -> r.toString().equals(region)) -// .findAny() -// .orElse(null), -// Optional.ofNullable(credentials), -// Optional.ofNullable(dynamoDb)); -// -// } -// -// public AwsProperties() { -// this(Region.AP_NORTHEAST_2, Optional.empty(), Optional.empty()); -//// this(null, Optional.empty(), Optional.empty()); -// } -// -// @ConfigurationPropertiesBinding -// public record CredentialsProperties(@NotNull String accessKeyId, @NotNull String secretAccessKey) { -// } -// -// @ConfigurationPropertiesBinding -// public record DynamoDbProperties(Optional endpoint) { -// } -// } -//} diff --git a/src/main/java/io/github/daengdaenglee/mangurl/inboundadapter/api/ApiController.java b/src/main/java/io/github/daengdaenglee/mangurl/inboundadapter/api/ApiController.java index c0ae4e5..83cbb34 100644 --- a/src/main/java/io/github/daengdaenglee/mangurl/inboundadapter/api/ApiController.java +++ b/src/main/java/io/github/daengdaenglee/mangurl/inboundadapter/api/ApiController.java @@ -11,6 +11,8 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.server.ResponseStatusException; +import java.net.URI; + @RestController @RequestMapping("/api") class ApiController { @@ -35,9 +37,10 @@ ShortenResponse shorten(@RequestBody ShortenRequest shortenRequest) { // @TODO RestControllerAdvice 에서 처리 throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "잘못된 URL 입니다."); } + var shortUrl = URI.create(this.origin).resolve(shortUrlCode).toASCIIString(); return ShortenResponse .builder() - .shortUrl(this.origin + shortUrlCode) + .shortUrl(shortUrl) .build(); } } diff --git a/src/main/java/io/github/daengdaenglee/mangurl/inboundadapter/app/AppController.java b/src/main/java/io/github/daengdaenglee/mangurl/inboundadapter/app/AppController.java index 5fa1450..935dbd9 100644 --- a/src/main/java/io/github/daengdaenglee/mangurl/inboundadapter/app/AppController.java +++ b/src/main/java/io/github/daengdaenglee/mangurl/inboundadapter/app/AppController.java @@ -12,10 +12,14 @@ import org.springframework.web.server.ResponseStatusException; import org.springframework.web.servlet.mvc.support.RedirectAttributes; +import java.net.URI; + @Slf4j @Controller -@RequestMapping("/app") +@RequestMapping(AppController.basePath) class AppController { + static final String basePath = "/app"; + private final String origin; private final EncodeUrlService encodeUrlService; private final ShortenUrlService shortenUrlService; @@ -29,6 +33,11 @@ class AppController { this.shortenUrlService = shortenUrlService; } + @GetMapping("/") + String appSlash() { + return "redirect:" + this.createFullUrl(AppController.basePath); + } + @GetMapping String app( @RequestParam(value = "result", required = false) String result, @@ -75,11 +84,15 @@ String shorten( // @TODO binding result 를 이용하여 오류 메세지 보여주기 throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "잘못된 URL 입니다."); } - var shortUrl = this.origin + shortUrlCode; + var shortUrl = this.createFullUrl(shortUrlCode); redirectAttributes.addAttribute("result", ""); redirectAttributes.addAttribute("resOriginalUrl", originalUrl); redirectAttributes.addAttribute("resShortUrl", shortUrl); - return "redirect:/app"; + return "redirect:" + this.createFullUrl(AppController.basePath); + } + + private String createFullUrl(String path) { + return URI.create(this.origin).resolve(path).toASCIIString(); } } diff --git a/src/main/java/io/github/daengdaenglee/mangurl/inboundadapter/app/RootController.java b/src/main/java/io/github/daengdaenglee/mangurl/inboundadapter/app/RootController.java new file mode 100644 index 0000000..24f703a --- /dev/null +++ b/src/main/java/io/github/daengdaenglee/mangurl/inboundadapter/app/RootController.java @@ -0,0 +1,22 @@ +package io.github.daengdaenglee.mangurl.inboundadapter.app; + +import io.github.daengdaenglee.mangurl.config.properties.MangurlProperties; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +import java.net.URI; + +@Controller +class RootController { + private final String origin; + + RootController(MangurlProperties mangurlProperties) { + this.origin = mangurlProperties.getOrigin(); + } + + @RequestMapping({"", "/"}) + String home() { + var appUrl = URI.create(this.origin).resolve(AppController.basePath).toASCIIString(); + return "redirect:" + appUrl; + } +} diff --git a/src/main/java/io/github/daengdaenglee/mangurl/inboundadapter/root/RootController.java b/src/main/java/io/github/daengdaenglee/mangurl/inboundadapter/shorturl/ShortUrlController.java similarity index 83% rename from src/main/java/io/github/daengdaenglee/mangurl/inboundadapter/root/RootController.java rename to src/main/java/io/github/daengdaenglee/mangurl/inboundadapter/shorturl/ShortUrlController.java index 6892287..77655f8 100644 --- a/src/main/java/io/github/daengdaenglee/mangurl/inboundadapter/root/RootController.java +++ b/src/main/java/io/github/daengdaenglee/mangurl/inboundadapter/shorturl/ShortUrlController.java @@ -1,4 +1,4 @@ -package io.github.daengdaenglee.mangurl.inboundadapter.root; +package io.github.daengdaenglee.mangurl.inboundadapter.shorturl; import io.github.daengdaenglee.mangurl.application.url.inboundport.EncodeUrlService; import io.github.daengdaenglee.mangurl.application.url.inboundport.RestoreUrlService; @@ -13,20 +13,15 @@ @Slf4j @RequiredArgsConstructor @Controller -class RootController { +class ShortUrlController { private final RestoreUrlService restoreUrlService; private final EncodeUrlService encodeUrlService; - @RequestMapping("/{shortUrlCode}") + @RequestMapping({"/{shortUrlCode}", "/{shortUrlCode}/"}) String redirect(@PathVariable String shortUrlCode) { return this.restoreUrlService.restoreUrl(shortUrlCode) .map(this.encodeUrlService::encode) .map(url -> "redirect:" + url) .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND)); } - - @RequestMapping - String home() { - return "redirect:/app"; - } } diff --git a/src/main/resources/mangurl/local.yml b/src/main/resources/mangurl/local.yml index 7195606..3d28285 100644 --- a/src/main/resources/mangurl/local.yml +++ b/src/main/resources/mangurl/local.yml @@ -1,5 +1,5 @@ mangurl: - origin: "http://localhost:8080/" + origin: "http://localhost:8080" repository: type: "dynamodb" table-name-prefix: "" diff --git a/src/main/resources/mangurl/test.yml b/src/main/resources/mangurl/test.yml index a4d1363..24c2a51 100644 --- a/src/main/resources/mangurl/test.yml +++ b/src/main/resources/mangurl/test.yml @@ -1,4 +1,4 @@ mangurl: - origin: "http://localhost:8080/" + origin: "http://localhost:8080" repository: type: "in-memory"