-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
3주차 미션 / 서버 2조 임제형 #3
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package controller; | ||
|
||
import java.io.IOException; | ||
import http.HttpRequest; | ||
import http.HttpResponse; | ||
|
||
public interface Controller { | ||
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package controller; | ||
|
||
import java.io.IOException; | ||
import http.HttpRequest; | ||
import http.HttpResponse; | ||
|
||
// url이 html로 들어오면 200으로 body에 html 파일 보내주기 | ||
public class ForwardController implements Controller{ | ||
|
||
@Override | ||
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException { | ||
String path = httpRequest.getUrl(); | ||
httpResponse.forward(path); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package controller; | ||
|
||
import enums.route.StaticRoute; | ||
import http.HttpRequest; | ||
import http.HttpResponse; | ||
|
||
// url이 "/"로 들어오면 302로 /index.html로 redirect, 페이지 반환은 재요청 받으면 200이 반환 | ||
public class HomeController implements Controller{ | ||
|
||
@Override | ||
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
String location = StaticRoute.INDEX_HTML.getRoute(); | ||
httpResponse.redirect(location); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package controller; | ||
|
||
import enums.http.header.RequestHeader; | ||
import http.HttpRequest; | ||
import http.HttpResponse; | ||
|
||
import static enums.route.StaticRoute.*; | ||
|
||
public class ListController implements Controller { | ||
@Override | ||
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
|
||
String cookie = httpRequest.getHeaderMap().get(RequestHeader.COOKIE.getValue()); | ||
|
||
if (cookie != null && cookie.contains("logined=true")) { | ||
String location = USER_LIST_HTML.getRoute(); | ||
httpResponse.redirect(location); | ||
return; | ||
} | ||
|
||
String location = LOGIN_HTML.getRoute(); | ||
httpResponse.redirect(location); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package controller; | ||
|
||
import db.MemoryUserRepository; | ||
import db.Repository; | ||
import http.HttpRequest; | ||
import http.HttpResponse; | ||
import model.User; | ||
|
||
import static enums.http.HttpMethod.POST; | ||
import static enums.key.UserQueryKey.PASSWORD; | ||
import static enums.key.UserQueryKey.USERID; | ||
import static enums.route.StaticRoute.*; | ||
|
||
public class LoginController implements Controller{ | ||
Repository repository = MemoryUserRepository.getInstance(); | ||
|
||
@Override | ||
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
|
||
boolean isLoginSuccessed = false; | ||
// if (httpRequest.getHttpMethod().equals("GET")){ } // 일단 POST 방식으로 전송되니까 보류 | ||
if (httpRequest.getHttpMethod().equals(POST.getValue())) { | ||
String userId = httpRequest.getBodyMap().get(USERID.getValue()); | ||
String password = httpRequest.getBodyMap().get(PASSWORD.getValue()); | ||
|
||
isLoginSuccessed = checkIdPwValid(userId, password); | ||
} | ||
|
||
if(isLoginSuccessed){ | ||
String location = INDEX_HTML.getRoute(); | ||
httpResponse.redirectSettingCookie(location); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. early return 을 사용해 보는건 어떨까요? |
||
} | ||
if(!isLoginSuccessed){ | ||
String location = LOGIN_FAILED_HTML.getRoute(); | ||
httpResponse.redirect(location); | ||
} | ||
} | ||
|
||
private boolean checkIdPwValid(String userId, String password){ | ||
User user = repository.findUserById(userId); | ||
if(user != null && user.getPassword().equals(password)) return true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 조건문 없이 `user != null && user.getPassword().equals(password) 만 리턴해도 결과는 같을 거 같네요. |
||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package controller; | ||
|
||
import db.MemoryUserRepository; | ||
import db.Repository; | ||
import http.HttpRequest; | ||
import http.HttpResponse; | ||
import model.User; | ||
|
||
import java.util.Map; | ||
|
||
import static enums.http.HttpMethod.GET; | ||
import static enums.http.HttpMethod.POST; | ||
import static enums.key.UserQueryKey.*; | ||
import static enums.route.StaticRoute.*; | ||
|
||
public class SignUpController implements Controller{ | ||
Repository repository = MemoryUserRepository.getInstance(); | ||
|
||
@Override | ||
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
|
||
if (httpRequest.getHttpMethod().equals(GET.getValue())){ | ||
Map<String, String> queryMap = httpRequest.getQueryMap(); | ||
User user = makeUserFromMap(queryMap); | ||
repository.addUser(user); | ||
} | ||
|
||
if (httpRequest.getHttpMethod().equals(POST.getValue())) { | ||
Map<String, String> bodyMap = httpRequest.getBodyMap(); | ||
User user = makeUserFromMap(bodyMap); | ||
repository.addUser(user); | ||
} | ||
|
||
String location = INDEX_HTML.getRoute(); | ||
httpResponse.redirect(location); | ||
} | ||
|
||
private User makeUserFromMap(Map<String, String> map){ | ||
String userId = map.get(USERID.getValue()); | ||
String password = map.get(PASSWORD.getValue()); | ||
String name = map.get(NAME.getValue()); | ||
String email = map.get(EMAIL.getValue()); | ||
return User.of(userId, password, name, email); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ public static MemoryUserRepository getInstance() { | |
|
||
public void addUser(User user) { | ||
users.put(user.getUserId(), user); | ||
System.out.println(user.getUserId()+"님 이 회원가입 하였습니다."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 실무에선 System.out을 사용하지 않습니다! 지금부터라도 log 사용에 익숙해 지시는게 좋을 것 같습니다! |
||
} | ||
|
||
public User findUserById(String userId) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package enums.exception; | ||
|
||
public enum ExceptionMessage { | ||
INVALID_START_LINE("요청의 1번째 줄이 비어있습니다."), | ||
INVALID_REQUEST_URL("요청의 URL이 유효하지 않습니다."); | ||
|
||
private final String message; | ||
|
||
ExceptionMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package enums.extension; | ||
|
||
public enum FileExtension { | ||
HTML("html"), CSS("css"); | ||
|
||
private final String value; | ||
|
||
FileExtension(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public String addFrontPoint() { | ||
return "."+value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package enums.http; | ||
|
||
public enum HttpMethod { | ||
GET("GET"), | ||
POST("POST"); | ||
|
||
private final String value; | ||
|
||
HttpMethod(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package enums.http; | ||
|
||
public enum HttpStatus { | ||
OK("HTTP/1.1 200 OK \r\n"), | ||
REDIRECT("HTTP/1.1 302 Found \r\n"); | ||
|
||
private final String value; | ||
|
||
HttpStatus(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package enums.http.header; | ||
|
||
public enum EntityHeader { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 음... 이건 제 생각이지만, HttpHeader 이넘클래스 하나로 상수를 관리하는게 좋지 않을까요? 어떤 헤더가 EntityHeader에 포함되는지 일일히 기억하기 쉽지 않을 것 같습니다..! |
||
CONTENTTYPE("Content-Type"), | ||
CONTENTLENGTH("Content-Length"); | ||
|
||
private final String value; | ||
|
||
EntityHeader(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public String toResponseString() { | ||
return value + ": "; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package enums.http.header; | ||
|
||
public enum RequestHeader { | ||
COOKIE("Cookie"); | ||
|
||
private final String value; | ||
|
||
RequestHeader(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package enums.http.header; | ||
|
||
public enum ResponseHeader { | ||
LOCATION("Location"), | ||
SETCOOKIE("Set-Cookie"); | ||
|
||
private final String value; | ||
|
||
ResponseHeader(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public String toResponseString() { | ||
return value + ": "; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package enums.key; | ||
|
||
public enum UserQueryKey { | ||
USERID("userId"), | ||
PASSWORD("password"), | ||
NAME("name"), | ||
EMAIL("email"); | ||
|
||
private final String value; | ||
|
||
UserQueryKey(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package enums.route; | ||
|
||
public enum PageRoute { | ||
HOME("/"), | ||
SIGNUP("/user/signup"), | ||
LOGIN("/user/login"), | ||
USER_LIST("/user/userList"); | ||
|
||
private final String route; | ||
|
||
PageRoute(String route) { | ||
this.route = route; | ||
} | ||
|
||
public String getRoute() { | ||
return route; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package enums.route; | ||
|
||
public enum StaticRoute { | ||
INDEX_HTML("/index.html"), | ||
LOGIN_HTML("/user/login.html"), | ||
LOGIN_FAILED_HTML("/user/login_failed.html"), | ||
USER_LIST_HTML("/user/list.html"); | ||
|
||
private final String route; | ||
|
||
StaticRoute(String route) { | ||
this.route = route; | ||
} | ||
|
||
public String getRoute() { | ||
return route; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
정상적인 경로에 대한 요청을 응답할 경우, redirect 보단 forward를 사용하면 좋을 것 같습니다. redirect 는 응답 후 get으로 재요청을 명령하기 때문에 네트워크 통신하는데 리소스가 더 소모됩니다!