C.W.K.
Stream
Lesson 07 of 10 · published

FastAPI + aiosqlite 아키텍처

~14 min · fastapi, architecture, real-world

Level 0Scout
0 XP0/80 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

진짜 제품으로 scale 하는 end-to-end

조각 합치기: aiosqlite 쓰는 진짜 FastAPI 서비스가 명확한 5 레이어로 끝남.

  1. Lifespan — startup 에 store open, shutdown 에 close.
  2. Store — aiosqlite wrap CRUD class. SQL 사는 유일한 자리.
  3. Routes — FastAPI endpoint. Depends 로 store inject, method 호출, Pydantic model 반환.
  4. Models — request/response shape Pydantic schema.
  5. Background task — long-running (embedding 생성, 인덱싱) 은 asyncio.create_task 또는 job queue, request/response path X.
Self-reference: 피파 backend 가 정확히 이 모양: main.py 가 lifespan, store/conversations.py 가 store, routes/chat.py 가 route, routes/models.py-style 파일이 Pydantic schema, heartbeat scheduler 가 long-lived 백그라운드 task.

Code

SQL 아닌 store 의지하는 route·python
from fastapi import FastAPI, Depends, HTTPException, Request
from pydantic import BaseModel

class MessageIn(BaseModel):
    role: str
    content: str

class MessageOut(BaseModel):
    id: int
    created_at: str

async def store(request: Request) -> 'ConversationStore':
    return request.app.state.store

@app.post('/conversations/{cid}/messages', response_model=MessageOut)
async def post_message(
    cid: int,
    body: MessageIn,
    store: 'ConversationStore' = Depends(store),
):
    return await store.add_message(cid, body.role, body.content)

@app.get('/conversations/{cid}/messages')
async def list_messages(
    cid: int, store: 'ConversationStore' = Depends(store),
):
    return await store.messages_for(cid)

External links

Exercise

Mini-Pippa 빌드: FastAPI + aiosqlite + ConversationStore + endpoint 4 개 (conversation create, conversation list, message post, message list). Lifespan, dependency, Pydantic model wire. Read+write 모두 hit 하는 load test (hey -n 1000 -c 50). Latency 유지 확인.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.