{"id":947,"date":"2026-05-26T23:13:56","date_gmt":"2026-05-26T15:13:56","guid":{"rendered":"https:\/\/junai.ai\/blog\/fastapi-mini-project-12\/"},"modified":"2026-05-26T23:13:56","modified_gmt":"2026-05-26T15:13:56","slug":"fastapi-mini-project-12","status":"publish","type":"post","link":"https:\/\/junai.ai\/blog\/fastapi-mini-project-12\/","title":{"rendered":"FastAPI \ubbf8\ub2c8 \ud504\ub85c\uc81d\ud2b8 \u2014 To-Do API"},"content":{"rendered":"\n<!-- WordPress REST API \ubc1c\ud589\uc6a9 HTML (\uc790\ub3d9 \uc0dd\uc131) -->\n<!-- WP-FEATURED-MEDIA-ID: 934 -->\n<div style=\"max-width:800px;margin:0 auto;\">\n<style>\n:root {--color-primary:#00897b;--color-accent:#009688;--color-bg:#fafbfc;--color-bg-card:#fff;--color-text:#1a202c;--color-text-muted:#64748b;--hero-start:#004d40;--hero-end:#00897b;}\n*{box-sizing:border-box;}\n.container{max-width:760px;margin:0 auto;padding:0 24px 80px;}\n.hero{background:linear-gradient(135deg,var(--hero-start) 0%,var(--hero-end) 100%);color:#fff;padding:80px 24px 60px;text-align:center;}\n.hero .eyebrow{display:inline-block;font-size:14px;color:#80cbc4;font-weight:700;letter-spacing:0.1em;text-transform:uppercase;margin-bottom:14px;}\n.hero h1{font-size:36px;margin:0 0 16px;line-height:1.3;font-weight:800;}\n.hero p{color:#b2dfdb;font-size:18px;max-width:640px;margin:0 auto;line-height:1.6;}\n.hero img{width:100%;max-width:640px;height:auto;margin:32px auto 0;border-radius:10px;display:block;}\narticle{padding-top:48px;}\narticle h2{font-size:26px;margin:56px 0 20px;padding-left:14px;border-left:5px solid var(--color-accent);line-height:1.4;}\narticle h3{font-size:19px;margin:32px 0 12px;color:var(--color-primary);}\narticle p{margin:16px 0;}\narticle strong{color:var(--color-primary);font-weight:700;}\narticle code{background:#b2dfdb;padding:2px 8px;border-radius:4px;font-family:'SF Mono',Menlo,Consolas,monospace;font-size:14px;color:#004d40;}\n.databox{background:#b2dfdb;border-left:4px solid var(--color-accent);padding:16px 20px;margin:24px 0;border-radius:0 8px 8px 0;font-size:15.5px;}\n.databox strong{color:var(--color-primary);}\n.warnbox{background:linear-gradient(135deg,#fef3c7 0%,#fde68a 100%);padding:16px 20px;margin:24px 0;border-radius:8px;font-size:15.5px;}\n.tablewrap{overflow-x:auto;-webkit-overflow-scrolling:touch;margin:22px 0;}\ntable{width:100%;border-collapse:collapse;font-size:15px;background:var(--color-bg-card);}\nth,td{padding:11px 12px;text-align:left;border-bottom:1px solid #e2e8f0;vertical-align:top;}\nth{background:#f1f5f9;font-weight:700;color:#0f172a;}\ntd:first-child,th:first-child{font-weight:700;}\n@media (max-width:560px){.tablewrap table,.tablewrap thead,.tablewrap tbody,.tablewrap tr,.tablewrap th,.tablewrap td{display:block;width:auto;}.tablewrap thead{display:none;}.tablewrap tr{margin:0 0 14px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden;}.tablewrap td{border:none;border-bottom:1px solid #f1f5f9;padding:9px 14px;}.tablewrap td:first-child{background:#f1f5f9;font-weight:800;font-size:15.5px;}.tablewrap td:last-child{border-bottom:none;}.tablewrap td[data-label]::before{content:attr(data-label) \" \u2014 \";font-weight:700;color:var(--color-primary);}}\n.code-block{background:#0f172a;color:#e2e8f0;padding:16px 20px;border-radius:8px;font-family:'SF Mono',Menlo,Consolas,monospace;font-size:14px;line-height:1.6;margin:20px 0;overflow-x:auto;white-space:pre;}\n.cta{background:linear-gradient(135deg,#00897b 0%,#009688 100%);color:#fff;padding:28px 24px;border-radius:12px;margin:48px 0 0;text-align:center;}\n.cta h3{color:#fff;margin:0 0 8px;font-size:20px;}\n.cta p{color:#b2dfdb;margin:0;font-size:15.5px;}\n.footer-nav{margin-top:32px;padding-top:20px;border-top:1px solid #e2e8f0;font-size:14px;color:var(--color-text-muted);}\n.footer-nav a{color:var(--color-primary);text-decoration:none;}\n@media (max-width:480px){.hero h1{font-size:26px;}.hero p{font-size:16px;}article h2{font-size:21px;}article h3{font-size:17px;}body{font-size:16px;}}\n<\/style>\n<section class=\"hero\">\n  <span class=\"eyebrow\">FastAPI \uad50\uc7ac \u00b7 12\ud3b8 \u00b7 \ubbf8\ub2c8 \ud504\ub85c\uc81d\ud2b8 (\uc644\uacb0)<\/span>\n  <h1>FastAPI \ubbf8\ub2c8 \ud504\ub85c\uc81d\ud2b8 \u2014 To-Do API<\/h1>\n  <p>\ud769\uc5b4\uc838 \uc788\ub358 11\ud3b8\uc758 \uc870\uac01\uc744 \ud55c\uc790\ub9ac\uc5d0 \ubaa8\uc740\ub2e4. \ubaa8\ub378\u00b7\uac80\uc99d\u00b7\uc778\uc99d\u00b7\ud14c\uc2a4\ud2b8\u00b7\ubc30\ud3ec\uac00 \uc804\ubd80 \ub4e4\uc5b4\uac04 \uc0ac\uc6a9\uc790\ubcc4 \ud560 \uc77c API \ub97c \ucc98\uc74c\ubd80\ud130 \uc644\uc131\ud55c\ub2e4.<\/p>\n  <img decoding=\"async\" src=\"https:\/\/junai.ai\/blog\/wp-content\/uploads\/2026\/05\/hero-5-109.jpg\" alt=\"\ud560 \uc77c \ubaa9\ub85d API \uac00 \ub370\uc774\ud130\ubca0\uc774\uc2a4\u00b7\uc778\uc99d\u00b7\uc11c\ubc84 \ube14\ub85d\uc73c\ub85c \uc870\ub9bd\ub418\uc5b4 \uc644\uc131\ub418\ub294 FastAPI \ucea1\uc2a4\ud1a4 \ud504\ub85c\uc81d\ud2b8 \uc544\uc774\uc18c\uba54\ud2b8\ub9ad \uc77c\ub7ec\uc2a4\ud2b8\">\n<\/section>\n\n<div class=\"container\">\n<article>\n\n<p>\ub4dc\ub514\uc5b4 \ub9c8\uc9c0\ub9c9 \ud3b8\uc774\ub2e4. \uc9c0\ub09c \uc5f4\ud55c \ud3b8 \ub3d9\uc548 \uc6b0\ub9ac\ub294 \ubd80\ud488\uc744 \ud558\ub098\uc529 \ub9cc\ub4e4\uc5b4 \uc654\ub2e4 \u2014 \ud0c0\uc785\ud78c\ud2b8\ub85c \uc785\ub825\uc744 \ubc1b\uace0(2\u00b73\ud3b8), \uc758\uc874\uc131\uc744 \uc8fc\uc785\ud558\uace0(5\ud3b8), \ub77c\uc6b0\ud130\ub85c \ub098\ub204\uace0(6\ud3b8), DB \uc5d0 \uc800\uc7a5\ud558\uace0(7\u00b78\ud3b8), \ud1a0\ud070\uc73c\ub85c \uc7a0\uadf8\uace0(9\ud3b8), \ud14c\uc2a4\ud2b8\ud558\uace0(10\ud3b8), \ubc30\ud3ec\ud558\ub294(11\ud3b8) \ubc95\uae4c\uc9c0. \ud558\ub098\ud558\ub098\ub294 \uc775\ud614\uc9c0\ub9cc, <strong>\ud55c \ud504\ub85c\uc81d\ud2b8\uc5d0\uc11c \uc5b4\ub5bb\uac8c \ub9de\ubb3c\ub9ac\ub294\uc9c0<\/strong>\ub294 \ud1b5\uc9f8\ub85c \ubcf8 \uc801\uc774 \uc5c6\ub2e4.<\/p>\n\n<p>12\ud3b8\uc740 \uadf8 \uc870\uac01\ub4e4\uc744 \uaebc\ub0b4 \ud558\ub098\ub85c \uc870\ub9bd\ud55c\ub2e4. \ub9cc\ub4e4 \uac83\uc740 <strong>\uc0ac\uc6a9\uc790\ubcc4 \ud560 \uc77c(To-Do) REST API<\/strong> \u2014 \ub85c\uadf8\uc778\ud55c \uc0ac\ub78c\uc774 \uc790\uae30 \ud560 \uc77c\uc744 \ub9cc\ub4e4\uace0, \ubcf4\uace0, \ub05d\ub0b4\uace0, \uc9c0\uc6b0\ub294 \uc791\uc9c0\ub9cc \uc644\uacb0\ub41c \uc11c\ube44\uc2a4\ub2e4. \uc0c8 \ubb38\ubc95\uc740 \uac70\uc758 \uc5c6\ub2e4. <strong>&#8220;\uc774\uac8c \uc774\ub807\uac8c \ud569\uccd0\uc9c0\ub294\uad6c\ub098&#8221;<\/strong> \ub97c \ubcf4\ub294 \uac8c \uc804\ubd80\ub2e4.<\/p>\n\n<h2>1. \ud504\ub85c\uc81d\ud2b8 \uac1c\uc694\uc640 \ud3f4\ub354 \uad6c\uc870<\/h2>\n\n<p>\uba3c\uc800 \ub9cc\ub4e4 \uac83\uc758 \uc724\uacfd\ubd80\ud130. <strong>To-Do API<\/strong> \ub294 \uc0ac\uc6a9\uc790(User)\uc640 \ud560 \uc77c(Todo) \ub450 \ud14c\uc774\ube14\uc744 \uac00\uc9c4\ub2e4. \ud55c \uc0ac\uc6a9\uc790\ub294 \uc5ec\ub7ec \ud560 \uc77c\uc744 \uac00\uc9c8 \uc218 \uc788\uace0, \uac01 \ud560 \uc77c\uc5d0\ub294 <strong>\uc8fc\uc778(owner)<\/strong> \uc774 \uc788\ub2e4. \uadf8\ub798\uc11c \uae40\ucca0\uc218\uac00 \ub85c\uadf8\uc778\ud558\uba74 \uae40\ucca0\uc218\uc758 \ud560 \uc77c\ub9cc \ubcf4\uc774\uace0, \ub0a8\uc758 \uac83\uc740 \ubcf4\uc9c0\ub3c4 \uace0\uce58\uc9c0\ub3c4 \ubabb\ud55c\ub2e4. \uc774 &#8220;\ubcf8\uc778 \uac83\ub9cc&#8221; \uaddc\uce59\uc774 9\ud3b8 \uc778\uc99d\uc744 \uc2e4\uc804\uc5d0\uc11c \uc4f0\ub294 \ud575\uc2ec \uc7a5\uba74\uc774\ub2e4.<\/p>\n\n<p>6\ud3b8\uc758 \uad8c\uc7a5 \uad6c\uc870\ub97c \uadf8\ub300\ub85c \ub530\ub978\ub2e4 \u2014 \ud30c\uc77c\ub9c8\ub2e4 \ud55c \uac00\uc9c0 \uc77c\ub9cc \ub9e1\ub294\ub2e4.<\/p>\n\n<div class=\"code-block\">todo-api\/\n\u251c\u2500\u2500 app\/\n\u2502   \u251c\u2500\u2500 main.py          # \uc571 + \ub77c\uc6b0\ud130 \uc5f0\uacb0 (6\ud3b8)\n\u2502   \u251c\u2500\u2500 database.py      # \uc5d4\uc9c4\u00b7\uc138\uc158\u00b7get_db (7\ud3b8)\n\u2502   \u251c\u2500\u2500 models.py        # \ud14c\uc774\ube14 User\u00b7Todo (7\ud3b8)\n\u2502   \u251c\u2500\u2500 schemas.py       # \uc785\ub825\u00b7\ucd9c\ub825 \uc2a4\ud0a4\ub9c8 (3\ud3b8)\n\u2502   \u251c\u2500\u2500 auth.py          # \ud574\uc2f1\u00b7JWT\u00b7get_current_user (9\ud3b8)\n\u2502   \u2514\u2500\u2500 routers\/\n\u2502       \u251c\u2500\u2500 users.py     # \ud68c\uc6d0\uac00\uc785\u00b7\ub85c\uadf8\uc778 (8\u00b79\ud3b8)\n\u2502       \u2514\u2500\u2500 todos.py     # \ud560 \uc77c CRUD + \uc778\uc99d (8\u00b79\ud3b8)\n\u251c\u2500\u2500 tests\/test_todos.py  # TestClient + pytest (10\ud3b8)\n\u251c\u2500\u2500 requirements.txt\n\u2514\u2500\u2500 Dockerfile           # \ucee8\ud14c\uc774\ub108 \ubc30\ud3ec (11\ud3b8)<\/div>\n\n<p>\uc774 \ud2b8\ub9ac\uac00 \uc2dc\ub9ac\uc988 \uc804\uccb4\uc758 \uc9c0\ub3c4\ub2e4. \uc8fc\uc11d\uc758 \ud3b8 \ubc88\ud638\ub97c \ub530\ub77c\uac00\uba74 \uac01 \uc870\uac01\uc774 \uc5b4\ub514\uc11c \uc654\ub294\uc9c0 \ud55c\ub208\uc5d0 \ubcf4\uc778\ub2e4 \u2014 \uc774 \ub9e4\ud551\uc774 \uba38\ub9ac\uc5d0 \ubc15\ud788\uba74 \uc5b4\ub5a4 FastAPI \ud504\ub85c\uc81d\ud2b8\ub97c \ubd10\ub3c4 \uae38\uc744 \uc783\uc9c0 \uc54a\ub294\ub2e4.<\/p>\n\n<h2>2. \ubaa8\ub378\uacfc \uc2a4\ud0a4\ub9c8 \u2014 \ub370\uc774\ud130\uc758 \ubf08\ub300<\/h2>\n\n<p>\ucd9c\ubc1c\uc810\uc740 \ub370\uc774\ud130\uc758 \ubaa8\uc591\uc774\ub2e4. 7\ud3b8\uc758 SQLAlchemy \ubaa8\ub378 \ub450 \uac1c\ubd80\ud130 \uc801\ub294\ub2e4. <code>User<\/code> \ub294 9\ud3b8 \uc778\uc99d\uc744 \uc704\ud574 <code>hashed_password<\/code> \ub97c \uac00\uc9c0\uace0, <code>Todo<\/code> \ub294 <code>owner_id<\/code> \ub85c \uc8fc\uc778\uc744 \uac00\ub9ac\ud0a8\ub2e4. \ub450 \ud14c\uc774\ube14\uc740 <code>relationship<\/code> \uc73c\ub85c \ubb36\ub294\ub2e4.<\/p>\n\n<div class=\"code-block\"># app\/models.py\nfrom sqlalchemy import Column, Integer, String, Boolean, ForeignKey\nfrom sqlalchemy.orm import relationship\nfrom database import Base\n\nclass User(Base):\n    __tablename__ = &#8220;users&#8221;\n    id = Column(Integer, primary_key=True, index=True)\n    email = Column(String, unique=True, index=True)\n    hashed_password = Column(String)          # \ud3c9\ubb38 \uae08\uc9c0 \u2014 9\ud3b8\n    todos = relationship(&#8220;Todo&#8221;, back_populates=&#8221;owner&#8221;)\n\nclass Todo(Base):\n    __tablename__ = &#8220;todos&#8221;\n    id = Column(Integer, primary_key=True, index=True)\n    title = Column(String, nullable=False)\n    done = Column(Boolean, default=False)\n    owner_id = Column(Integer, ForeignKey(&#8220;users.id&#8221;))   # \ud560 \uc77c\uc758 \uc8fc\uc778\n    owner = relationship(&#8220;User&#8221;, back_populates=&#8221;todos&#8221;)<\/div>\n\n<p>\ud575\uc2ec\uc740 <code>owner_id = Column(Integer, ForeignKey(\"users.id\"))<\/code> \ud55c \uc904\uc774\ub2e4. \uc774\uac8c \ud560 \uc77c\uc744 \uc0ac\uc6a9\uc790\uc5d0 \ubb36\ub294 <strong>\uc678\ub798 \ud0a4(foreign key)<\/strong> \ub85c, &#8220;\uc774 Todo \uc758 \uc8fc\uc778\uc740 users \uc758 \ub204\uad6c\uc778\uac00&#8221; \ub97c \uc22b\uc790 \ud558\ub098\ub85c \uac00\ub9ac\ud0a8\ub2e4. <code>relationship<\/code> \uc740 \uadf8 \uc5f0\uacb0\uc744 \ud30c\uc774\uc36c \uac1d\uccb4\ub85c \ud0c0\uace0 \ub2e4\ub2c8\uac8c \ud574 \uc900\ub2e4 \u2014 <code>user.todos<\/code> \ub85c \uadf8 \uc0ac\ub78c\uc758 \ud560 \uc77c\uc744, <code>todo.owner<\/code> \ub85c \uc8fc\uc778\uc744 \uace7\uc7a5 \uaebc\ub0bc \uc218 \uc788\ub2e4.<\/p>\n\n<p>\uc774\uc81c 3\ud3b8\uc758 Pydantic \uc2a4\ud0a4\ub9c8. 8\ud3b8\uc5d0\uc11c \uac15\uc870\ud55c \ub300\ub85c <strong>\uc785\ub825\uc6a9\uacfc \ucd9c\ub825\uc6a9\uc744 \ubd84\ub9ac<\/strong>\ud55c\ub2e4. \ud074\ub77c\uc774\uc5b8\ud2b8\ub294 <code>title<\/code> \ub9cc \ubcf4\ub0b4\uba74 \ub418\uace0(<code>id<\/code>\u00b7<code>owner_id<\/code> \ub294 \uc11c\ubc84\uac00 \uc815\ud55c\ub2e4), \uc751\ub2f5\uc5d0\ub294 \ube44\ubc00\ubc88\ud638 \uac19\uc740 \uac8c \uc0c8\uc9c0 \uc54a\uac8c \ucd9c\ub825 \uc2a4\ud0a4\ub9c8\ub85c \uac70\ub978\ub2e4.<\/p>\n\n<div class=\"code-block\"># app\/schemas.py\nfrom pydantic import BaseModel, ConfigDict\n\nclass TodoCreate(BaseModel):     # \uc785\ub825\uc6a9 \u2014 \ud074\ub77c\uc774\uc5b8\ud2b8\uac00 \ubcf4\ub0b4\ub294 \uac83\n    title: str\n\nclass TodoOut(BaseModel):        # \ucd9c\ub825\uc6a9 \u2014 \uc751\ub2f5\uc73c\ub85c \ub3cc\ub824\uc904 \uac83\n    id: int\n    title: str\n    done: bool\n    owner_id: int\n    model_config = ConfigDict(from_attributes=True)   # ORM \uac1d\uccb4 \u2192 \uc2a4\ud0a4\ub9c8 \uc790\ub3d9 \ubcc0\ud658<\/div>\n\n<p>\uc65c \ub458\ub85c \ub098\ub204\ub098 \u2014 \uc785\ub825 \uc2a4\ud0a4\ub9c8\uc5d0 <code>id<\/code> \ub97c \ub123\uc73c\uba74 \ud074\ub77c\uc774\uc5b8\ud2b8\uac00 \uba4b\ub300\ub85c \uc815\ud574 \ubcf4\ub0bc \uc218 \uc788\uace0, \ucd9c\ub825 \uc2a4\ud0a4\ub9c8\uac00 \uc5c6\uc73c\uba74 \ubbfc\uac10 \ud544\ub4dc\uac00 \uc751\ub2f5\uc5d0 \uc0c8\uc5b4 \ub098\uac04\ub2e4. <code>from_attributes=True<\/code>(Pydantic v2) \ub294 SQLAlchemy \uac1d\uccb4\ub97c <code>TodoOut<\/code> \uc73c\ub85c \uc790\ub3d9 \ubcc0\ud658\ud574 \uc8fc\ub294 \uc2a4\uc704\uce58\ub77c, \ub77c\uc6b0\ud2b8\uc5d0\uc11c <code>return todo<\/code> \ud55c \uc904\uc774\uba74 FastAPI \uac00 \uc54c\uc544\uc11c JSON \uc73c\ub85c \ube5a\uc5b4 \uc900\ub2e4.<\/p>\n\n<h2>3. CRUD \ub77c\uc6b0\ud130 + \uc778\uc99d \ubcf4\ud638 \u2014 \ubcf8\uc778 \uac83\ub9cc<\/h2>\n\n<p>\uc774\uc81c \uc774 \ud504\ub85c\uc81d\ud2b8\uc758 \uc2ec\uc7a5\uc774\ub2e4. 8\ud3b8\uc758 CRUD \uc640 9\ud3b8\uc758 \uc778\uc99d\uc774 \uc5ec\uae30\uc11c \ub9cc\ub09c\ub2e4. 9\ud3b8\uc5d0\uc11c \ub9cc\ub4e0 \uc778\uc99d \uc870\uac01 \u2014 \ud1a0\ud070\uc744 \uac80\uc99d\ud574 \uc0ac\uc6a9\uc790\ub97c \ub3cc\ub824\uc8fc\ub294 <code>get_current_user<\/code> \u2014 \uc744 <code>auth.py<\/code> \uc5d0 \uadf8\ub300\ub85c \ub450\uace0, <strong>\ubaa8\ub4e0 \ub77c\uc6b0\ud2b8\uac00 &#8220;\ub85c\uadf8\uc778\ud55c \ubcf8\uc778\uc758 \ud560 \uc77c\ub9cc&#8221; \ub2e4\ub8e8\uac8c<\/strong> \ub9cc\ub4e0\ub2e4.<\/p>\n\n<p>\ud575\uc2ec\uc740 <code>routers\/todos.py<\/code> \ub2e4. CRUD \ub124 \ub3d9\uc791\uc744 \uc801\ub418 \ubaa8\ub4e0 \ub77c\uc6b0\ud2b8 \uc778\uc790\uc5d0 <code>user: models.User = Depends(get_current_user)<\/code> \ub97c \ub07c\uc6b4\ub2e4. \uc774 \ud55c \uc904 \ub355\ubd84\uc5d0 \ubcf8\ubb38\uc774 \uc2e4\ud589\ub418\uae30 \uc804\uc5d0 \ud1a0\ud070 \uac80\uc0ac\uac00 \ub05d\ub098 \uc788\uace0, <code>user<\/code> \uc5d0\ub294 <strong>\uc9c0\uae08 \ub85c\uadf8\uc778\ud55c \uc0ac\ub78c<\/strong> \uc774 \ub2f4\uaca8 \ub4e4\uc5b4\uc628\ub2e4. \uadf8\ub2e4\uc74c \ubaa8\ub4e0 \uc9c8\uc758\ub97c <code>owner_id == user.id<\/code> \ub85c \uac78\uae30\ub9cc \ud558\uba74 \ub41c\ub2e4.<\/p>\n\n<div class=\"code-block\"># app\/routers\/todos.py\nfrom fastapi import APIRouter, Depends, HTTPException\nfrom sqlalchemy.orm import Session\nfrom database import get_db\nfrom auth import get_current_user\nimport models, schemas\n\nrouter = APIRouter(prefix=&#8221;\/todos&#8221;, tags=[&#8220;todos&#8221;])\n\n# Create \u2014 \uc8fc\uc778\uc740 \ub85c\uadf8\uc778 \uc0ac\uc6a9\uc790\n@router.post(&#8220;&#8221;, response_model=schemas.TodoOut, status_code=201)\ndef create_todo(payload: schemas.TodoCreate, db: Session = Depends(get_db),\n                user: models.User = Depends(get_current_user)):\n    todo = models.Todo(title=payload.title, owner_id=user.id)\n    db.add(todo); db.commit(); db.refresh(todo)\n    return todo\n\n# Read \ubaa9\ub85d \u2014 \ub0b4 \ud560 \uc77c\ub9cc\n@router.get(&#8220;&#8221;, response_model=list[schemas.TodoOut])\ndef list_my_todos(db: Session = Depends(get_db),\n                  user: models.User = Depends(get_current_user)):\n    return db.query(models.Todo).filter(models.Todo.owner_id == user.id).all()<\/div>\n\n<p>\ub098\uba38\uc9c0 \uc218\uc815\u00b7\uc0ad\uc81c\ub3c4 \ud328\ud134\uc774 \uac19\ub2e4 \u2014 <strong>\uba3c\uc800 \ucc3e\uace0, \uc5c6\uc73c\uba74 404, \ub0a8\uc758 \uac83\uc774\uba74 403, \uadf8\uc81c\uc57c \uc190\uc744 \ub304\ub2e4.<\/strong> \uc774 &#8220;\ub0b4 \uac83 \ud655\uc778&#8221; \uc744 \uc791\uc740 \ud5ec\ud37c\ub85c \ube7c\uba74 \ub77c\uc6b0\ud2b8\uac00 \ud55c\uacb0 \uae54\ub054\ud574\uc9c4\ub2e4.<\/p>\n\n<div class=\"code-block\"># app\/routers\/todos.py  (\uc774\uc5b4\uc11c)\n\n# \ud5ec\ud37c \u2014 \uc5c6\uc73c\uba74 404, \ub0a8\uc758 \uac83\uc774\uba74 403\ndef get_owned_todo(todo_id, db, user) -> models.Todo:\n    todo = db.get(models.Todo, todo_id)\n    if todo is None:\n        raise HTTPException(status_code=404, detail=&#8221;\ud560 \uc77c\uc744 \ucc3e\uc744 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4&#8221;)\n    if todo.owner_id != user.id:                  # \ub0a8\uc758 \uac83 \u2014 \uc778\uac00 \uc2e4\ud328\n        raise HTTPException(status_code=403, detail=&#8221;\uad8c\ud55c\uc774 \uc5c6\uc2b5\ub2c8\ub2e4&#8221;)\n    return todo\n\n# Update \u2014 \uc81c\ubaa9 \uc218\uc815 + \uc644\ub8cc \ud1a0\uae00\n@router.put(&#8220;\/{todo_id}&#8221;, response_model=schemas.TodoOut)\ndef update_todo(todo_id: int, payload: schemas.TodoCreate,\n                db: Session = Depends(get_db),\n                user: models.User = Depends(get_current_user)):\n    todo = get_owned_todo(todo_id, db, user)\n    todo.title = payload.title\n    todo.done = not todo.done                     # \ud638\ucd9c\ub9c8\ub2e4 \uc644\ub8cc\/\ubbf8\uc644\ub8cc \ud1a0\uae00\n    db.commit(); db.refresh(todo)\n    return todo\n\n# Delete \u2014 \ubcf8\ubb38 \uc5c6\uc774 204\n@router.delete(&#8220;\/{todo_id}&#8221;, status_code=204)\ndef delete_todo(todo_id: int, db: Session = Depends(get_db),\n                user: models.User = Depends(get_current_user)):\n    db.delete(get_owned_todo(todo_id, db, user)); db.commit()<\/div>\n\n<div class=\"databox\">\n<strong>404 \uc640 403 \uc744 \ub098\ub208 \uc774\uc720<\/strong> \u2014 \ud560 \uc77c\uc774 \uc544\uc608 \uc5c6\uc73c\uba74 <strong>404<\/strong>(Not Found), \uc788\ub294\ub370 \ub0a8\uc758 \uac83\uc774\uba74 <strong>403<\/strong>(Forbidden)\uc774\ub2e4. 9\ud3b8\uc5d0\uc11c \ub9d0\ud55c <strong>\uc778\uc99d\uacfc \uc778\uac00\uc758 \ucc28\uc774<\/strong> \uac00 \ucf54\ub4dc\ub85c \ub4dc\ub7ec\ub098\ub294 \uc9c0\uc810\uc774\ub2e4. \ud1a0\ud070 \uac80\uc0ac\ub97c \ud1b5\uacfc\ud574 &#8220;\ub204\uad6c\uc778\uc9c0(\uc778\uc99d)&#8221; \ub294 \uc54c\uc558\uc9c0\ub9cc \uc774 \ud560 \uc77c\uc744 \ub9cc\uc9c8 &#8220;\uc790\uaca9(\uc778\uac00)&#8221; \uc774 \uc5c6\uc73c\ub2c8 \ub9c9\ub294\ub2e4. <code>get_current_user<\/code> \uac00 \uc778\uc99d\uc744, <code>owner_id != user.id<\/code> \ud55c \uc904\uc774 \uc778\uac00\ub97c \ucc45\uc784\uc9c4\ub2e4.\n<\/div>\n\n<p>\ub9c8\uc9c0\ub9c9\uc73c\ub85c 6\ud3b8\ucc98\ub7fc <code>main.py<\/code> \uc5d0\uc11c \ub77c\uc6b0\ud130\ub97c \ud569\uce58\uace0, 7\ud3b8\ucc98\ub7fc \ud14c\uc774\ube14\uc744 \ub9cc\ub4e0\ub2e4. \ubcf8\uccb4\ub294 \ub2e4\uc2dc \uac00\ubcbc\uc6cc\uc9c4\ub2e4.<\/p>\n\n<div class=\"code-block\"># app\/main.py\nfrom fastapi import FastAPI\nfrom database import Base, engine\nfrom routers import users, todos\nimport models                                # \ubaa8\ub378\uc744 import \ud574\uc57c \uba54\ud0c0\ub370\uc774\ud130\uc5d0 \ub4f1\ub85d\ub41c\ub2e4\n\nBase.metadata.create_all(bind=engine)        # \uc5c6\ub294 \ud14c\uc774\ube14\ub9cc \uc0dd\uc131 (7\ud3b8)\n\napp = FastAPI(title=&#8221;To-Do API&#8221;)\napp.include_router(users.router)             # \/users\/signup \u00b7 \/users\/login\napp.include_router(todos.router)             # \/todos &#8230;\n\n@app.get(&#8220;\/&#8221;)\ndef health():\n    return {&#8220;status&#8221;: &#8220;ok&#8221;}<\/div>\n\n<h2>4. \uc2e4\ud589 \u00b7 \ud14c\uc2a4\ud2b8 \u00b7 \ub2e4\uc74c \ub2e8\uacc4<\/h2>\n\n<p>\ucf54\ub4dc\uac00 \ub2e4 \ubaa8\uc600\uc73c\ub2c8 \ub3cc\ub824 \ubcf8\ub2e4. 9\ud3b8\uc5d0\uc11c <code>SECRET_KEY<\/code> \ub294 \ud658\uacbd\ubcc0\uc218\ub85c\ub9cc \ub454\ub2e4\uace0 \ud588\uc73c\ub2c8, \ud0a4\ub97c \ub0b4\ubcf4\ub0b4\uace0 1\ud3b8 \uadf8\ub300\ub85c \uac1c\ubc1c \uc11c\ubc84\ub97c \ub744\uc6b4\ub2e4.<\/p>\n\n<div class=\"code-block\">$ export SECRET_KEY=&#8221;\uc544\ubb34\ub3c4-\ubaa8\ub974\ub294-\uae34-\ub79c\ub364-\ubb38\uc790\uc5f4&#8221;\n$ fastapi dev app\/main.py\n\n  Serving at http:\/\/127.0.0.1:8000\n  API docs at http:\/\/127.0.0.1:8000\/docs<\/div>\n\n<p><code>\/docs<\/code> \uc5d0 \ub4e4\uc5b4\uac00\uba74 9\ud3b8\uc5d0\uc11c \ubcf8 <strong>Authorize \ubc84\ud2bc<\/strong> \uc774 \ubcf4\uc778\ub2e4. \u2460 <code>POST \/users\/signup<\/code> \uc73c\ub85c \uac00\uc785\ud558\uace0, \u2461 Authorize \uc5d0 \ub85c\uadf8\uc778\ud558\uba74 \ud1a0\ud070\uc774 \uc790\ub3d9\uc73c\ub85c \ub07c\uc6cc\uc9c4\ub2e4. \u2462 <code>POST \/todos<\/code> \ub85c \ud560 \uc77c\uc744 \ub9cc\ub4e4\uba74, \u2463 <code>GET \/todos<\/code> \ub294 <strong>\ub0b4 \uac83\ub9cc<\/strong> \ub3cc\ub824\uc900\ub2e4. \ub2e4\ub978 \uacc4\uc815\uc73c\ub85c \ub85c\uadf8\uc778\ud558\uba74 \uadf8 \ud560 \uc77c\uc740 \ubaa9\ub85d\uc5d0 \ub728\uc9c0 \uc54a\ub294\ub2e4 \u2014 \uc778\uc99d\uacfc \uc778\uac00\uac00 \uc0b4\uc544 \uc6c0\uc9c1\uc774\ub294 \uc21c\uac04\uc774\ub2e4.<\/p>\n\n<p>\ub208\uc73c\ub85c \ud655\uc778\ud588\uc73c\uba74 10\ud3b8\uc758 \uc790\ub3d9 \ud14c\uc2a4\ud2b8\ub85c \ubabb\uc744 \ubc15\ub294\ub2e4. <code>TestClient<\/code> \ub85c \uc11c\ubc84\ub97c \ub744\uc6b0\uc9c0 \uc54a\uace0\ub3c4 \ub77c\uc6b0\ud2b8\ub97c \ud638\ucd9c\ud574, &#8220;\ud1a0\ud070 \uc5c6\uc774 \ubd80\ub974\uba74 401 \uc778\uac00&#8221; \uac19\uc740 \ud575\uc2ec \uaddc\uce59\uc744 \ucf54\ub4dc\ub85c \uad73\ud78c\ub2e4.<\/p>\n\n<div class=\"code-block\"># tests\/test_todos.py  (10\ud3b8)\nfrom fastapi.testclient import TestClient\nfrom app.main import app\n\nclient = TestClient(app)\n\ndef test_todos_require_auth():\n    # \ud1a0\ud070 \uc5c6\uc774 \ud638\ucd9c\ud558\uba74 \ubcf4\ud638\ub41c \ub77c\uc6b0\ud2b8\ub294 401 \uc774\uc5b4\uc57c \ud55c\ub2e4\n    res = client.get(&#8220;\/todos&#8221;)\n    assert res.status_code == 401<\/div>\n\n<div class=\"warnbox\">\n<strong>\uc2e4\uc804\uc73c\ub85c \uac00\uae30 \uc804 \uccb4\ud06c<\/strong> \u2014 \uc9c0\uae08 \ucf54\ub4dc\ub294 \ud559\uc2b5\uc6a9\uc774\ub77c SQLite \ud30c\uc77c \ud558\ub098(<code>app.db<\/code>)\ub97c \uc4f4\ub2e4. \uc11c\ube44\uc2a4\ub85c \uc62c\ub9ac\uae30 \uc804\uc5d4 \u2460 <code>SECRET_KEY<\/code> \ub97c \uae43\uc5d0 \uc62c\ub9ac\uc9c0 \ub9d0\uace0 \ud658\uacbd\ubcc0\uc218\ub85c, \u2461 DB \ub97c <strong>PostgreSQL<\/strong> \ub85c \ubc14\uafb8\uace0(7\ud3b8\ub300\ub85c \uc5f0\uacb0 \ubb38\uc790\uc5f4 \ud55c \uc904\ub9cc \uad50\uccb4), \u2462 \ubaa9\ub85d\uc774 \uae38\uc5b4\uc9c0\uba74 <code>limit<\/code>\u00b7<code>offset<\/code> \uc73c\ub85c <strong>\ud398\uc774\uc9c0\ub124\uc774\uc158<\/strong> \uc744 \ubd99\uc778\ub2e4. \uac70\uc758 \ubaa8\ub4e0 \uc2e4\uc804 API \uac00 \uac70\uce58\ub294 \uad00\ubb38\uc774\ub2e4.\n<\/div>\n\n<p>\ub9c8\uc9c0\ub9c9\uc73c\ub85c 11\ud3b8\uc758 Docker \ub85c \uc5b4\ub514\uc11c\ub4e0 \ub611\uac19\uc774 \ub3cc\uac8c \ud3ec\uc7a5\ud55c\ub2e4. \uc774\ubbf8\uc9c0 \ud558\ub098\uba74 \ub0b4 \ub178\ud2b8\ubd81\uc774\ub4e0 \ud074\ub77c\uc6b0\ub4dc\ub4e0 \ud658\uacbd \ucc28\uc774\ub85c \uae68\uc9c0\uc9c0 \uc54a\ub294\ub2e4.<\/p>\n\n<div class=\"code-block\"># Dockerfile  (11\ud3b8)\nFROM python:3.12-slim\nWORKDIR \/code\nCOPY requirements.txt .\nRUN pip install &#8211;no-cache-dir -r requirements.txt\nCOPY .\/app .\/app\n# \uc6b4\uc601\uc5d0\uc11c\ub294 fastapi dev \uac00 \uc544\ub2c8\ub77c uvicorn \uc73c\ub85c\nCMD [&#8220;uvicorn&#8221;, &#8220;app.main:app&#8221;, &#8220;&#8211;host&#8221;, &#8220;0.0.0.0&#8221;, &#8220;&#8211;port&#8221;, &#8220;80&#8221;]<\/div>\n\n<p>\uc5ec\uae30\uc11c \uba48\ucd94\uc9c0 \ub9d0\uace0 \ud55c \ubc1c \ub354 \ub098\uac00 \ubcf4\uc790. <strong>\ud504\ub860\ud2b8\uc5d4\ub4dc(Next.js\u00b7React)\ub97c \ubd99\uc5ec<\/strong> \ud654\uba74 \uc788\ub294 \uc571\uc73c\ub85c \ud0a4\uc6b0\uac70\ub098, DB \ub97c PostgreSQL \ub85c \ubc14\uafb8\uac70\ub098, \uac80\uc0c9\u00b7\uc815\ub82c\uc744 \ub354\ud558\uba74 \uadf8\ub300\ub85c \ud3ec\ud2b8\ud3f4\ub9ac\uc624\uac00 \ub41c\ub2e4. \uc774 API \uac00 \uadf8 \ubaa8\ub4e0 \ud655\uc7a5\uc758 \ucd9c\ubc1c\uc810\uc774\ub2e4.<\/p>\n\n<h3>\uc694\uc57d<\/h3>\n\n<p>\uc2dc\ub9ac\uc988 12\ud3b8, \uc644\uc8fc\ub97c \ucd95\ud558\ud55c\ub2e4. 1\ud3b8\uc758 <code>{\"message\": \"\uc548\ub155, FastAPI!\"}<\/code> \ud55c \uc904\ub85c \uc2dc\uc791\ud55c \uc5ec\uc815\uc774, \uc774\uc81c <strong>\uc778\uc99d\ub41c \uc0ac\uc6a9\uc790\uac00 \uc790\uae30 \ub370\uc774\ud130\ub97c \uc548\uc804\ud558\uac8c \ub2e4\ub8e8\uace0 \ud14c\uc2a4\ud2b8\u00b7Docker \ub85c \ub2e8\ub2e8\ud788 \ubb36\uc778 \uc644\uacb0\ub41c API<\/strong> \ub85c \ub05d\ub0ac\ub2e4. \ud070 \uc904\uae30\ub97c \ub418\uac10\uc73c\uba74 \uc774\ub807\ub2e4 \u2014 <strong>\ud0c0\uc785\ud78c\ud2b8\ub85c \ubc1b\uace0 \u2192 Pydantic \uc73c\ub85c \uac80\uc99d\ud558\uace0 \u2192 \uc790\ub3d9 \ubb38\uc11c\ub85c \ub4dc\ub7ec\ub0b4\uace0 \u2192 SQLAlchemy \ub85c \uc800\uc7a5\ud558\uace0 \u2192 JWT \ub85c \ubcf8\uc778\ub9cc \uc7a0\uadf8\uace0 \u2192 pytest \ub85c \uac80\uc99d\ud558\uace0 \u2192 Docker \ub85c \ubc30\ud3ec\ud55c\ub2e4.<\/strong> FastAPI \uc758 \uac70\uc758 \ubaa8\ub4e0 \uc2e4\uc804 \ucf54\ub4dc\uac00 \uc774 \uc77c\uacf1 \ub2e8\uacc4 \uc704\uc5d0 \uc120\ub2e4. \uc0c8 \ud504\ub85c\uc81d\ud2b8\ub97c \ub9cc\ub098\ub3c4 &#8220;\uc774\uac74 \uc5b4\ub290 \ub2e8\uacc4\uc9c0?&#8221; \ub9cc \ubb3c\uc73c\uba74 \uae38\uc774 \ubcf4\uc778\ub2e4. \uc5ec\uae30\uae4c\uc9c0 \uc628 \ub2f9\uc2e0\uc740 \uc774\uc81c FastAPI \ub85c \ubb34\uc5c7\uc774\ub4e0 \uc2dc\uc791\ud560 \uc218 \uc788\ub2e4.<\/p>\n\n<div class=\"cta\">\n<h3>\uc2dc\ub9ac\uc988 \uc644\uacb0 \u2014 \uc774\uc81c \ub2f9\uc2e0 \ucc28\ub840<\/h3>\n<p>To-Do \ub97c \ud3ec\ud06c\ud574 \ud504\ub860\ud2b8\uc5d4\ub4dc\ub97c \ubd99\uc774\uace0, PostgreSQL \ub85c \ubc14\uafb8\uace0, \uc138\uc0c1\uc5d0 \ubc30\ud3ec\ud574 \ubcf4\uc138\uc694. \uac00\uc7a5 \ube68\ub9ac \ub290\ub294 \uae38\uc740 \uc9c1\uc811 \ub9cc\ub4dc\ub294 \uac83\uc785\ub2c8\ub2e4.<\/p>\n<\/div>\n\n<div class=\"footer-nav\">\n\uc2dc\ub9ac\uc988 \u00b7 <a href=\"https:\/\/junai.ai\/blog\/category\/fastapi\/\">\uc27d\uac8c \ubc30\uc6b0\ub294 FastAPI<\/a> \u00b7 \ud568\uaed8 \ubcf4\uae30: <a href=\"https:\/\/junai.ai\/blog\/category\/python\/\">\ud30c\uc774\uc36c \uae30\ucd08 \uc2dc\ub9ac\uc988<\/a>\n<\/div>\n\n<\/article>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\ubaa8\ub378\u00b7\uac80\uc99d\u00b7\uc778\uc99d\u00b7\ud14c\uc2a4\ud2b8\u00b7\ubc30\ud3ec\ub97c \ud55c \ud504\ub85c\uc81d\ud2b8\uc5d0 \ud569\uce5c\ub2e4. \uc0ac\uc6a9\uc790\ubcc4 \ud560 \uc77c To-Do REST API \ub97c \uc644\uc131\ud558\ub294 \ucea1\uc2a4\ud1a4. \uad50\uc7ac 12\ud3b8 \uc644\uacb0.<\/p>\n","protected":false},"author":1,"featured_media":934,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-947","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-fastapi"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/posts\/947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/comments?post=947"}],"version-history":[{"count":0,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/posts\/947\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/media\/934"}],"wp:attachment":[{"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/media?parent=947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/categories?post=947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/tags?post=947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}