Skip to content

Commit

Permalink
整理: 設定機能を API Router でモジュール化 (#1158)
Browse files Browse the repository at this point in the history
* refactor: 設定 API Router へのモジュール化

* fix: lint

* fix: `router` 名を明瞭化

* fix: グローバル変数 FIXME 追加

* fix: lint

---------

Co-authored-by: Hiroshiba Kazuyuki <[email protected]>
  • Loading branch information
tarepan and Hiroshiba authored Apr 14, 2024
1 parent 425e214 commit 6943c8a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 47 deletions.
53 changes: 6 additions & 47 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import soundfile
import uvicorn
from fastapi import Depends, FastAPI, Form, HTTPException
from fastapi import Depends, FastAPI, HTTPException
from fastapi import Path as FAPath
from fastapi import Query, Request, Response
from fastapi.middleware.cors import CORSMiddleware
Expand All @@ -34,7 +34,7 @@
check_disabled_mutable_api,
deprecated_mutable_api,
)
from voicevox_engine.app.routers import preset, user_dict
from voicevox_engine.app.routers import preset, setting, user_dict
from voicevox_engine.cancellable_engine import CancellableEngine
from voicevox_engine.core.core_adapter import CoreAdapter
from voicevox_engine.core.core_initializer import initialize_cores
Expand Down Expand Up @@ -74,7 +74,7 @@
)
from voicevox_engine.preset.PresetError import PresetError
from voicevox_engine.preset.PresetManager import PresetManager
from voicevox_engine.setting.Setting import CorsPolicyMode, Setting
from voicevox_engine.setting.Setting import CorsPolicyMode
from voicevox_engine.setting.SettingLoader import USER_SETTING_PATH, SettingHandler
from voicevox_engine.tts_pipeline.kana_converter import create_kana, parse_kana
from voicevox_engine.tts_pipeline.tts_engine import (
Expand Down Expand Up @@ -1046,52 +1046,11 @@ async def validate_kana(
detail=ParseKanaBadRequest(err).dict(),
)

@app.get("/setting", response_class=Response, tags=["設定"])
def setting_get(request: Request) -> Response:
"""
設定ページを返します。
"""
settings = setting_loader.load()

brand_name = engine_manifest_data.brand_name
cors_policy_mode = settings.cors_policy_mode
allow_origin = settings.allow_origin

if allow_origin is None:
allow_origin = ""

return setting_ui_template.TemplateResponse(
"ui.html",
{
"request": request,
"brand_name": brand_name,
"cors_policy_mode": cors_policy_mode.value,
"allow_origin": allow_origin,
},
app.include_router(
setting.generate_router(
setting_loader, engine_manifest_data, setting_ui_template
)

@app.post(
"/setting",
response_class=Response,
tags=["設定"],
dependencies=[Depends(check_disabled_mutable_api)],
)
def setting_post(
cors_policy_mode: CorsPolicyMode = Form(), # noqa
allow_origin: str | None = Form(default=None), # noqa
) -> Response:
"""
設定を更新します。
"""
settings = Setting(
cors_policy_mode=cors_policy_mode,
allow_origin=allow_origin,
)

# 更新した設定へ上書き
setting_loader.save(settings)

return Response(status_code=204)

# BaseLibraryInfo/VvlibManifestモデルはAPIとして表には出ないが、エディタ側で利用したいので、手動で追加する
# ref: https://fastapi.tiangolo.com/advanced/extending-openapi/#modify-the-openapi-schema
Expand Down
68 changes: 68 additions & 0 deletions voicevox_engine/app/routers/setting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""設定機能を提供する API Router"""

from fastapi import APIRouter, Depends, Form, Request, Response
from fastapi.templating import Jinja2Templates

from voicevox_engine.engine_manifest.EngineManifest import EngineManifest
from voicevox_engine.setting.Setting import CorsPolicyMode, Setting
from voicevox_engine.setting.SettingLoader import SettingHandler

from ..dependencies import check_disabled_mutable_api


def generate_router(
setting_loader: SettingHandler,
engine_manifest_data: EngineManifest,
setting_ui_template: Jinja2Templates,
) -> APIRouter:
"""設定 API Router を生成する"""
router = APIRouter()

@router.get("/setting", response_class=Response, tags=["設定"])
def setting_get(request: Request) -> Response:
"""
設定ページを返します。
"""
settings = setting_loader.load()

brand_name = engine_manifest_data.brand_name
cors_policy_mode = settings.cors_policy_mode
allow_origin = settings.allow_origin

if allow_origin is None:
allow_origin = ""

return setting_ui_template.TemplateResponse(
"ui.html",
{
"request": request,
"brand_name": brand_name,
"cors_policy_mode": cors_policy_mode.value,
"allow_origin": allow_origin,
},
)

@router.post(
"/setting",
response_class=Response,
tags=["設定"],
dependencies=[Depends(check_disabled_mutable_api)],
)
def setting_post(
cors_policy_mode: CorsPolicyMode = Form(), # noqa
allow_origin: str | None = Form(default=None), # noqa
) -> Response:
"""
設定を更新します。
"""
settings = Setting(
cors_policy_mode=cors_policy_mode,
allow_origin=allow_origin,
)

# 更新した設定へ上書き
setting_loader.save(settings)

return Response(status_code=204)

return router

0 comments on commit 6943c8a

Please sign in to comment.