Skip to content

[Spring Core]주병주 미션 제출합니다. #70

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

Open
wants to merge 73 commits into
base: gotobill
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
18b8ea6
1-2-3단계 커밋
Jun 24, 2024
ac9d564
수정
Jun 24, 2024
0194a4a
삼단계 누락 추가
Jun 25, 2024
58bb75f
리뷰 반영
Jun 30, 2024
84fb7a1
리뷰 반영
Jun 30, 2024
f53da3a
리뷰 반영2
Jun 30, 2024
e6ea575
jpa 4,5,6
Jul 2, 2024
6f3270b
jpa modified
Jul 2, 2024
f450b0f
7,8 단계
Jul 8, 2024
f5d81d5
7단계 수정
Jul 8, 2024
0ced918
7 retry
Jul 8, 2024
9552f5f
add: ci.yml add
Jul 21, 2024
bd79489
ci : test
Jul 21, 2024
5469490
ci : core branch add
Jul 21, 2024
a43cc9e
ci : core branch add after -> test
Jul 21, 2024
4206a81
ci : test 0.0.1
Jul 21, 2024
9915ae8
ci : test 0.0.2
Jul 21, 2024
dab6831
ci > ci/cd
Jul 21, 2024
2bf3d7b
ci > ci/cd.001
Jul 21, 2024
ea30f53
ci > ci/cd.002 -x test
Jul 21, 2024
0c12843
ci > ci/cd.003 key update
Jul 21, 2024
41d7316
ci > ci/cd.004 ci/cd check
Jul 21, 2024
bf448cc
ci > ci/cd.005 ci/cd check:main->gotobill-core
Jul 21, 2024
aefd93c
ci > ci/cd.006 ci/cd check:main->gotobill-core
Jul 21, 2024
5db14e0
ci > ci/cd.007 배포 test
Jul 21, 2024
9d5f106
ci > ci/cd.008 kill now process and run
Jul 21, 2024
aa081a4
ci > ci/cd.009 add: after nohup sleep 10
Jul 21, 2024
be84bd3
ci > ci/cd.010 -: after nohup sleep 10
Jul 21, 2024
59b528c
ci > ci/cd.011 add: kill process
Jul 21, 2024
3dec885
ci > ci/cd.012 add: kill process before pull
Jul 21, 2024
05b16f2
ci > ci/cd.013 add: kill process before pull
Jul 21, 2024
ffb620e
ci > ci/cd.014 add: kill process before pull
Jul 21, 2024
972306f
ci > ci/cd.015 add: kill process before pull
Jul 21, 2024
b5ca92a
ci > ci/cd.016 add: kill process before pull
Jul 21, 2024
c9771d3
ci > ci/cd.017 last
Jul 21, 2024
4be8a68
ci > ci/cd.018
Jul 21, 2024
f4ede7d
ci > ci/cd.019
Jul 21, 2024
8aa3c81
ci > ci/cd.20
Jul 21, 2024
4599618
ci > ci/cd.21
Jul 21, 2024
2309e53
ci > ci/cd.22
Jul 21, 2024
38e646a
ci > ci/cd.23
Jul 21, 2024
dc4aee2
ci > ci/cd.24
Jul 21, 2024
320e50b
ci > ci/cd.25
Jul 21, 2024
abc990a
ci > ci/cd.26
Jul 21, 2024
fccb995
ci > ci/cd.27
Jul 21, 2024
dabedb8
ci > ci/cd.28
Jul 21, 2024
5821086
add: nginx 30
Jul 22, 2024
fe59f04
add: nginx 30
Jul 22, 2024
565bc8d
add: nginx 31
Jul 22, 2024
e4959af
add: nginx 32
Jul 22, 2024
b1cb56f
add: nginx 33
Jul 22, 2024
db17df5
add: nginx 34
Jul 22, 2024
cdbf9b2
add: nginx 35
Jul 22, 2024
9755601
add docker
GoToBILL Aug 8, 2024
25fdc65
add docker :01
GoToBILL Aug 8, 2024
b2efc64
add docker :02
GoToBILL Aug 12, 2024
3efd6bd
add docker :03
GoToBILL Aug 12, 2024
80642c0
add docker :04
GoToBILL Aug 12, 2024
08a4470
add docker :05
GoToBILL Aug 12, 2024
4203c1a
add docker :06
GoToBILL Aug 12, 2024
d568742
add docker :07
GoToBILL Aug 12, 2024
5a3f2e8
add docker :08
GoToBILL Aug 12, 2024
39078a3
add docker :09
GoToBILL Aug 12, 2024
8373483
add docker :10
GoToBILL Aug 12, 2024
8d9dabe
add docker :11
GoToBILL Aug 12, 2024
31f5f98
add docker :12
GoToBILL Aug 12, 2024
2a2950f
add docker :13
GoToBILL Aug 12, 2024
496db22
add docker :14
GoToBILL Aug 12, 2024
0d264df
add docker :15
GoToBILL Aug 12, 2024
0f76376
add docker :16
GoToBILL Aug 12, 2024
fbae782
add docker :17
GoToBILL Aug 12, 2024
157de6d
add docker :18
GoToBILL Aug 12, 2024
df0350e
add docker :19
GoToBILL Aug 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: CI/CD Pipeline

