diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml deleted file mode 100644 index 27e0411..0000000 --- a/.github/workflows/test.yml +++ /dev/null @@ -1,34 +0,0 @@ -name: test - -on: - push: - branches: - - master - - develop - pull_request: - branches: - - master - - develop - -jobs: - build: - runs-on: ubuntu-latest - strategy: - matrix: - python-version: [3.9] - - steps: - - uses: actions/checkout@v2 - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v1 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - pip install -U pip - pip install pipenv - pipenv install - pipenv install --dev - - name: Run Test - run: | - pipenv run test diff --git a/Pipfile b/Pipfile index 6445051..d19b130 100644 --- a/Pipfile +++ b/Pipfile @@ -31,7 +31,7 @@ python_version = "3.9" [scripts] start = "python main.py --start" help = "python main.py --help" -test = "python -m unittest discover tests" +test = "python -m unittest discover -s tests" format = "autopep8 -ivr ." lint = "flake8 --show-source ." mypy = "mypy ." diff --git a/core/config.py b/app/config.py similarity index 100% rename from core/config.py rename to app/config.py diff --git a/app/handler/controller/dajare_controller.py b/app/handler/controller/dajare_controller.py new file mode 100644 index 0000000..5fbfd97 --- /dev/null +++ b/app/handler/controller/dajare_controller.py @@ -0,0 +1,38 @@ +from fastapi import APIRouter, Depends + +from app.handler.dto.eval_dto import EvalV1 +from app.handler.dto.judge_dto import JudgeV1 +from app.handler.dto.reading_dto import ReadingV1 +from app.service.dajare_service import DajareService + +dajare_service = DajareService() + +router = APIRouter() + + +@router.get('/eval/', status_code=200, response_model=EvalV1.Response, include_in_schema=False) +@router.get('/eval', status_code=200, response_model=EvalV1.Response) +async def eval_v1(request: EvalV1.Request = Depends()): + dajare = dajare_service.eval_dajare(request.dajare) + return EvalV1.Response( + score=dajare.score, + ) + + +@router.get('/judge/', status_code=200, response_model=JudgeV1.Response, include_in_schema=False) +@router.get('/judge', status_code=200, response_model=JudgeV1.Response) +async def judge_v1(request: JudgeV1.Request = Depends()): + dajare = dajare_service.judge_dajare(request.dajare) + return JudgeV1.Response( + is_dajare=dajare.is_dajare, + applied_rule=dajare.applied_rule, + ) + + +@router.get('/reading/', status_code=200, response_model=ReadingV1.Response, include_in_schema=False) +@router.get('/reading', status_code=200, response_model=ReadingV1.Response) +async def reading_v1(request: ReadingV1.Request = Depends()): + dajare = dajare_service.convert_reading(request.dajare) + return ReadingV1.Response( + reading=dajare.reading, + ) diff --git a/app/handler/controller/outer.py b/app/handler/controller/outer.py new file mode 100644 index 0000000..6b46525 --- /dev/null +++ b/app/handler/controller/outer.py @@ -0,0 +1,30 @@ +from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware + +from app.handler.controller import dajare_controller + + +def fastapi_app() -> FastAPI: + app = FastAPI() + + app.add_middleware( + CORSMiddleware, + allow_origins=['*'], + allow_credentials=True, + allow_methods=['*'], + allow_headers=['*'], + ) + + app.include_router(dajare_controller.router, prefix='/v1', tags=['dajare']) + app.include_router(dajare_controller.router, prefix='', tags=['dajare']) + + @app.get('/') + def index(): + return "health" + + app.title = 'DaaS API' + app.openapi_tags = [ + {"name": "dajare", "description": "Operation with dajare _(ダジャレ)_ ."} + ] + + return app diff --git a/app/handler/dto/eval_dto.py b/app/handler/dto/eval_dto.py new file mode 100644 index 0000000..708398e --- /dev/null +++ b/app/handler/dto/eval_dto.py @@ -0,0 +1,9 @@ +from pydantic import BaseModel + + +class EvalV1: + class Request(BaseModel): + dajare: str + + class Response(BaseModel): + score: float diff --git a/app/handler/dto/judge_dto.py b/app/handler/dto/judge_dto.py new file mode 100644 index 0000000..ca79549 --- /dev/null +++ b/app/handler/dto/judge_dto.py @@ -0,0 +1,10 @@ +from pydantic import BaseModel + + +class JudgeV1: + class Request(BaseModel): + dajare: str + + class Response(BaseModel): + is_dajare: bool + applied_rule: str diff --git a/app/handler/dto/reading_dto.py b/app/handler/dto/reading_dto.py new file mode 100644 index 0000000..3d5848e --- /dev/null +++ b/app/handler/dto/reading_dto.py @@ -0,0 +1,9 @@ +from pydantic import BaseModel + + +class ReadingV1: + class Request(BaseModel): + dajare: str + + class Response(BaseModel): + reading: str diff --git a/core/message.py b/app/message.py similarity index 100% rename from core/message.py rename to app/message.py diff --git a/core/model/dajare_model.py b/app/model/dajare_model.py similarity index 100% rename from core/model/dajare_model.py rename to app/model/dajare_model.py diff --git a/core/service/dajare_service.py b/app/service/dajare_service.py similarity index 81% rename from core/service/dajare_service.py rename to app/service/dajare_service.py index 0017d76..a271fc2 100644 --- a/core/service/dajare_service.py +++ b/app/service/dajare_service.py @@ -1,10 +1,10 @@ from functools import lru_cache -from core import config -from core.model.dajare_model import DajareModel -from core.service.engine.judge_engine import JudgeEngine -from core.service.engine.eval_engine import EvalEngine -from core.service.engine.reading_engine import ReadingEngine +from app import config +from app.model.dajare_model import DajareModel +from app.service.engine.judge_engine import JudgeEngine +from app.service.engine.eval_engine import EvalEngine +from app.service.engine.reading_engine import ReadingEngine class DajareService: diff --git a/core/service/engine/eval_engine.py b/app/service/engine/eval_engine.py similarity index 96% rename from core/service/engine/eval_engine.py rename to app/service/engine/eval_engine.py index 960ffd4..a01b4d5 100644 --- a/core/service/engine/eval_engine.py +++ b/app/service/engine/eval_engine.py @@ -1,8 +1,8 @@ import numpy as np import Levenshtein -from core import config -from core.util import text_util +from app import config +from app.util import text_util class EvalEngine: diff --git a/core/service/engine/judge_engine.py b/app/service/engine/judge_engine.py similarity index 99% rename from core/service/engine/judge_engine.py rename to app/service/engine/judge_engine.py index 2780ed6..382eacd 100644 --- a/core/service/engine/judge_engine.py +++ b/app/service/engine/judge_engine.py @@ -3,8 +3,8 @@ import pyboin import collections -from core import config -from core.util import text_util +from app import config +from app.util import text_util class JudgeEngine: diff --git a/core/service/engine/reading_engine.py b/app/service/engine/reading_engine.py similarity index 86% rename from core/service/engine/reading_engine.py rename to app/service/engine/reading_engine.py index 5f17b73..e67d2df 100644 --- a/core/service/engine/reading_engine.py +++ b/app/service/engine/reading_engine.py @@ -1,4 +1,4 @@ -from core.util import text_util +from app.util import text_util class ReadingEngine: diff --git a/core/util/text_util.py b/app/util/text_util.py similarity index 99% rename from core/util/text_util.py rename to app/util/text_util.py index dddf445..65b6977 100644 --- a/core/util/text_util.py +++ b/app/util/text_util.py @@ -7,8 +7,7 @@ import mojimoji from janome.tokenizer import Tokenizer -from core import config - +from app import config # morph analyzer tokenizer = Tokenizer() diff --git a/core/api/controller/__init__.py b/core/api/controller/__init__.py deleted file mode 100644 index 8a1bba6..0000000 --- a/core/api/controller/__init__.py +++ /dev/null @@ -1,40 +0,0 @@ -from fastapi import FastAPI -from fastapi.middleware.cors import CORSMiddleware - -from . import judge_controller -from . import eval_controller -from . import reading_controller - - -def create_app() -> FastAPI: - app = FastAPI() - - # middleware - app.add_middleware( - CORSMiddleware, - allow_origins=['*'], - allow_credentials=True, - allow_methods=['*'], - allow_headers=['*'], - ) - - # routing - app.include_router(judge_controller.router, prefix='/judge', tags=['dajare']) - app.include_router(eval_controller.router, prefix='/eval', tags=['dajare']) - app.include_router(reading_controller.router, prefix='/reading', tags=['dajare']) - - # index - @app.get('/') - def index(): - return 'Hello, World!' - - # OpenAPI - app.title = 'DaaS API' - app.description = 'This is a document of DaaS.' - app.version = '1.0.0' - - app.openapi_tags = [ - {"name": "dajare", "description": "Operation with dajare _(ダジャレ)_ ."} - ] - - return app diff --git a/core/api/controller/eval_controller.py b/core/api/controller/eval_controller.py deleted file mode 100644 index c76b541..0000000 --- a/core/api/controller/eval_controller.py +++ /dev/null @@ -1,23 +0,0 @@ -from fastapi import APIRouter, Depends, HTTPException - -from core.service.dajare_service import DajareService -from core.api.request.eval_request import EvalRequest -from core.api.response.eval_response import EvalResponse - -dajare_service = DajareService() - -router = APIRouter() - - -@router.get('/', status_code=200, response_model=EvalResponse, include_in_schema=False) -@router.get('', status_code=200, response_model=EvalResponse) -async def eval_dajare(request: EvalRequest = Depends()): - # eval dajare - try: - dajare = dajare_service.eval_dajare(request.dajare) - except Exception: - raise HTTPException(status_code=500) - - return EvalResponse( - score=dajare.score, - ) diff --git a/core/api/controller/judge_controller.py b/core/api/controller/judge_controller.py deleted file mode 100644 index 4006bf9..0000000 --- a/core/api/controller/judge_controller.py +++ /dev/null @@ -1,24 +0,0 @@ -from fastapi import APIRouter, Depends, HTTPException - -from core.service.dajare_service import DajareService -from core.api.request.judge_request import JudgeRequest -from core.api.response.judge_response import JudgeResponse - -dajare_service = DajareService() - -router = APIRouter() - - -@router.get('/', status_code=200, response_model=JudgeResponse, include_in_schema=False) -@router.get('', status_code=200, response_model=JudgeResponse) -async def judge_dajare(request: JudgeRequest = Depends()): - # judge dajare - try: - dajare = dajare_service.judge_dajare(request.dajare) - except Exception: - raise HTTPException(status_code=500) - - return JudgeResponse( - is_dajare=dajare.is_dajare, - applied_rule=dajare.applied_rule, - ) diff --git a/core/api/controller/reading_controller.py b/core/api/controller/reading_controller.py deleted file mode 100644 index 6e1f4a6..0000000 --- a/core/api/controller/reading_controller.py +++ /dev/null @@ -1,23 +0,0 @@ -from fastapi import APIRouter, Depends, HTTPException - -from core.service.dajare_service import DajareService -from core.api.request.reading_request import ReadingRequest -from core.api.response.reading_response import ReadingResponse - -dajare_service = DajareService() - -router = APIRouter() - - -@router.get('/', status_code=200, response_model=ReadingResponse, include_in_schema=False) -@router.get('', status_code=200, response_model=ReadingResponse) -async def reading_dajare(request: ReadingRequest = Depends()): - # convert reading - try: - dajare = dajare_service.convert_reading(request.dajare) - except Exception: - raise HTTPException(status_code=500) - - return ReadingResponse( - reading=dajare.reading, - ) diff --git a/core/api/request/eval_request.py b/core/api/request/eval_request.py deleted file mode 100644 index 1bf84fa..0000000 --- a/core/api/request/eval_request.py +++ /dev/null @@ -1,5 +0,0 @@ -from pydantic import BaseModel - - -class EvalRequest(BaseModel): - dajare: str diff --git a/core/api/request/judge_request.py b/core/api/request/judge_request.py deleted file mode 100644 index 68fb48e..0000000 --- a/core/api/request/judge_request.py +++ /dev/null @@ -1,5 +0,0 @@ -from pydantic import BaseModel - - -class JudgeRequest(BaseModel): - dajare: str diff --git a/core/api/request/reading_request.py b/core/api/request/reading_request.py deleted file mode 100644 index 85e5fba..0000000 --- a/core/api/request/reading_request.py +++ /dev/null @@ -1,5 +0,0 @@ -from pydantic import BaseModel - - -class ReadingRequest(BaseModel): - dajare: str diff --git a/core/api/response/eval_response.py b/core/api/response/eval_response.py deleted file mode 100644 index e494a65..0000000 --- a/core/api/response/eval_response.py +++ /dev/null @@ -1,5 +0,0 @@ -from pydantic import BaseModel - - -class EvalResponse(BaseModel): - score: float diff --git a/core/api/response/judge_response.py b/core/api/response/judge_response.py deleted file mode 100644 index 3dc1f28..0000000 --- a/core/api/response/judge_response.py +++ /dev/null @@ -1,6 +0,0 @@ -from pydantic import BaseModel - - -class JudgeResponse(BaseModel): - is_dajare: bool - applied_rule: str diff --git a/core/api/response/reading_response.py b/core/api/response/reading_response.py deleted file mode 100644 index 9add243..0000000 --- a/core/api/response/reading_response.py +++ /dev/null @@ -1,5 +0,0 @@ -from pydantic import BaseModel - - -class ReadingResponse(BaseModel): - reading: str diff --git a/core/service/engine/__init__.py b/core/service/engine/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/core/util/__init__.py b/core/util/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/main.py b/main.py index 086011f..b064d6f 100755 --- a/main.py +++ b/main.py @@ -7,13 +7,12 @@ import random import argparse -from core import config -from core import message -from core.api.controller import create_app +from app import config, message +from app.handler.controller.outer import fastapi_app def start_mode(): - app = create_app() + app = fastapi_app() uvicorn.run(app, host=config.API_HOST, port=config.API_PORT) @@ -33,7 +32,7 @@ def accuracy_mode(): data = random.sample(data, n_samples) # launch API - app = TestClient(create_app()) + app = TestClient(fastapi_app()) # measure accuracy error_samples: list[dict] = [] diff --git a/core/__init__.py b/tests/__init__.py similarity index 100% rename from core/__init__.py rename to tests/__init__.py diff --git a/core/api/request/__init__.py b/tests/handler/__init__.py similarity index 100% rename from core/api/request/__init__.py rename to tests/handler/__init__.py diff --git a/core/api/response/__init__.py b/tests/handler/controller/__init__.py similarity index 100% rename from core/api/response/__init__.py rename to tests/handler/controller/__init__.py diff --git a/tests/test_api.py b/tests/handler/controller/test_api.py similarity index 80% rename from tests/test_api.py rename to tests/handler/controller/test_api.py index 3c9258f..61d3bf7 100644 --- a/tests/test_api.py +++ b/tests/handler/controller/test_api.py @@ -1,10 +1,10 @@ import unittest from fastapi.testclient import TestClient -from core.api.controller import create_app -from core.api.request.judge_request import JudgeRequest -from core.api.request.eval_request import EvalRequest -from core.api.request.reading_request import ReadingRequest +from app.handler.controller.outer import fastapi_app +from app.handler.dto.eval_dto import EvalV1 +from app.handler.dto.judge_dto import JudgeV1 +from app.handler.dto.reading_dto import ReadingV1 class TestAPI(unittest.TestCase): @@ -15,11 +15,11 @@ class TestAPI(unittest.TestCase): SAMPLE_STR: str = '布団が吹っ飛んだ' def setUp(self): - self.app = TestClient(create_app()) + self.app = TestClient(fastapi_app()) def test_正_ダジャレを判定(self): # setup - request_body = JudgeRequest(dajare=self.SAMPLE_STR) + request_body = JudgeV1.Request(dajare=self.SAMPLE_STR) # test res = self.app.get(self.DAJARE_JUDGE_PATH, params=request_body) @@ -38,7 +38,7 @@ def test_異_判定APIのパラメータが不足(self): def test_正_ダジャレを評価(self): # setup - request_body = EvalRequest(dajare=self.SAMPLE_STR) + request_body = EvalV1.Request(dajare=self.SAMPLE_STR) # test res = self.app.get(self.DAJARE_EVAL_PATH, params=request_body) @@ -57,7 +57,7 @@ def test_異_評価APIのパラメータが不足(self): def test_正_ダジャレを読みに変換(self): # setup - request_body = ReadingRequest(dajare=self.SAMPLE_STR) + request_body = ReadingV1.Request(dajare=self.SAMPLE_STR) # test res = self.app.get(self.DAJARE_READING_PATH, params=request_body) diff --git a/core/model/__init__.py b/tests/service/__init__.py similarity index 100% rename from core/model/__init__.py rename to tests/service/__init__.py diff --git a/tests/test_dajare_service.py b/tests/service/test_dajare_service.py similarity index 98% rename from tests/test_dajare_service.py rename to tests/service/test_dajare_service.py index 0a355a6..946da4e 100644 --- a/tests/test_dajare_service.py +++ b/tests/service/test_dajare_service.py @@ -1,7 +1,7 @@ import unittest from parameterized import parameterized -from core.service.dajare_service import DajareService +from app.service.dajare_service import DajareService class TestDajareService(unittest.TestCase): diff --git a/core/service/__init__.py b/tests/util/__init__.py similarity index 100% rename from core/service/__init__.py rename to tests/util/__init__.py diff --git a/tests/test_text_util.py b/tests/util/test_text_util.py similarity index 98% rename from tests/test_text_util.py rename to tests/util/test_text_util.py index 8e08b86..ac77ff2 100644 --- a/tests/test_text_util.py +++ b/tests/util/test_text_util.py @@ -1,8 +1,8 @@ import unittest from parameterized import parameterized -from core import config -from core.util import text_util +from app import config +from app.util import text_util class TestTextUtil(unittest.TestCase):