PKSelecter/

PKSelecter/config.py

application 구동 시 상황에 맞게 적절한 config (테스트 모드, 개발모드 등)을 설정하여 동작할 수 있도록 합니다.

"""
Application Config Setting
"""
import os

APP_NAME = "PKSelect"

class Config:
    """General Config"""
    SLOW_API_TIME = 0.5
    API_LOGGING = False
    JSON_AS_ASCII = False

    @staticmethod
    def init_app(app):
        pass


class TestingConfig(Config):
    DEBUG=False
    TESTING=True
    ENV="\n>>>>> THIS IS TESTING MODE <<<<<\n"


class DevelopmentConfig(Config):
    DEBUG=True
    TESTING=False


class ProductionConfig(Config):
    DEBUG = False
    TESTING = False


config_dict = {
    'development': DevelopmentConfig,
    'production': ProductionConfig,
    'testing': TestingConfig,
    'default': DevelopmentConfig,
}

config = config_dict[os.getenv("FLASK_CONFIG") or "default"]

if __name__ == "__main__":
    pass

PKSelecter/run_app.py

flask 웹 어플리케이션을 실행시키기 위한 메인 코드입니다. console을 이용해 flask 모드를 제어하여 개발을 진행할 수 있습니다.

import os
from os import path as environ
import unittest
import click
from flask.app import Flask
from flask.cli import FlaskGroup

from app import create_app
from config import config
from config import config_dict


application = create_app(config)


# production : debug = False, Testing = False,
# Develope : debug = True Testing = False, 
# Testing : ENV로 구분 후 테스트.py 실행
# Develope : debug = True, 개발
"""test_mode"""
@application.cli.command("test_mode")
@click.argument("test_names_tuple", nargs=-1)  # nargs -1 로 해야 문자열로 받음 아니면 문자로 받음
def test(test_names_tuple):

    application = create_app(config_dict['testing'])
    test_dir = "test"
    print(
         application.config["ENV"],
        "|- - - Config Check - - - -|\n"
        " |- - - DEBUG : ", application.config["DEBUG"], "- - -|"
        "\n |- - - TESTING : ", application.config["TESTING"], " - -|"
        "\n ┗ - - - - - - - - - - - - -┛\n"
    )  
    try:
        # tests에 없는 요소가 들어오면 에러처리
        if test_names_tuple:
            for index in range(len(test_names_tuple)):
                print(">>> mode: test_mode \n>>> test name : '{}'".format(test_names_tuple[index]))
                name_to_test_suite = unittest.TestLoader().discover(
                    test_dir, test_names_tuple[index]
                )
                unittest.TextTestRunner(verbosity=1).run(name_to_test_suite)
        else:
            raise ValueError

    
    except ValueError:
        print("Error, you must be 'flask test_mode a.py b.py ...' \n")
    

HOW TO FLASK RUN ?

각 모드는 개발모드, 운용모드, 테스트모드 세개로 이루어져 있으며 다음과 같은 명령어를 실행해야한다.

Application은 개발, 운용, 테스트 레벨에서 모두 다른 환경변수 값을 가지고 동작해야하기에, 실행하고자 하는 환경에 맞춰 config 값을 다르게 입력하는 Application을 구현하였다.

// win10 powershell 기준이며, 해당 cmd에 맞춰서 사용하시길 바랍니다.
$env:FLASK_APP="run_app.py" // flask App 객체 위치를 설정

$env:FLASK_CONFIG="development" or "production" // 어떤 config를 할지 설정
$env:FLASK_ENV="development" or "production" // 어떤 환경으로 실행할지 설정

flask run // 

1.개발모드 *( Debug : on )

2. 운용모드 *( Debug : off )

3. 테스트 모드

flask test_mode a.py b.py . . . 

위 사진은 flask test_mode 이후 테스트할 모듈 명을 정해주지 않았기 때문에 에러가 발생시키도록 하였다.

Last updated