on:
pull_request:
branches:
- main
- gotobill-core
push:
branches:
- main
- gotobill-core

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: 'adopt'
java-version: '17'

- name: Install Docker Compose
run: |
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

- name: Build with Gradle
run: ./gradlew clean -x test build

- name: Copy JAR for Docker
run: cp build/libs/spring-basic-roomescape-playground-0.0.1-SNAPSHOT.jar app.jar

- name: Upload app.jar as artifact
uses: actions/upload-artifact@v3
with:
name: app-jar
path: ./app.jar

- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

- name: Build and Push Docker Image
run: docker-compose build && docker-compose push

deploy:
if: github.event_name == 'push'
needs: build
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Download app.jar artifact
uses: actions/download-artifact@v3
with:
name: app-jar
path: .

- name: Deploy with Docker Compose
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
run: |
echo "$SSH_PRIVATE_KEY" > private_key.pem
chmod 600 private_key.pem
scp -i private_key.pem -o StrictHostKeyChecking=no \
docker-compose.yml \
build.sh \
app.jar \
Dockerfile \
${{ secrets.EC2_USERNAME }}@${{ secrets.EC2_HOST }}:/home/${{ secrets.EC2_USERNAME }}/

ssh -i private_key.pem -o StrictHostKeyChecking=no ${{ secrets.EC2_USERNAME }}@${{ secrets.EC2_HOST }} "chmod +x build.sh && ./build.sh"
rm private_key.pem
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# OpenJDK 17을 기반으로 하는 이미지를 사용합니다
FROM openjdk:17-jdk-slim

# 컨테이너 내의 작업 디렉터리를 설정합니다
WORKDIR /app

# 호스트의 JAR 파일을 컨테이너로 복사합니다
COPY ./app.jar app.jar

# 애플리케이션이 사용할 포트를 노출합니다
EXPOSE 8080

# JAR 파일을 실행하는 명령을 지정합니다
CMD ["java", "-jar", "app.jar"]
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'dev.akkinoc.spring.boot:logback-access-spring-boot-starter:4.0.0'
implementation 'org.springframework.boot:spring-boot-starter'

implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
implementation 'io.jsonwebtoken:jjwt-impl:0.11.2'
Expand Down
33 changes: 33 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

# 현재 구동 중인 애플리케이션 확인
CURRENT_APP=$(docker-compose ps | grep "app_blue" | grep "Up" || echo "")

# 새 버전을 배포할 대상 결정
if [[ -n "$CURRENT_APP" ]]; then
TARGET="app_green"
else
TARGET="app_blue"
fi

echo "배포할 대상: $TARGET"

# Docker 이미지 빌드
docker-compose build $TARGET

# 새로운 버전 실행
docker-compose up -d $TARGET

# 새로운 버전이 구동될 때까지 대기
echo "새로운 버전의 애플리케이션이 구동될 때까지 대기 중..."
sleep 15

# 오래된 버전 중지
if [[ "$TARGET" == "app_green" ]]; then
OLD_APP="app_blue"
else
OLD_APP="app_green"
fi

echo "오래된 버전 중지: $OLD_APP"
docker-compose stop $OLD_APP
37 changes: 37 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
services:
app_blue:
build:
context: .
dockerfile: Dockerfile
image: gotobill/app_blue:latest
container_name: roomescape-app-blue
environment:
- SERVER_PORT=8081
ports:
- "8081:8081"
restart: always

app_green:
build:
context: .
dockerfile: Dockerfile
image: gotobill/app_green:latest
container_name: roomescape-app-green
environment:
- SERVER_PORT=8082
ports:
- "8082:8082"
restart: always

