Skip to content

Commit e0823a8

Browse files
authored
[FIX]: 인프라 및 마이그레이션 수정 (#63)
* Chore: 디렉토리 수정 * refactor: 리소스 분리 * chore: redis 노출 여부 추가 * chore: 운영 전용 스키마 추가 * fix: localhost로 적힌 부분 수정 * fix: 세션 저장을 위한 직렬화 요소 추가 * feat: 개발 환경 용 redis 추가 * fix: 스크립트 수정 * fix: 프로덕션 환경에 맞게 수정
1 parent 2caff7a commit e0823a8

File tree

19 files changed

+1066
-725
lines changed

19 files changed

+1066
-725
lines changed

back/docker-compose-dev.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: OneTop-Relife
2+
3+
services:
4+
5+
redis:
6+
image: redis:8.2-alpine
7+
container_name: redis_1
8+
restart: unless-stopped
9+
command: redis-server --appendonly no
10+
networks:
11+
- common
12+
ports:
13+
- "6379:6379"
14+
environment:
15+
TZ: "Asia/Seoul"
16+
volumes:
17+
- /dockerProjects/redis_1/volumes/data:/data
18+
19+
networks:
20+
common:
21+
driver: bridge

back/src/main/java/com/back/domain/user/entity/User.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import lombok.*;
1111
import org.springframework.data.annotation.LastModifiedDate;
1212

13+
import java.io.Serializable;
1314
import java.time.LocalDateTime;
1415
import java.util.ArrayList;
1516
import java.util.List;
@@ -25,7 +26,7 @@
2526
@NoArgsConstructor
2627
@AllArgsConstructor
2728
@Builder
28-
public class User extends BaseEntity {
29+
public class User extends BaseEntity implements Serializable {
2930

3031
@Column(nullable = false, unique = true)
3132
private String email;

back/src/main/java/com/back/global/config/CorsConfig.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.back.global.config;
22

3+
import org.springframework.beans.factory.annotation.Value;
34
import org.springframework.context.annotation.Bean;
45
import org.springframework.context.annotation.Configuration;
56
import org.springframework.web.cors.CorsConfiguration;
@@ -13,10 +14,13 @@
1314
*/
1415
@Configuration
1516
public class CorsConfig {
17+
@Value("${custom.site.frontUrl}")
18+
private String frontUrl;
19+
1620
@Bean
1721
public CorsConfigurationSource corsConfigurationSource(){
1822
CorsConfiguration conf = new CorsConfiguration();
19-
conf.setAllowedOriginPatterns(List.of("http://localhost:*"));
23+
conf.setAllowedOriginPatterns(List.of("http://localhost:*", frontUrl));
2024
conf.setAllowedMethods(List.of("GET","POST","PUT","DELETE","PATCH","OPTIONS"));
2125
conf.setAllowedHeaders(List.of("*"));
2226
conf.setAllowCredentials(true);

back/src/main/java/com/back/global/security/oauth2/OAuth2FailureHandler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import jakarta.servlet.http.HttpServletRequest;
55
import jakarta.servlet.http.HttpServletResponse;
66
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.beans.factory.annotation.Value;
78
import org.springframework.security.core.AuthenticationException;
89
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
910
import org.springframework.stereotype.Component;
@@ -19,11 +20,14 @@
1920
@Component
2021
public class OAuth2FailureHandler extends SimpleUrlAuthenticationFailureHandler {
2122

23+
@Value("${custom.site.frontUrl}")
24+
private String frontUrl;
25+
2226
@Override
2327
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
2428
log.error("OAuth2 Login Failure: {}", exception.getMessage());
2529

26-
String targetUrl = UriComponentsBuilder.fromUriString("http://localhost:3000/oauth2/redirect")
30+
String targetUrl = UriComponentsBuilder.fromUriString(frontUrl + "/oauth2/redirect")
2731
.queryParam("error", exception.getMessage())
2832
.build().toUriString();
2933

back/src/main/java/com/back/global/security/oauth2/OAuth2SuccessHandler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import jakarta.servlet.http.HttpServletRequest;
77
import jakarta.servlet.http.HttpServletResponse;
88
import lombok.extern.slf4j.Slf4j;
9+
import org.springframework.beans.factory.annotation.Value;
910
import org.springframework.security.core.Authentication;
1011
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
1112
import org.springframework.stereotype.Component;
@@ -20,6 +21,9 @@
2021
@Slf4j
2122
public class OAuth2SuccessHandler implements AuthenticationSuccessHandler {
2223

24+
@Value("${custom.site.frontUrl}")
25+
private String frontUrl;
26+
2327
@Override
2428
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
2529
Authentication authentication) throws IOException, ServletException {
@@ -31,6 +35,6 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
3135

3236
log.info("OAuth2 로그인 완료 - 사용자: {} ({})", user.getEmail(), user.getAuthProvider());
3337

34-
response.sendRedirect("http://localhost:3000/oauth2/redirect?success=true");
38+
response.sendRedirect(frontUrl + "/oauth2/redirect?success=true");
3539
}
3640
}
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
-- ========================================
2+
-- 사용자 테이블
3+
-- ========================================
4+
CREATE TABLE users (
5+
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
6+
created_date TIMESTAMP(6),
7+
auth_provider VARCHAR(10) CHECK (auth_provider IN ('GITHUB','GOOGLE','GUEST','LOCAL')),
8+
beliefs VARCHAR(255),
9+
birthday_at TIMESTAMP(6) NOT NULL,
10+
email VARCHAR(255) NOT NULL,
11+
gender VARCHAR(1) CHECK (gender IN ('F','M','N','O')),
12+
life_satis INTEGER,
13+
mbti VARCHAR(4) CHECK (mbti IN ('ENFJ','ENFP','ENTJ','ENTP','ESFJ','ESFP','ESTJ','ESTP','INFJ','INFP','INTJ','INTP','ISFJ','ISFP','ISTJ','ISTP')),
14+
nickname VARCHAR(80) NOT NULL,
15+
password VARCHAR(255),
16+
relationship INTEGER,
17+
risk_avoid INTEGER,
18+
role VARCHAR(10) CHECK (role IN ('ADMIN','GUEST','USER')) NOT NULL,
19+
updated_at TIMESTAMP(6),
20+
username VARCHAR(30) NOT NULL,
21+
work_life_bal INTEGER
22+
);
23+
24+
-- ========================================
25+
-- 베이스라인 및 노드 테이블
26+
-- ========================================
27+
CREATE TABLE base_lines (
28+
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
29+
created_date TIMESTAMP(6),
30+
title VARCHAR(100) NOT NULL,
31+
user_id BIGINT NOT NULL
32+
);
33+
34+
CREATE TABLE base_nodes (
35+
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
36+
created_date TIMESTAMP(6),
37+
age_year INTEGER NOT NULL,
38+
alt_opt1 VARCHAR(255),
39+
alt_opt1target_decision_id BIGINT,
40+
alt_opt2 VARCHAR(255),
41+
alt_opt2target_decision_id BIGINT,
42+
category VARCHAR(20) CHECK (category IN ('CAREER','EDUCATION','ETC','FINANCE','HEALTH','LOCATION','RELATIONSHIP')),
43+
decision TEXT,
44+
description TEXT,
45+
fixed_choice VARCHAR(255),
46+
node_kind VARCHAR(10) CHECK (node_kind IN ('BASE','DECISION')) NOT NULL,
47+
situation TEXT,
48+
base_line_id BIGINT,
49+
parent_node_id BIGINT,
50+
user_id BIGINT NOT NULL
51+
);
52+
53+
CREATE TABLE decision_lines (
54+
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
55+
created_date TIMESTAMP(6),
56+
status VARCHAR(10) CHECK (status IN ('CANCELLED','COMPLETED','DRAFT')) NOT NULL,
57+
base_line_id BIGINT NOT NULL,
58+
user_id BIGINT NOT NULL
59+
);
60+
61+
CREATE TABLE decision_nodes (
62+
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
63+
created_date TIMESTAMP(6),
64+
age_year INTEGER NOT NULL,
65+
background TEXT,
66+
category VARCHAR(20) CHECK (category IN ('CAREER','EDUCATION','ETC','FINANCE','HEALTH','LOCATION','RELATIONSHIP')),
67+
decision TEXT,
68+
description TEXT,
69+
node_kind VARCHAR(10) CHECK (node_kind IN ('BASE','DECISION')) NOT NULL,
70+
option1 VARCHAR(255),
71+
option2 VARCHAR(255),
72+
option3 VARCHAR(255),
73+
parent_option_index INTEGER,
74+
selected_index INTEGER,
75+
situation TEXT,
76+
base_node_id BIGINT,
77+
dec_line_id BIGINT NOT NULL,
78+
parent_node_id BIGINT,
79+
user_id BIGINT NOT NULL
80+
);
81+
82+
-- ========================================
83+
-- 포스트 및 댓글 테이블
84+
-- ========================================
85+
CREATE TABLE post (
86+
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
87+
created_date TIMESTAMP(6),
88+
category VARCHAR(10) CHECK (category IN ('CHAT','POLL','SCENARIO')),
89+
content TEXT,
90+
hide BOOLEAN DEFAULT TRUE NOT NULL,
91+
like_count INTEGER NOT NULL,
92+
title VARCHAR(200),
93+
updated_at TIMESTAMP(6),
94+
vote_content JSONB,
95+
scenario_id BIGINT,
96+
user_id BIGINT
97+
);
98+
99+
CREATE TABLE comments (
100+
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
101+
created_date TIMESTAMP(6),
102+
content TEXT NOT NULL,
103+
hide BOOLEAN DEFAULT TRUE NOT NULL,
104+
like_count INTEGER NOT NULL,
105+
updated_at TIMESTAMP(6),
106+
post_id BIGINT NOT NULL,
107+
user_id BIGINT NOT NULL
108+
);
109+
110+
CREATE TABLE comment_likes (
111+
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
112+
created_date TIMESTAMP(6),
113+
comment_id BIGINT,
114+
user_id BIGINT
115+
);
116+
117+
CREATE TABLE post_likes (
118+
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
119+
created_date TIMESTAMP(6),
120+
post_id BIGINT,
121+
user_id BIGINT
122+
);
123+
124+
-- ========================================
125+
-- 투표 및 시나리오 테이블
126+
-- ========================================
127+
CREATE TABLE poll_votes (
128+
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
129+
created_date TIMESTAMP(6),
130+
choice_json JSONB NOT NULL,
131+
poll_uid UUID NOT NULL,
132+
post_id BIGINT NOT NULL,
133+
user_id BIGINT
134+
);
135+
136+
CREATE TABLE scenarios (
137+
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
138+
created_date TIMESTAMP(6),
139+
description TEXT,
140+
error_message TEXT,
141+
img VARCHAR(255),
142+
job VARCHAR(200),
143+
status VARCHAR(15) CHECK (status IN ('COMPLETED','FAILED','PENDING','PROCESSING')) NOT NULL,
144+
summary TEXT,
145+
timeline_titles TEXT,
146+
total INTEGER NOT NULL,
147+
updated_date TIMESTAMP(6),
148+
base_line_id BIGINT NOT NULL,
149+
decision_line_id BIGINT,
150+
user_id BIGINT NOT NULL
151+
);
152+
153+
CREATE TABLE scene_compare (
154+
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
155+
created_date TIMESTAMP(6),
156+
compare_result TEXT,
157+
result_type VARCHAR(10) CHECK (result_type IN ('TOTAL','건강','경제','관계','직업','행복')),
158+
scenario_id BIGINT NOT NULL
159+
);
160+
161+
CREATE TABLE scene_type (
162+
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
163+
created_date TIMESTAMP(6),
164+
analysis TEXT,
165+
point INTEGER NOT NULL,
166+
type VARCHAR(10) CHECK (type IN ('건강','경제','관계','직업','행복')),
167+
scenarios_id BIGINT
168+
);
169+
170+
-- ========================================
171+
-- 인덱스 정의
172+
-- ========================================
173+
CREATE INDEX idx_post_category_created ON post (category, created_date DESC);
174+
CREATE INDEX idx_post_user_created ON post (user_id, created_date DESC);
175+
CREATE INDEX idx_post_title ON post (title);
176+
CREATE INDEX idx_comment_post_created ON comments (post_id, created_date DESC);
177+
CREATE INDEX idx_comment_post_like ON comments (post_id, like_count DESC);
178+
179+
-- ========================================
180+
-- 유니크 제약조건
181+
-- ========================================
182+
ALTER TABLE users ADD CONSTRAINT uk_users_email UNIQUE (email);
183+
ALTER TABLE users ADD CONSTRAINT uk_users_nickname UNIQUE (nickname);
184+
ALTER TABLE comment_likes ADD CONSTRAINT comment_like_uk UNIQUE (comment_id, user_id);
185+
ALTER TABLE decision_nodes ADD CONSTRAINT uq_decision_line_age UNIQUE (dec_line_id, age_year);
186+
ALTER TABLE poll_votes ADD CONSTRAINT uq_logged_in_once UNIQUE (post_id, poll_uid, user_id);
187+
ALTER TABLE post ADD CONSTRAINT uq_post_scenario UNIQUE (scenario_id);
188+
ALTER TABLE post_likes ADD CONSTRAINT post_like_uk UNIQUE (post_id, user_id);
189+
ALTER TABLE scenarios ADD CONSTRAINT uq_scenario_decision_line UNIQUE (decision_line_id);
190+
191+
-- ========================================
192+
-- 외래키 제약조건
193+
-- ========================================
194+
ALTER TABLE base_lines ADD CONSTRAINT fk_base_lines_user FOREIGN KEY (user_id) REFERENCES users(id);
195+
ALTER TABLE base_nodes ADD CONSTRAINT fk_base_nodes_base_line FOREIGN KEY (base_line_id) REFERENCES base_lines(id);
196+
ALTER TABLE base_nodes ADD CONSTRAINT fk_base_nodes_parent FOREIGN KEY (parent_node_id) REFERENCES base_nodes(id);
197+
ALTER TABLE base_nodes ADD CONSTRAINT fk_base_nodes_user FOREIGN KEY (user_id) REFERENCES users(id);
198+
ALTER TABLE comment_likes ADD CONSTRAINT fk_comment_likes_comment FOREIGN KEY (comment_id) REFERENCES comments(id);
199+
ALTER TABLE comment_likes ADD CONSTRAINT fk_comment_likes_user FOREIGN KEY (user_id) REFERENCES users(id);
200+
ALTER TABLE comments ADD CONSTRAINT fk_comments_post FOREIGN KEY (post_id) REFERENCES post(id);
201+
ALTER TABLE comments ADD CONSTRAINT fk_comments_user FOREIGN KEY (user_id) REFERENCES users(id);
202+
ALTER TABLE decision_lines ADD CONSTRAINT fk_decision_lines_base_line FOREIGN KEY (base_line_id) REFERENCES base_lines(id);
203+
ALTER TABLE decision_lines ADD CONSTRAINT fk_decision_lines_user FOREIGN KEY (user_id) REFERENCES users(id);
204+
ALTER TABLE decision_nodes ADD CONSTRAINT fk_decision_nodes_base_node FOREIGN KEY (base_node_id) REFERENCES base_nodes(id);
205+
ALTER TABLE decision_nodes ADD CONSTRAINT fk_decision_nodes_dec_line FOREIGN KEY (dec_line_id) REFERENCES decision_lines(id);
206+
ALTER TABLE decision_nodes ADD CONSTRAINT fk_decision_nodes_parent FOREIGN KEY (parent_node_id) REFERENCES decision_nodes(id);
207+
ALTER TABLE decision_nodes ADD CONSTRAINT fk_decision_nodes_user FOREIGN KEY (user_id) REFERENCES users(id);
208+
ALTER TABLE poll_votes ADD CONSTRAINT fk_poll_votes_post FOREIGN KEY (post_id) REFERENCES post(id);
209+
ALTER TABLE poll_votes ADD CONSTRAINT fk_poll_votes_user FOREIGN KEY (user_id) REFERENCES users(id);
210+
ALTER TABLE post ADD CONSTRAINT fk_post_scenario FOREIGN KEY (scenario_id) REFERENCES scenarios(id);
211+
ALTER TABLE post ADD CONSTRAINT fk_post_user FOREIGN KEY (user_id) REFERENCES users(id);
212+
ALTER TABLE post_likes ADD CONSTRAINT fk_post_likes_post FOREIGN KEY (post_id) REFERENCES post(id);
213+
ALTER TABLE post_likes ADD CONSTRAINT fk_post_likes_user FOREIGN KEY (user_id) REFERENCES users(id);
214+
ALTER TABLE scenarios ADD CONSTRAINT fk_scenarios_base_line FOREIGN KEY (base_line_id) REFERENCES base_lines(id);
215+
ALTER TABLE scenarios ADD CONSTRAINT fk_scenarios_decision_line FOREIGN KEY (decision_line_id) REFERENCES decision_lines(id);
216+
ALTER TABLE scenarios ADD CONSTRAINT fk_scenarios_user FOREIGN KEY (user_id) REFERENCES users(id);
217+
ALTER TABLE scene_compare ADD CONSTRAINT fk_scene_compare_scenario FOREIGN KEY (scenario_id) REFERENCES scenarios(id);
218+
ALTER TABLE scene_type ADD CONSTRAINT fk_scene_type_scenario FOREIGN KEY (scenarios_id) REFERENCES scenarios(id);

infra/README.md renamed to infra/aws/README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@ AWS 기반 인프라 관리 및 배포를 위한 Terraform 코드와 관련 문
1313
## Structure
1414
### - Diagram
1515
```
16-
infra/
17-
├── main.tf # AWS 인프라 리소스 정의
18-
├── variables.tf # Terraform 변수
16+
infra/aws/terraform
17+
├── main.tf # Terraform 메인 설정 및 프로바이더
18+
├── variables.tf # Terraform 변수 정의
19+
├── network.tf # VPC, Subnet, IGW, Route Table 정의
20+
├── security_groups.tf # Security Groups 정의
21+
├── ec2.tf # EC2 Instance, IAM 정의
22+
├── rds.tf # RDS Database 정의
23+
├── s3.tf # S3 Bucket 정의
24+
├── cloudfront.tf # CloudFront CDN 정의
25+
├── outputs.tf # Terraform 출력 값 정의
1926
├── terraform.tfvars.default # Terraform 변수 값 (복사 후 terraform.tfvars로 사용)
2027
└── ec2_user_data.tpl # EC2 초기화 스크립트 템플릿
2128
```
@@ -180,13 +187,13 @@ graph TB
180187

181188
#### 3. 터미널
182189
```terraform
183-
# infra 디렉토리에서 진행
190+
# infra/aws/terraform 디렉토리에서 진행
184191
185-
# 초기화
192+
# 초기화
186193
terraform init
187194
188195
# 인프라 구성 검토
189-
# terraform plan
196+
# terraform plan
190197
191198
# 인프라 구성 적용
192199
# 'yes' 입력 시 구성 시작

0 commit comments

Comments
 (0)