-
Notifications
You must be signed in to change notification settings - Fork 24
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주차 미션 / 서버 1조 김지현 #6
base: main
Are you sure you want to change the base?
Changes from all commits
b4a8477
73e6cc8
b817e4b
3192b08
f136b9a
57f30ee
54d807b
8c5831f
88af81f
e6e5dfc
8c399ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package controller; | ||
|
||
import webserver.Response; | ||
import webserver.Request; | ||
|
||
import java.io.IOException; | ||
|
||
public interface Controller { | ||
void execute(Request request, Response response) throws IOException; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package controller; | ||
|
||
import webserver.Request; | ||
import webserver.Response; | ||
|
||
import java.io.IOException; | ||
|
||
public class HomeController implements Controller{ | ||
@Override | ||
public void execute(Request request, Response response) throws IOException { | ||
response.redirect("/index.html"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package controller; | ||
|
||
import db.MemoryUserRepository; | ||
import model.User; | ||
import webserver.*; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
public class LoginController implements Controller{ | ||
@Override | ||
public void execute(Request request, Response response) throws IOException { | ||
|
||
Map<String, String> queryParams = request.getBodyParams(); | ||
String userId = queryParams.get(UserQueryKey.USER_ID.getKey()); | ||
String password = queryParams.get(UserQueryKey.PASSWORD.getKey()); | ||
|
||
User user = MemoryUserRepository.getInstance().findUserById(userId); //findUser 여기서 user 받기 | ||
|
||
if(user.getPassword().equals(password)) { | ||
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. 여기서도 NullPointException이 발생할 수 있을 것 같지 않나요?? |
||
response.addCookie("logined=true"); | ||
response.redirect("/index.html"); | ||
}else{ | ||
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. 1주 차 강의에서 else 예약어 사용을 지양하자고 언급했었죠. 1주 차 내용은 10주 차까지는 물론이고 , kuit 서버 파트를 넘어 백엔드 개발의 기반이 되는 내용이기 때문에 이 부분 계속 상기하면서 미션 진행하시면 좋을 것 같습니다! |
||
response.redirect("/user/login_failed.html"); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package controller; | ||
|
||
import db.MemoryUserRepository; | ||
import model.User; | ||
import webserver.*; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class SignUpController implements Controller { | ||
private final MemoryUserRepository memoryUserRepository = MemoryUserRepository.getInstance(); | ||
|
||
@Override | ||
public void execute(Request request, Response response) throws IOException { | ||
|
||
|
||
String method = request.getMethod(); | ||
Map<String, String> queryParams = new HashMap<>(); | ||
|
||
if (method.equals(HttpMethod.GET.toString())) { | ||
queryParams = request.getQueryParams(); | ||
}else if(method.equals(HttpMethod.POST.toString())){ | ||
queryParams = request.getBodyParams(); | ||
} | ||
|
||
// 사용자 정보 저장 | ||
String userId = queryParams.get(UserQueryKey.USER_ID.getKey()); | ||
String password = queryParams.get(UserQueryKey.PASSWORD.getKey()); | ||
String name = queryParams.get(UserQueryKey.NAME.getKey()); | ||
String email = queryParams.get(UserQueryKey.EMAIL.getKey()); | ||
|
||
|
||
if (userId != null && name != null && password != null) { | ||
User newUser = new User(userId, password, name, email); | ||
memoryUserRepository.addUser(newUser); | ||
} | ||
|
||
response.redirect("/index.html"); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package controller; | ||
|
||
import webserver.Request; | ||
import webserver.Response; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
public class UserListController implements Controller{ | ||
@Override | ||
public void execute(Request request, Response response) throws IOException { | ||
Map<String, String> headers = request.getHeaders(); | ||
String cookie = headers.get("Cookie"); | ||
System.out.println(cookie); | ||
|
||
if (cookie != null && cookie.contains("logined=true")) { | ||
response.redirect("/user/list.html"); | ||
} else { | ||
response.redirect("/user/login.html"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package webserver; | ||
|
||
public enum HttpHeader { | ||
CONTENT_LENGTH("Content-Length"), | ||
LOCATION("Location"), | ||
COOKIE("Cookie"), | ||
CONTENT_TYPE("Content-Type"), | ||
CONNECTION("Connection"), | ||
HTTP_302("HTTP/1.1 302 Found"); | ||
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. 이 친구는 헤더 값이 아닌 것 같죠..? 👀 |
||
|
||
private final String headerValue; | ||
|
||
HttpHeader(String headerValue) { | ||
this.headerValue = headerValue; | ||
} | ||
|
||
public String getHeaderValue() { | ||
return headerValue; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package webserver; | ||
|
||
public enum HttpMethod { | ||
GET, | ||
POST, | ||
PUT, | ||
DELETE; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package webserver; | ||
|
||
public enum PathName { | ||
SIGNUP("/user/signup"), | ||
LOGIN("/user/login"), | ||
USER_LIST("/user/userList"), | ||
HOME("/"); | ||
|
||
|
||
private final String pathName; | ||
|
||
PathName(String pathName) { | ||
this.pathName = pathName; | ||
} | ||
|
||
public String getPathName() { | ||
return pathName; | ||
} | ||
} |
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로 처리해주세요!
리다이렉트는 재요청을 명령하는 응답을 내리기 때문에 불필요하게 한 번 더 네트워크 통신이 발생해요. 네트워크 통신은 생각보다 비싸고, 리소스가 많이 소모되기 때문에 리다이렉트의 목적을 이해하고 필요한 곳에만 사용해야 합니다!
서버의 입장에서 서버 성능 개선에 가장 중요한 부분이 불필요한 외부 통신을 줄이는 것이란걸 알고 계시면 좋을 것 같습니다.