From ba5c11d61b61b389f40c50a97489295f0a7fd4f2 Mon Sep 17 00:00:00 2001 From: sosokker Date: Tue, 23 Apr 2024 23:36:44 +0700 Subject: [PATCH] Add database connection file and config file --- StreamServer/src/config.py | 24 ++++++++++++++++++++++++ StreamServer/src/database.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/StreamServer/src/config.py b/StreamServer/src/config.py index e69de29..673351c 100644 --- a/StreamServer/src/config.py +++ b/StreamServer/src/config.py @@ -0,0 +1,24 @@ +""" +This file is used to store the configuration of the project. + +Attributes: + DB_HOST: The host of the MySQL database + DB_USER: The username of the MySQL database + DB_PASSWD: The password of the MySQL database + DB_NAME: The name of the MySQL database + MINIO_ENDPOINT: The endpoint of the MinIO storage + MINIO_ACCESS_KEY: The access key of the MinIO storage + MINIO_SECRET_KEY: The secret key of the MinIO storage +""" + +from decouple import Config, Csv + +config = Config('.env') + +DB_HOST = config.get('DB_HOST') +DB_USER = config.get('DB_USER') +DB_PASSWD = config.get('DB_PASSWD') +DB_NAME = config.get('DB_NAME') +MINIO_ENDPOINT = config.get('MINIO_ENDPOINT') +MINIO_ACCESS_KEY = config.get('MINIO_ACCESS_KEY') +MINIO_SECRET_KEY = config.get('MINIO_SECRET_KEY') \ No newline at end of file diff --git a/StreamServer/src/database.py b/StreamServer/src/database.py index e69de29..20712da 100644 --- a/StreamServer/src/database.py +++ b/StreamServer/src/database.py @@ -0,0 +1,30 @@ +""" +Database Configuration and Connection Setup + +This file contains the configuration and setup for connecting to the database using SQLAlchemy and MinIO. + +Attributes: + engine: SQLAlchemy engine for database connection. + SessionLocal: SQLAlchemy session maker for database sessions. + Base: SQLAlchemy base class for declarative ORM models. + minio_client: MinIO client for interacting with MinIO storage. +""" + +from sqlalchemy import create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker + +from minio import Minio +from config import DB_HOST, DB_USER, DB_PASSWD, DB_NAME, MINIO_ENDPOINT, MINIO_ACCESS_KEY, MINIO_SECRET_KEY + +engine = create_engine(f"mysql+pymysql://{DB_USER}:{DB_PASSWD}@{DB_HOST}/{DB_NAME}") +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) +Base = declarative_base() + +USE_SSL = False +minio_client = Minio( + endpoint=MINIO_ENDPOINT, + access_key=MINIO_ACCESS_KEY, + secret_key=MINIO_SECRET_KEY, + secure=USE_SSL +) \ No newline at end of file