{"id":204007,"date":"2025-05-29T11:00:58","date_gmt":"2025-05-29T03:00:58","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204007\/"},"modified":"2025-05-29T11:00:58","modified_gmt":"2025-05-29T03:00:58","slug":"%e4%bd%bf%e7%94%a8fastapi%e6%9e%84%e5%bb%ba%e5%ba%93%e5%ad%98%e7%b3%bb%e7%bb%9f%e7%9a%84%e7%9b%ae%e5%bd%95%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204007\/","title":{"rendered":"\u4f7f\u7528FastAPI\u6784\u5efa\u5e93\u5b58\u7cfb\u7edf\u7684\u76ee\u5f55\u7ed3\u6784"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4f7f\u7528FastAPI\u6784\u5efa\u5e93\u5b58\u7cfb\u7edf\u7684\u76ee\u5f55\u7ed3\u6784<\/h1>\n<p>\u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300a\u4f7f\u7528FastAPI\u6784\u5efa\u5e93\u5b58\u7cfb\u7edf\u7684\u76ee\u5f55\u7ed3\u6784\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241017\/17291502326710bd18939c1.jpg\" class=\"aligncenter\"><\/p>\n<p>\u8fd9\u79cd\u7ed3\u6784\u5206\u79bb\u4e86\u5173\u6ce8\u70b9\uff0c\u4f7f\u5f97\u968f\u7740\u9879\u76ee\u89c4\u6a21\u7684\u6269\u5927\u800c\u66f4\u5bb9\u6613\u7ba1\u7406\u3002<\/p>\n<pre>stock-system\/\n\u2502\n\u251c\u2500\u2500 app\/\n\u2502   \u251c\u2500\u2500 __init__.py\n\u2502   \u251c\u2500\u2500 main.py                     # Entry point of the FastAPI app\n\u2502   \u251c\u2500\u2500 api\/                        # API related code (routers)\n\u2502   \u2502   \u251c\u2500\u2500 __init__.py\n\u2502   \u2502   \u251c\u2500\u2500 products.py              # Routes related to products\n\u2502   \u2502   \u251c\u2500\u2500 inventory.py             # Routes related to inventory management\n\u2502   \u2502   \u251c\u2500\u2500 sales.py                 # Routes related to sales and orders\n\u2502   \u2502   \u2514\u2500\u2500 users.py                 # Routes related to user management\n\u2502   \u2502\n\u2502   \u251c\u2500\u2500 core\/                       # Core settings and configurations\n\u2502   \u2502   \u251c\u2500\u2500 __init__.py\n\u2502   \u2502   \u251c\u2500\u2500 config.py                # Configuration settings (DB, API keys, etc.)\n\u2502   \u2502   \u2514\u2500\u2500 security.py              # Authentication, Authorization, and Security utilities\n\u2502   \u2502\n\u2502   \u251c\u2500\u2500 crud\/                       # CRUD operations for database interactions\n\u2502   \u2502   \u251c\u2500\u2500 __init__.py\n\u2502   \u2502   \u251c\u2500\u2500 crud_product.py          # CRUD operations related to products\n\u2502   \u2502   \u251c\u2500\u2500 crud_inventory.py        # CRUD operations related to inventory\n\u2502   \u2502   \u251c\u2500\u2500 crud_sales.py            # CRUD operations related to sales\n\u2502   \u2502   \u2514\u2500\u2500 crud_user.py             # CRUD operations related to users\n\u2502   \u2502\n\u2502   \u251c\u2500\u2500 db\/                         # Database-related modules\n\u2502   \u2502   \u251c\u2500\u2500 __init__.py\n\u2502   \u2502   \u251c\u2500\u2500 base.py                  # SQLAlchemy base for models\n\u2502   \u2502   \u251c\u2500\u2500 session.py               # Database session creation\n\u2502   \u2502   \u2514\u2500\u2500 models\/                  # SQLAlchemy models\n\u2502   \u2502       \u251c\u2500\u2500 __init__.py\n\u2502   \u2502       \u251c\u2500\u2500 product.py           # Product model\n\u2502   \u2502       \u251c\u2500\u2500 inventory.py         # Inventory model\n\u2502   \u2502       \u251c\u2500\u2500 sales.py             # Sales\/Orders model\n\u2502   \u2502       \u2514\u2500\u2500 user.py              # User model\n\u2502   \u2502\n\u2502   \u251c\u2500\u2500 schemas\/                    # Pydantic schemas for request\/response models\n\u2502   \u2502   \u251c\u2500\u2500 __init__.py\n\u2502   \u2502   \u251c\u2500\u2500 product.py               # Product-related schemas\n\u2502   \u2502   \u251c\u2500\u2500 inventory.py             # Inventory-related schemas\n\u2502   \u2502   \u251c\u2500\u2500 sales.py                 # Sales-related schemas\n\u2502   \u2502   \u2514\u2500\u2500 user.py                  # User-related schemas\n\u2502   \u2502\n\u2502   \u251c\u2500\u2500 services\/                   # Additional business logic\/services\n\u2502   \u2502   \u251c\u2500\u2500 __init__.py\n\u2502   \u2502   \u251c\u2500\u2500 product_service.py       # Logic related to products\n\u2502   \u2502   \u251c\u2500\u2500 inventory_service.py     # Logic related to inventory\n\u2502   \u2502   \u251c\u2500\u2500 sales_service.py         # Logic related to sales\n\u2502   \u2502   \u2514\u2500\u2500 user_service.py          # Logic related to users\n\u2502   \u2502\n\u2502   \u2514\u2500\u2500 utils\/                      # Utility functions\/helpers\n\u2502       \u251c\u2500\u2500 __init__.py\n\u2502       \u251c\u2500\u2500 dependencies.py          # Common dependencies for routes\n\u2502       \u2514\u2500\u2500 helpers.py               # Miscellaneous helper functions\n\u2502\n\u251c\u2500\u2500 tests\/                          # Test cases\n\u2502   \u251c\u2500\u2500 __init__.py\n\u2502   \u251c\u2500\u2500 test_products.py             # Tests related to products\n\u2502   \u251c\u2500\u2500 test_inventory.py            # Tests related to inventory\n\u2502   \u251c\u2500\u2500 test_sales.py                # Tests related to sales\/orders\n\u2502   \u2514\u2500\u2500 test_users.py                # Tests related to users\n\u2502\n\u251c\u2500\u2500 alembic\/                        # Database migrations using Alembic (if needed)\n\u2502   \u251c\u2500\u2500 versions\/                    # Directory for migration scripts\n\u2502   \u2514\u2500\u2500 env.py                       # Alembic environment configuration\n\u2502\n\u251c\u2500\u2500 Dockerfile                      # Dockerfile for containerizing the application\n\u251c\u2500\u2500 .env                            # Environment variables file (for secrets and config)\n\u251c\u2500\u2500 .gitignore                      # Files and directories to ignore in git\n\u251c\u2500\u2500 pyproject.toml                   # Dependencies and project metadata (or requirements.txt)\n\u251c\u2500\u2500 README.md                       # Documentation of the project\n\u2514\u2500\u2500 uvicorn_config.py               # Configuration for running the FastAPI app with Uvicorn\n<\/pre>\n<ul>\n<li> <strong>app\/main.py<\/strong>\uff1afastapi \u5e94\u7528\u7a0b\u5e8f\u7684\u5165\u53e3\u70b9\u3002\u5b83\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\uff0c\u5305\u62ec\u8def\u7531\u5668\u548c\u5176\u4ed6\u914d\u7f6e\u3002<\/li>\n<li> <strong>api\/<\/strong>\uff1a\u5305\u542b\u5e93\u5b58\u7cfb\u7edf\u5404\u4e2a\u65b9\u9762\uff08\u4ea7\u54c1\u3001\u5e93\u5b58\u3001\u9500\u552e\u3001\u7528\u6237\uff09\u7684\u8def\u7531\u5b9a\u4e49\u3002<\/li>\n<li> <strong>db\/<\/strong>\uff1a\u5305\u62ec sqlalchemy \u6a21\u578b\u3001\u6570\u636e\u5e93\u4f1a\u8bdd\u8bbe\u7f6e\u548c\u76f8\u5173\u6587\u4ef6\u3002<\/li>\n<li> <strong>crud\/<\/strong>\uff1a\u901a\u8fc7crud\u64cd\u4f5c\u5904\u7406\u6570\u636e\u5e93\u548capi\u4e4b\u95f4\u7684\u4ea4\u4e92\u3002<\/li>\n<li> <strong>schemas\/<\/strong>\uff1a\u5b9a\u4e49\u7528\u4e8e\u9a8c\u8bc1\u548c\u5e8f\u5217\u5316\u8bf7\u6c42\u548c\u54cd\u5e94\u6570\u636e\u7684 pydantic \u6a21\u578b\u3002<\/li>\n<li> <strong>services\/<\/strong>\uff1a\u5305\u542b\u7cfb\u7edf\u5404\u79cd\u529f\u80fd\u7684\u4e1a\u52a1\u903b\u8f91\u3002<\/li>\n<li> <strong>tests\/<\/strong>\uff1a\u5305\u542b\u5355\u5143\u6d4b\u8bd5\u548c\u96c6\u6210\u6d4b\u8bd5\u4ee5\u786e\u4fdd\u529f\u80fd\u3002<\/li>\n<li> <strong>alembic\/<\/strong>\uff1a\u5982\u679c\u60a8\u4f7f\u7528 alembic \u8fdb\u884c\u6570\u636e\u5e93\u8fc1\u79fb\uff0c\u8fd9\u5c31\u662f\u8fc1\u79fb\u6587\u4ef6\u6240\u5728\u7684\u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u79cd\u7ed3\u6784\u5bf9\u4e8e\u5e93\u5b58\u7cfb\u7edf\u6765\u8bf4\u662f\u7075\u6d3b\u4e14\u53ef\u6269\u5c55\u7684\uff0c\u4fc3\u8fdb\u5173\u6ce8\u70b9\u5206\u79bb\uff0c\u66f4\u5bb9\u6613\u6d4b\u8bd5\u548c\u7ef4\u62a4\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u4f7f\u7528FastAPI\u6784\u5efa\u5e93\u5b58\u7cfb\u7edf\u7684\u76ee\u5f55\u7ed3\u6784\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528FastAPI\u6784\u5efa\u5e93\u5b58\u7cfb\u7edf\u7684&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204007","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204007","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204007"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204007\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}