nginx:
build:
context: ./nginx
dockerfile: Dockerfile
image: gotobill/nginx:latest
ports:
- "80:80"
depends_on:
- app_blue
- app_green
container_name: roomescape-nginx
restart: always
5 changes: 5 additions & 0 deletions nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Nginx 이미지를 기반으로 설정합니다
FROM nginx:alpine

# Nginx 설정 파일을 컨테이너로 복사합니다
COPY nginx.conf /etc/nginx/conf.d/default.conf
18 changes: 18 additions & 0 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
upstream backend {
server app_blue:8081;
server app_green:8082;
}

server {
listen 80;

server_name 52.64.39.97; # 서버의 공인 IP 주소를 설정합니다

location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
16 changes: 16 additions & 0 deletions src/main/java/auth/AuthConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package auth;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AuthConfig {

@Bean
public JwtUtils jwtUtils(@Value("${roomescape.auth.jwt.secret}") String secretKey) {
return new JwtUtils(secretKey);
// reload:
// rel
}
}
7 changes: 7 additions & 0 deletions src/main/java/auth/JwtTokenMember.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package auth;

public record JwtTokenMember(
String name,
String role
) {
}
44 changes: 44 additions & 0 deletions src/main/java/auth/JwtUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package auth;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import roomescape.member.Member;

public class JwtUtils {

private final String secretKey;

public JwtUtils(String secretKey) {
this.secretKey = secretKey;
}

public String createToken(Member member){
return Jwts.builder()
.setSubject(member.getId().toString())
.claim("name", member.getName())
.claim("role", member.getRole())
.signWith(Keys.hmacShaKeyFor(secretKey.getBytes()))
.compact();
}

public JwtTokenMember extractToken(String token) {
Claims claims = Jwts.parserBuilder()
.setSigningKey(Keys.hmacShaKeyFor(secretKey.getBytes()))
.build()
.parseClaimsJws(token)
.getBody();
String name = claims.get("name", String.class);
String role = claims.get("role", String.class);
return new JwtTokenMember(name,role);
}
public Long decodeToken(String token) {
if (token == null) return null;
return Long.valueOf(Jwts.parserBuilder()
.setSigningKey(Keys.hmacShaKeyFor(secretKey.getBytes()))
.build()
.parseClaimsJws(token)
.getBody().getSubject());
}

}
33 changes: 33 additions & 0 deletions src/main/java/roomescape/AppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package roomescape;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import roomescape.argumentResolver.LoginMemberArgumentResolver;
import roomescape.interceptor.AuthInterceptor;

import java.util.List;

@Configuration
public class AppConfig implements WebMvcConfigurer {

private final LoginMemberArgumentResolver loginMemberArgumentResolver;
private final AuthInterceptor authInterceptor;

public AppConfig(LoginMemberArgumentResolver loginMemberArgumentResolver, AuthInterceptor authInterceptor) {
this.loginMemberArgumentResolver = loginMemberArgumentResolver;
this.authInterceptor = authInterceptor;
}

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(loginMemberArgumentResolver);
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authInterceptor)
.addPathPatterns("/admin/**");
}
}
35 changes: 35 additions & 0 deletions src/main/java/roomescape/Login/LoginController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package roomescape.Login;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import roomescape.member.MemberResponse;

@RestController
public class LoginController {
private final LoginService loginService;

public LoginController(LoginService loginService) {
this.loginService = loginService;
}

@GetMapping("check_uptodata")
public String login(){
return "good";
}
@PostMapping("/login")
public void login(@RequestBody LoginRequest loginRequest, HttpServletResponse response) {
String token = loginService.login(loginRequest); //토큰 받음
Cookie cookie = new Cookie("token", token);
cookie.setHttpOnly(true);
cookie.setPath("/");
response.addCookie(cookie);
}

@GetMapping("/login/check")
public MemberResponse loginCheck(
@CookieValue("token") String token) {
return loginService.checkLogin(token);
}
}
6 changes: 6 additions & 0 deletions src/main/java/roomescape/Login/LoginMember.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package roomescape.Login;

public record LoginMember(
Long id, String name, String email, String role
) {
}
7 changes: 7 additions & 0 deletions src/main/java/roomescape/Login/LoginRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package roomescape.Login;

public record LoginRequest(
String email,
String password
) {
}
Loading