用 FastAPI + SQLite 搭建极简博客

用 FastAPI + SQLite 搭建极简博客

用 FastAPI + SQLite 搭建极简博客

在这个教程中,我们将使用 FastAPI 和 SQLite 从零搭建一个轻量级博客系统。

为什么选择 FastAPI?

FastAPI 是目前最快的 Python Web 框架之一,它基于 Starlette 和 Pydantic,天然支持异步编程和自动 API 文档生成。

技术栈

  • 后端: FastAPI + SQLAlchemy (async)
  • 数据库: SQLite + aiosqlite
  • 模板引擎: Jinja2
  • 部署: Nginx + systemd

快速开始

from fastapi import FastAPI
from fastapi.templating import Jinja2Templates

app = FastAPI()
templates = Jinja2Templates(directory="templates")

@app.get("/")
async def home(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})

部署到服务器

使用 uvicorn 作为 ASGI 服务器,配合 systemd 实现开机自启:

uvicorn app.main:app --host 127.0.0.1 --port 8000

总结

FastAPI + SQLite 的组合非常适合个人博客这种轻量级应用,部署简单,性能优秀。

评论 (0)

发表评论