- 백엔드, db에 대해 이해해 보고싶어서 FastApi 간단하게 공부해보았다.
from fastapi import FastAPI
from typing import Optional
from pydantic import BaseModel
app = FastAPI()
@app.get('/blog')
def index(limit = 10, published: bool = True, sort: Optional[str] = None):
if published:
return { 'data': f'{limit} published: {published} blogs from the db'}
else:
return { 'data': f'{limit} published: {published} blogs from the db'}
@app.get('/blog/unpublished')
def unpublished():
return {'data': 'all unpublished blogs'}
@app.get('/blog/{id}')
def show(id: int):
return {'data': id}
@app.get('/blog/{id}/comments')
def comments(id, limit = 10):
return {'data': {'1', '2'}}
class Blog(BaseModel):
title: str
body: str
published: Optional[bool]
@app.post('/blog')
def create_blog(blog: Blog):
return {'data': f"Blog is created with title as {blog.title}"}
YouTube-tutorial