Skip to content
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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/main/java/controller/Controller.java
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;
}
13 changes: 13 additions & 0 deletions src/main/java/controller/HomeController.java
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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정상적인 경로에 대한 응답은 redirect 보단 forward로 처리해주세요!
리다이렉트는 재요청을 명령하는 응답을 내리기 때문에 불필요하게 한 번 더 네트워크 통신이 발생해요. 네트워크 통신은 생각보다 비싸고, 리소스가 많이 소모되기 때문에 리다이렉트의 목적을 이해하고 필요한 곳에만 사용해야 합니다!
서버의 입장에서 서버 성능 개선에 가장 중요한 부분이 불필요한 외부 통신을 줄이는 것이란걸 알고 계시면 좋을 것 같습니다.

}
}
27 changes: 27 additions & 0 deletions src/main/java/controller/LoginController.java
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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서도 NullPointException이 발생할 수 있을 것 같지 않나요??

response.addCookie("logined=true");
response.redirect("/index.html");
}else{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1주 차 강의에서 else 예약어 사용을 지양하자고 언급했었죠. 1주 차 내용은 10주 차까지는 물론이고 , kuit 서버 파트를 넘어 백엔드 개발의 기반이 되는 내용이기 때문에 이 부분 계속 상기하면서 미션 진행하시면 좋을 것 같습니다!

response.redirect("/user/login_failed.html");
}
}

}
41 changes: 41 additions & 0 deletions src/main/java/controller/SignUpController.java
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");
}

}
21 changes: 21 additions & 0 deletions src/main/java/controller/UserListController.java
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");
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/webserver/HttpHeader.java
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");
Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}
8 changes: 8 additions & 0 deletions src/main/java/webserver/HttpMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package webserver;

public enum HttpMethod {
GET,
POST,
PUT,
DELETE;
}
19 changes: 19 additions & 0 deletions src/main/java/webserver/PathName.java
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;
}
}
Loading