diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..67edf2f --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +.git +.gitignore +.dockerignore +Dockerfile \ No newline at end of file diff --git a/code_red_crawling/.gitignore b/.gitignore similarity index 100% rename from code_red_crawling/.gitignore rename to .gitignore diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..bc16bcc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +# 파이썬 베이스 이미지 사용 +FROM python:3.10.14-slim + +# 작업 디렉토리 설정 +WORKDIR /app + +# 필요한 파이썬 패키지 설치 +# requirements.txt 파일에 Flask와 기타 의존성이 명시되어 있다고 가정 +COPY requirements.txt . +RUN pip install -r requirements.txt + +# 현재 디렉토리의 모든 파일을 컨테이너의 작업 디렉토리로 복사 +COPY . /app + +# Flask 서버 실행 +# "--host=0.0.0.0" 옵션은 Docker 컨테이너 외부에서 Flask 앱에 접근할 수 있도록 함 +CMD ["flask", "run", "--host=0.0.0.0"] diff --git a/code_red_crawling/app.py b/app.py similarity index 94% rename from code_red_crawling/app.py rename to app.py index 3eed088..caf945b 100644 --- a/code_red_crawling/app.py +++ b/app.py @@ -5,7 +5,7 @@ from selenium.webdriver.chrome.service import Service as ChromeService from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.common.by import By -import time +from time import sleep import jellyfish import os import psycopg2 @@ -22,14 +22,23 @@ scheduler.init_app(app) def create_connection(): - conn = psycopg2.connect( + retry_count = 5 + for i in range(retry_count): + try: + conn = psycopg2.connect( host=os.getenv('DB_HOST'), dbname=os.getenv('DB_NAME'), user=os.getenv('DB_USERNAME'), password=os.getenv('DB_PASSWORD'), port=os.getenv('DB_PORT') ) - return conn + print("Database connection successful") + return conn + except psycopg2.OperationalError as e: + print(f"Connection failed: {e}") + print("Trying again in 5 seconds...") + sleep(5) + raise Exception("Failed to connect to database after several attempts") conn = create_connection() diff --git a/code_red_crawling/requirements.txt b/code_red_crawling/requirements.txt deleted file mode 100644 index 50e64bc..0000000 Binary files a/code_red_crawling/requirements.txt and /dev/null differ diff --git a/code_red_crawling/code_red_study.csv b/code_red_study.csv similarity index 100% rename from code_red_crawling/code_red_study.csv rename to code_red_study.csv diff --git a/code_red_crawling/docker-compose.yml b/docker-compose.yml similarity index 55% rename from code_red_crawling/docker-compose.yml rename to docker-compose.yml index 6efbe39..3d8ca1a 100644 --- a/code_red_crawling/docker-compose.yml +++ b/docker-compose.yml @@ -4,12 +4,10 @@ services: codered_postgresql: image: postgis/postgis restart: always - container_name: postgres + container_name: codered_postgresql ports: - "5432:5432" - # 환경 변수 설정 - environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres @@ -18,3 +16,17 @@ services: - ./data/postgres/:/var/lib/postgresql/data networks: - compose-networks + codered_crawling: + image: codered_crawling + restart: always + container_name: codered_crawling + ports: + - "5000:5000" + depends_on: + - codered_postgresql + networks: + - compose-networks + +networks: + compose-networks: + driver: bridge diff --git a/code_red_crawling/model.pkl b/model.pkl similarity index 100% rename from code_red_crawling/model.pkl rename to model.pkl diff --git a/code_red_crawling/news_study.py b/news_study.py similarity index 100% rename from code_red_crawling/news_study.py rename to news_study.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..488b0bf Binary files /dev/null and b/requirements.txt differ diff --git a/code_red_crawling/stop_words.txt b/stop_words.txt similarity index 100% rename from code_red_crawling/stop_words.txt rename to stop_words.txt