{"id":3261,"date":"2024-03-26T21:33:26","date_gmt":"2024-03-26T12:33:26","guid":{"rendered":"https:\/\/nobunobu1717.site\/?p=3261"},"modified":"2024-03-26T22:42:58","modified_gmt":"2024-03-26T13:42:58","slug":"fastapi%e3%81%a7-env%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92%e4%bd%bf%e7%94%a8%e3%81%97%e3%81%a6%e8%a8%ad%e5%ae%9a%e3%82%92%e8%aa%ad%e3%81%bf%e8%be%bc%e3%82%80%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/nobunobu1717.site\/?p=3261","title":{"rendered":"FastAPI\u3067.env\u30d5\u30a1\u30a4\u30eb\u3092\u4f7f\u7528\u3057\u3066\u8a2d\u5b9a\u3092\u8aad\u307f\u8fbc\u3080\u65b9\u6cd5"},"content":{"rendered":"<h2>\u6982\u8981<\/h2>\n<p>\u6700\u8fd1\u5b66\u3073\u51fa\u3057\u305fFastAPI\u3067\u3001.env\u30d5\u30a1\u30a4\u30eb\u3092\u4f7f\u3063\u305f\u8a2d\u5b9a\u306e\u8aad\u307f\u8fbc\u3080\u65b9\u6cd5\u306e\u30e1\u30e2<\/p>\n<h2><span style=\"color: #1a1a1a;\"><span style=\"caret-color: #1a1a1a; font-size: 20px; background-color: #f3f3f3;\"><b>\u8aac\u660e<\/b><\/span><\/span><\/h2>\n<h3>FastAPI\u306b\u304a\u3051\u308b\u8a2d\u5b9a<\/h3>\n<p>FastAPI\u3067\u306fpydantic\u304c\u63d0\u4f9b\u3059\u308b\u578b\u30b7\u30b9\u30c6\u30e0\u3084\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u6a5f\u80fd\u304c\u9b45\u529b\u306e\u4e00\u3064\u3002BaseModel\u3092\u7d99\u627f\u3057\u305f\u30e2\u30c7\u30eb\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u304a\u624b\u8efd\u306b\u3053\u308c\u3089\u306e\u6a5f\u80fd\u304c\u4f7f\u3048\u308b\u3002<\/p>\n<p>\u3053\u306e\u6a5f\u80fd\u3092\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u8a2d\u5b9a\u3068\u3057\u3066\u4f7f\u7528\u3067\u304d\u308b\u3001pydantic-settings\u3068\u3044\u3063\u305f\u62e1\u5f35\u6a5f\u80fd\u304c\u5b58\u5728\u3059\u308b\u3002(pip\u306a\u3069\u3067\u8ffd\u52a0\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u5fc5\u8981\u3042\u308a\u3002)<\/p>\n<pre class=\"lang:default decode:true\">pip install pydantic-settings<\/pre>\n<p>&nbsp;<\/p>\n<h3>BaseSetting\u30af\u30e9\u30b9<\/h3>\n<p>BaseSettings\u30af\u30e9\u30b9\u3092\u7d99\u627f\u3059\u308b\u3053\u3068\u3067\u3001BaseModel\u3068\u540c\u69d8\u306a\u6a5f\u80fd\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n<pre class=\"lang:python decode:true\">from fastapi import FastAPI\r\nfrom pydantic_settings import BaseSettings\r\n\r\n\r\nclass Settings(BaseSettings):\r\n    app_name: str = \"Awesome API\"\r\n    admin_email: str\r\n    items_per_user: int = 50<\/pre>\n<p>&nbsp;<\/p>\n<p>\u3053\u306e\u307e\u307e\u3060\u3068\u901a\u5e38\u306eBaseModel\u3068\u5927\u304d\u3044\u9055\u3044\u306f\u306a\u3044\u304c\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306amodel_config\u3092\u5b9a\u7fa9\u3059\u308b\u3053\u3068\u3067\u3001env\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u8a2d\u5b9a\u3092\u8aad\u307f\u8fbc\u3080\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n<p>\u8907\u6570\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u3001\u8aad\u307f\u8fbc\u3080\u30d5\u30a1\u30a4\u30eb\u306e\u512a\u5148\u5ea6\u304c\u6307\u5b9a\u3067\u304d\u308b\u306e\u3067\u3001\u958b\u767a\u74b0\u5883\u3084\u672c\u756a\u74b0\u5883\u306b\u5408\u308f\u305b\u3066\u8a2d\u5b9a\u3092\u30b9\u30a4\u30c3\u30c1\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3002<\/p>\n<p>pydantic1\u3067\u306finternal class\u306eConfig\u3092\u4f7f\u7528\u3057\u305f\u5b9f\u88c5\u65b9\u6cd5\u306b\u306a\u3063\u3066\u3044\u308b\u304c\u30012\u3067\u306f\u4e0b\u8a18\u304c\u63a8\u5968\u65b9\u6cd5\u3068\u306a\u308b\u3088\u3046\u3067\u3042\u308b\u3002(Config class\u306e\u65b9\u3092\u4f7f\u3046\u3068\u78ba\u304b\u8b66\u544a\u304c\u51fa\u305f\u6c17\u304c\u3059\u308b\u3002)<\/p>\n<pre class=\"lang:default decode:true\">from pydantic_settings import BaseSettings, SettingsConfigDict\r\n\r\n\r\nclass Settings(BaseSettings):\r\n    app_name: str = \"Awesome API\"\r\n    admin_email: str\r\n    items_per_user: int = 50\r\n\r\n    # .env\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u8a2d\u5b9a\u3092\u8aad\u307f\u8fbc\u3080\u3002\u8907\u6570\u6307\u5b9a\u53ef\u80fd\u3002.env.prod =&gt; .env\u306e\u512a\u5148\u5ea6\u3067\u8aad\u307f\u8fbc\u3080\r\n    model_config = SettingsConfigDict(env_file=[\".env\", \".env.prod\"])\r\n\r\n<\/pre>\n<p>\u5b9a\u7fa9\u3055\u308c\u305f\u30d5\u30a3\u30fc\u30eb\u30c9\u540d\u3068\u4e00\u81f4\u3059\u308b.env\u30d5\u30a1\u30a4\u30eb\u3092\u7528\u610f\u3059\u308b\u3068\u3001\u5024\u304c\u4e0a\u66f8\u304d\u3055\u308c\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true\" title=\".env\">APP_NAME = \"Awesome APP\"\r\nADMIN_EMAIL = \"hoge@hoge.com\"\r\nITEMS_PER_USER = 11<\/pre>\n<p>&nbsp;<\/p>\n<p>\u8aad\u307f\u8fbc\u307e\u308c\u305f\u8a2d\u5b9a\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u306fBaseModel\u3068\u540c\u69d8\u306b\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true \">from pydantic_settings import BaseSettings, SettingsConfigDict\r\n\r\n\r\nclass Settings(BaseSettings):\r\n    app_name: str = \"Awesome API\"\r\n    admin_email: str\r\n    items_per_user: int = 50\r\n\r\n    # .env\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u8a2d\u5b9a\u3092\u8aad\u307f\u8fbc\u3080\u3002\u8907\u6570\u6307\u5b9a\u53ef\u80fd\u3002.env.prod =&gt; .env\u306e\u512a\u5148\u5ea6\u3067\u8aad\u307f\u8fbc\u3080\r\n    model_config = SettingsConfigDict(env_file=[\".env\", \".env.prod\"])\r\n\r\n    # app_name\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3002\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u306e\u5024\u304c\u7a7a\u767d\u3001\u7a7a\u6587\u5b57\u306a\u3089\u30a8\u30e9\u30fc\u3068\u3059\u308b\r\n    @field_validator('app_name', mode='before')\r\n    def validate_app_name_value(cls, v) -&gt; str:\r\n        if not v.strip():\r\n            raise ValueError(\"app_name must not be empty\")\r\n        return v\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>\u306a\u304a\u3001\u4f7f\u3046\u969b\u306b\u306f@iru_cache\u3067\u30c7\u30b3\u30ec\u30fc\u30c8\u3057\u305f\u30e1\u30bd\u30c3\u30c9\u304b\u3089\u8a2d\u5b9a\u3092\u53d6\u308a\u51fa\u3059\u3053\u3068\u3067\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u3055\u308c\u3066\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u751f\u6210\u3054\u3068\u306b.env\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u307e\u306a\u304f\u306a\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true\">from functools import lru_cache\r\n\r\nfrom fastapi import Depends, FastAPI\r\nfrom typing_extensions import Annotated\r\n\r\nfrom . import config\r\n\r\napp = FastAPI()\r\n\r\n\r\n@lru_cache\r\ndef get_settings():\r\n    return config.Settings()<\/pre>\n<p>&nbsp;<\/p>\n<p>\u53c2\u7167\u60c5\u5831(FastAPI\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8)<\/p>\n<p><a href=\"https:\/\/fastapi.tiangolo.com\/advanced\/settings\/\" target=\"_blank\" rel=\"noopener\">https:\/\/fastapi.tiangolo.com\/advanced\/settings\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 \u6700\u8fd1\u5b66\u3073\u51fa\u3057\u305fFastAPI\u3067\u3001.env\u30d5\u30a1\u30a4\u30eb\u3092\u4f7f\u3063\u305f\u8a2d\u5b9a\u306e\u8aad\u307f\u8fbc\u3080\u65b9\u6cd5\u306e\u30e1\u30e2 \u8aac\u660e FastAPI\u306b\u304a\u3051\u308b\u8a2d\u5b9a FastAPI\u3067\u306fpydantic\u304c\u63d0\u4f9b\u3059\u308b\u578b\u30b7\u30b9\u30c6\u30e0\u3084\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u6a5f\u80fd\u304c\u9b45 &#8230; <\/p>\n","protected":false},"author":1,"featured_media":3263,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[55,34],"tags":[],"_links":{"self":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts\/3261"}],"collection":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3261"}],"version-history":[{"count":4,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts\/3261\/revisions"}],"predecessor-version":[{"id":3266,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts\/3261\/revisions\/3266"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/media\/3263"}],"wp:attachment":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}