diff --git a/ReadMe.md b/ReadMe.md index ac2fa49..9406171 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -17,4 +17,6 @@ - [x] HTTP(HTTPS) URL 검증 구현 - [x] URL 단축 알고리즘 구현 - [x] URL 단축 저장 및 조회 기능 구현 -- [ ] 테스트 및 배포 \ No newline at end of file +- [x] 프론트엔드 페이지 구현 +- [ ] 테스트 및 배포 +- [ ] 추가 기능 구현 (예정)
(회원 기능 등) diff --git a/src/main/java/be/gyu/urlShortener/controller/MainController.java b/src/main/java/be/gyu/urlShortener/controller/MainController.java new file mode 100644 index 0000000..693998f --- /dev/null +++ b/src/main/java/be/gyu/urlShortener/controller/MainController.java @@ -0,0 +1,62 @@ +package be.gyu.urlShortener.controller; + +import java.util.Map; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import be.gyu.urlShortener.service.MainService; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +@Controller +public class MainController { + @Autowired + private MainService mainService; + + /** + * Main Index 페이지 매핑 메소드 + * @return index 페이지의 View Name을 반환 + */ + @GetMapping("/") + public String getIndex(){ + return "index"; + } + + /** + * 입력한 원본 URL을 검증 및 단축 후, 결과 반환 메소드 + * @param request Request 객체 + * @return 결과 값을 담아서 ResponseEntity> 객체로 반환 (json 형태로 전송) + */ + @PostMapping("/short") + @ResponseBody + public ResponseEntity> postGenerateShortUrl(HttpServletRequest request){ + // 서비스 로직 메소드 호출 + Map resultMap=mainService.generateShortUrl(request.getParameter("url")); + + if(!resultMap.containsKey("status") || resultMap.get("status").equals("failed")){ + resultMap.put("status","failed"); + resultMap.put("message","생성 실패"); + } + + return ResponseEntity.ok(resultMap); + } + + /** + * 단축 URL 조회 및 원본 URL로 Re-Direct 처리 메소드 + * @param shortUrl 단축 URL + * @param response Re-Direct 응답 처리를 위한 매개변수 + */ + @GetMapping("/{shortUrl}") + public void getShortUrl(@PathVariable(name="shortUrl") String shortUrl, HttpServletResponse response){ + String originalUrl=mainService.getOriginalUrl(shortUrl); + response.setStatus(HttpStatus.MOVED_PERMANENTLY.value()); + response.addHeader("Location",originalUrl); + } +}