Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

파이썬 설정로드, IDE선택 #1

Open
choisungwook opened this issue Apr 26, 2022 · 5 comments
Open

파이썬 설정로드, IDE선택 #1

choisungwook opened this issue Apr 26, 2022 · 5 comments
Assignees

Comments

@choisungwook
Copy link
Member

No description provided.

@choisungwook
Copy link
Member Author

  1. 프레임워크는 무엇을 사용할까?
  • FastAPI

@choisungwook
Copy link
Member Author

choisungwook commented Apr 27, 2022

1. 설정값을 어떻게 불러올까?

  • 후보
    • pydantic패키지 BaseSettings모듈을 이용(환경변수)
    • yaml파일을 수동으로 로드

@choisungwook choisungwook self-assigned this Apr 27, 2022
@choisungwook
Copy link
Member Author

choisungwook commented Apr 27, 2022

설정값은 pydantic패키지 BaseSettings모듈을 이용

패키지 설치

pip install pydantic python-dotenv

env파일 생성

  • env파일은 설정 값을 관리
  • ENV_STATE는 런타임 모드
  • prefix에 런타임모드 설정(DEV, PROD)
ENV_STATE="dev" # or prod

DEV_REDIS_HOST="127.0.0.1"
DEV_REDIS_PORT="4000"

PROD_REDIS_HOST="127.0.0.2"
PROD_REDIS_PORT="5000"

설정값 로드

  • .env파일에서 env_state값으로 런타임모드 확인
class FactoryConfig:
      """Returns a config instance depending on the ENV_STATE variable."""
      def __init__(self, env_state: Optional[str]):
          self.env_state = env_state

      def __call__(self):
          if self.env_state == "dev":
              return DevConfig()

          elif self.env_state == "prod":
              return ProdConfig()

          elif self.env_state == "stage"
              return StageConfig()

설정은 어떻게?

  • 공통으로 사용하는 변수는 GlobalConfig에 설정
  • 각 런타임모드는 Globalconfig를 상속해서 구현하고, env_prefix를 사용해서 설정 값 로드
class GlobalConfig(BaseSettings):
    """Global configurations."""
    # define global variables with the Field class
    ENV_STATE: Optional[str] = Field(None, env="ENV_STATE")

    # environment specific variables do not need the Field class
    REDIS_HOST: Optional[str] = None
    REDIS_PORT: Optional[int] = None
    REDIS_PASS: Optional[str] = None

    class Config:
        """Loads the dotenv file."""
        env_file: str = ".env"


class DevConfig(GlobalConfig):
    """Development configurations."""
    class Config:
        env_prefix: str = "DEV_"


class ProdConfig(GlobalConfig):
    """Production configurations."""
    class Config:
        env_prefix: str = "PROD_"

참고자료

@choisungwook
Copy link
Member Author

choisungwook commented Apr 27, 2022

2. 어떤 에디터를 사용할까?

  • pycharm
  • vscode

@choisungwook
Copy link
Member Author

choisungwook commented Apr 27, 2022

3. 어떻게 pycharm에서 실행할까?

참고자료: https://stackoverflow.com/questions/62856818/how-can-i-run-the-fast-api-server-using-pycharm

  • 실행모드를 uvicorn으로
    image

choisungwook added a commit that referenced this issue Apr 27, 2022
@choisungwook choisungwook changed the title [백엔드] 파이썬 프로젝트 구조 설정 파이썬 설정로드, IDE선택 Apr 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

No branches or pull requests

1 participant