2025년 March 12일
  1. 개발 히스토리
  2. [Backend] MSP 프로젝트 구조 – CORE, Models

[Backend] MSP 프로젝트 구조 – CORE, Models

현재 구성된 core 디렉터리 구조

우선 기본적인 틀만 잡고 이후 더 추가할 예정입니다.

API KEY나 구글 이메일 SMTP 인증 관련 정보, DB 접속 URL 등 여러 코드에서 사용할만한 내용들을 core에 정리해서 다른 코드에서 import하는 방식으로 구조를 변경하는 중입니다.

새로운 API KEY나 비밀번호, DB URL을 사용하게 되더라도 코드를 바꾸는 것이 간편하도록 하기 위함입니다.

config

API KEY, SMTP 이메일 인증 관련 정보 등을 정의합니다.

import os

DATABASE_URL = os.getenv('DATABASE_URL', 'postgresql://postgres:3636@localhost:5433/msp_database')

SMTP_SERVER = 'YOUR EMAIL'
SMTP_PORT = 587
SENDER_EMAIL = 'YOUR EMAIL ADDRESS'
SENDER_PASSWORD = 'YOUR SENDER PASSWORD'

CLAUDE_API = "YOUR API KEY"
GPT_API = "YOUR API KEY"

주로 자주 사용하는 값들을 미리 정의합니다.

DB 접속의 경우 사용자의 OS 내에 미리 저장된 환경 변수가 있다면 그 값을 사용합니다.

import core.config as config

client = anthropic.Anthropic(
    api_key = config.CLAUDE_API
)

이런 방식으로 import해서 사용할 수 있습니다.

database

데이터베이스 설정 및 접속과 관련된 코드는 여러 번 호출해야 하기에 이 database.py에서 함수로 정의해두고 불러오는 것이 더욱 코드를 간결하게 합니다.

import core.config as config
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base

DATABASE_URL = config.DATABASE_URL

Base = declarative_base()
engine = create_engine(DATABASE_URL, echo=True)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

DB에 접속하기 위한 get DB 함수를 정의합니다.

MODELS

PROJECT

from sqlalchemy import Column, Integer, String, Date, Text, ForeignKey, JSON, TIMESTAMP
from core.database import Base
from sqlalchemy.orm import relationship
from sqlalchemy.dialects.postgresql import ARRAY

class User(Base):
    __tablename__ = 'user_table'

    email = Column(String, primary_key=True, index=True)
    pw = Column(String, nullable=False)
    role = Column(String, nullable=False)
    group = Column(Text, nullable=True)
    name = Column(Text, nullable=True)
    status = Column(Text, nullable=True)
    skills = Column(ARRAY(Text), nullable=True)
    register_at = Column(Date, nullable=True)

class Project(Base):
    __tablename__ = 'project'

    project_id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    project_name = Column(String, nullable=True)
    start_date = Column(Date, nullable=False)
    end_date = Column(Date, nullable=False)
    description = Column(Text, nullable=True)
    requirements = Column(String, nullable=True)
    model_setting = Column(String, nullable=False)
    num_of_member_ = Column(Integer, nullable=True)
    user_email = Column(String, ForeignKey('user_table.email'), nullable=True)

    # 관계 설정: 프로젝트는 특정 사용자를 참조할  있음
    user = relationship("User", back_populates="projects")
    table_data = relationship("TableData", back_populates="project")
    api_data = relationship("APITable", back_populates="project")

# User 모델에 Project와의 관계를 설정
User.projects = relationship("Project", back_populates="user")

class Requirements(Base):
    __tablename__ = 'requirements'

    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    project_id = Column(Integer, ForeignKey('project.project_id'), nullable=False)
    title = Column(String(255), nullable=True)
    description = Column(Text, nullable=True)
    definition = Column(Text, nullable=True)

    # 관계 설정: 요구 사항은 특정 프로젝트와 관련됨
    project = relationship("Project", back_populates="requirements")

# Project 모델에 Requirements와의 관계를 설정
Project.requirements = relationship("Requirements", back_populates="project")


class SystemSetting(Base):
    __tablename__ = 'systemSetting'

    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    project_id = Column(Integer, ForeignKey('project.project_id'), nullable=False)
    title = Column(String(255), nullable=True)
    description = Column(Text, nullable=True)
    definition = Column(Text, nullable=True)

    project = relationship("Project", back_populates="system_settings")

# Project 모델에 SystemSetting과의 관계 설정
Project.system_settings = relationship("SystemSetting", back_populates="project")

class TableData(Base):
    __tablename__ = 'tabledata'

    id = Column(Integer, primary_key=True, autoincrement=True)
    project_id = Column(Integer, ForeignKey('project.project_id'), nullable=False)
    table_name = Column(String(255), nullable=False)
    columns = Column(ARRAY(Text), nullable=True)
    description = Column(Text, nullable=True)

    # Project와의 관계 설정 ( 프로젝트는 여러 개의 TableData를 가질  있음)
    project = relationship("Project", back_populates="table_data")

class APITable(Base):
    __tablename__ = 'apitable'

    id = Column(Integer, primary_key=True, autoincrement=True)
    project_id = Column(Integer, ForeignKey('project.project_id'), nullable=False)
    api_name = Column(String(255), nullable=False)
    apidata = Column(JSON, nullable=True)  # JSON 필드
    description = Column(Text, nullable=True)

    # Project와의 관계 설정
    project = relationship("Project", back_populates="api_data")

회원에 대한 정보를 담는 User 테이블과, 해당 User가 보유한 프로젝트 목록을 담는 Project 테이블,

그리고 각 Project를 구성하는 테이블들은 모두 User : Project : 기타 테이블 관계로 연관이 있습니다.

이러한 관계에 해당하는 테이블의 SQL Alchemy 모델만 정리해둔 코드입니다.

API

from sqlalchemy import Column, Integer, String, Text, TIMESTAMP
from core.database import Base
from sqlalchemy.sql import func


class Provider(Base):
    __tablename__ = 'provider_table'

    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(100), nullable=False)
    status = Column(Text)
    website = Column(Text)
    description = Column(Text)

class ApiKey(Base):
    __tablename__ = 'api_keys'

    id = Column(Integer, primary_key=True, autoincrement=True)
    provider_id = Column(String(50), nullable=False)
    name = Column(String(100), nullable=False)
    key = Column(String(255), nullable=False, unique=True)
    created_at = Column(TIMESTAMP, nullable=False, default=func.now())
    expires_at = Column(TIMESTAMP, nullable=False)
    status = Column(String(20), nullable=False)
    environment = Column(String(50), nullable=False)
    usage_limit = Column(Integer, nullable=False)
    usage_count = Column(Integer, nullable=False, default=0)
    user_email = Column(String(255), nullable=False)

GPT나 Claude 등, API 등과 관련이 있는 모델들만 정의한 코드입니다.

Leave a Reply

Your email address will not be published. Required fields are marked *

연관 글
BCT NEWS
인기 글
워드프레스 보안
워드프레스 모음
워드프레스 유지보수