-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
整理: 設定機能を API Router でモジュール化 (#1158)
* refactor: 設定 API Router へのモジュール化 * fix: lint * fix: `router` 名を明瞭化 * fix: グローバル変数 FIXME 追加 * fix: lint --------- Co-authored-by: Hiroshiba Kazuyuki <[email protected]>
- Loading branch information
Showing
2 changed files
with
74 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |