From 113626cc5d027e0666ff00ec3909852877bf74e6 Mon Sep 17 00:00:00 2001 From: jwkim Date: Fri, 19 Dec 2025 15:32:05 +0900 Subject: [PATCH] =?UTF-8?q?edit=20:=20api=20key=20=ED=8C=8C=EC=9D=BC?= =?UTF-8?q?=EB=A1=9C=20=EA=B4=80=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- config.py | 6 ++++++ const.py | 6 +++++- custom_apps/gemini/main.py | 10 +++++++--- main_rest/app/main.py | 5 ++++- requirements_main.txt | 1 + utils/api_key_manager.py | 33 +++++++++++++++++++++++++++++++++ 7 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 utils/api_key_manager.py diff --git a/.gitignore b/.gitignore index cbbfe00..5e6dbde 100644 --- a/.gitignore +++ b/.gitignore @@ -147,4 +147,5 @@ log *temp datas/* -*result \ No newline at end of file +*result +*.env \ No newline at end of file diff --git a/config.py b/config.py index 79d7ff9..393d620 100644 --- a/config.py +++ b/config.py @@ -15,6 +15,8 @@ class Config: self.sftp_port = 22 self.sftp_id = "fermat" self.sftp_pw = "1234" + + self.api_path = "./gemini.env" def set_dev(self): """ @@ -28,6 +30,8 @@ class Config: self.sftp_pw = "fermat3514" self.db_pw = "1234" + self.api_path = "./gemini.env" + if not os.path.exists(self.remote_folder): os.makedirs(self.remote_folder) @@ -37,6 +41,8 @@ class Config: self.db_pw = "Fermat3514!" + self.api_path = "../gemini.env" + if not os.path.exists(self.local_folder): os.makedirs(self.local_folder) diff --git a/const.py b/const.py index 1dc79a6..599aad7 100644 --- a/const.py +++ b/const.py @@ -1,3 +1,7 @@ +from utils.api_key_manager import ApiKeyManager + TEMP_FOLDER = "./temp" -ILLEGAL_FILE_NAME = ['<', '>', ':', '"', '/', '\ ', '|', '?', '*'] \ No newline at end of file +ILLEGAL_FILE_NAME = ['<', '>', ':', '"', '/', '\ ', '|', '?', '*'] + +API_KEY_MANAGER = ApiKeyManager() \ No newline at end of file diff --git a/custom_apps/gemini/main.py b/custom_apps/gemini/main.py index d6e7829..d6484ff 100644 --- a/custom_apps/gemini/main.py +++ b/custom_apps/gemini/main.py @@ -7,14 +7,18 @@ from PIL import Image from io import BytesIO from main_rest.app.utils.date_utils import D -from const import TEMP_FOLDER - +from const import TEMP_FOLDER, API_KEY_MANAGER def gemini_image(prompt, folder=None): from custom_logger.main_log import main_logger as LOG image_path = '' - client = genai.Client(api_key="AIzaSyCSw4pcPDYdAnjzBB7J9ZKXtRJJvunjWtA") # a2tec key + api_key = API_KEY_MANAGER.get_api_key() + + if api_key is None: + raise Exception("API 키 세팅 필요! - 서버를 다시 구동하거나, API키 파일을 확인") + + client = genai.Client(api_key=API_KEY_MANAGER.get_api_key()) # a2tec key for i in range(3): response = client.models.generate_content( diff --git a/main_rest/app/main.py b/main_rest/app/main.py index 4297637..124bdc2 100644 --- a/main_rest/app/main.py +++ b/main_rest/app/main.py @@ -34,7 +34,6 @@ from custom_logger.main_log import main_logger as LOG API_KEY_HEADER = APIKeyHeader(name='Authorization', auto_error=False) - @asynccontextmanager async def lifespan(app: FastAPI): # When service starts. @@ -42,6 +41,10 @@ async def lifespan(app: FastAPI): import os import const + from const import API_KEY_MANAGER + + API_KEY_MANAGER.set_api_key() + if os.path.exists(const.TEMP_FOLDER): for _file in os.scandir(const.TEMP_FOLDER): os.remove(_file) diff --git a/requirements_main.txt b/requirements_main.txt index 7300b85..1fcd080 100644 --- a/requirements_main.txt +++ b/requirements_main.txt @@ -13,6 +13,7 @@ pycryptodomex pycryptodome email-validator requests +python-dotenv #imagen google-cloud-aiplatform diff --git a/utils/api_key_manager.py b/utils/api_key_manager.py new file mode 100644 index 0000000..33b1d22 --- /dev/null +++ b/utils/api_key_manager.py @@ -0,0 +1,33 @@ +import os +from dotenv import load_dotenv + +from config import rest_config +from custom_logger.main_log import main_logger as LOG + + +class ApiKeyManager: + + def __init__(self): + self.api_key = None + + def set_api_key(self, env_path=rest_config.api_path): + + if os.path.exists(env_path): + + load_dotenv(dotenv_path=env_path) + key = os.getenv("GEMINI_API_KEY") + + if key is None: + LOG.error(f"api key 파일에 GEMINI_API_KEY라는 변수가 없습니다") + elif key == "": + LOG.error(f"api key 파일에 내부 변수 GEMINI_API_KEY 값이 빈값입니다 값을 설정해주세요") + else: + self.api_key = key + + else: + LOG.error(f"api key 파일이 없습니다 : {os.path.abspath(env_path)}") + + def get_api_key(self): + if self.api_key is not None: + return self.api_key + \ No newline at end of file