Skip to content

Commit

Permalink
feat: http 로 페이지 조회하면 https 로 리다이렉트 (#58)
Browse files Browse the repository at this point in the history
- https origin 까지 포함한 full url 로 리다이렉트
- URI#resolve 를 사용한 안전한 url 합성

* feat: root 경로에서 trailing slash 처리

* feat: short url code 경로에서 trailing slash 처리

* refactor: controller 구조 변경 및 app/ 경로 처리

* refactor: 안 보는 주석 삭제

* feat: 안전한 URL 합성

- URI#resolve 사용
- origin 영역까지 연결하여 full url 생성
  • Loading branch information
daengdaengLee authored Sep 15, 2023
1 parent daa4f13 commit 0619fb0
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,74 +126,3 @@ public Optional<String> 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<String> tableNamePrefix,
// Set<DdlAuto> ddlAuto) {
// public enum Type {
// IN_MEMORY,
// DYNAMODB
// }
//
// public enum DdlAuto {
// DROP,
// CREATE,
// VALIDATE
// }
// }
//
// @ConfigurationPropertiesBinding
// public record AwsProperties(
// @NotNull Region region,
// Optional<CredentialsProperties> credentials,
// Optional<DynamoDbProperties> 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<String> endpoint) {
// }
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";
}
}
2 changes: 1 addition & 1 deletion src/main/resources/mangurl/local.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mangurl:
origin: "http://localhost:8080/"
origin: "http://localhost:8080"
repository:
type: "dynamodb"
table-name-prefix: ""
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/mangurl/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mangurl:
origin: "http://localhost:8080/"
origin: "http://localhost:8080"
repository:
type: "in-memory"

0 comments on commit 0619fb0

Please sign in to comment.