Skip to content

numb2too/dessert-backend

Repository files navigation

甜點後台管理系統-後端

https://disturbing-riki-numb2too-a14e6a03.koyeb.app/docs alt text alt text

Python + Flask/APIFlask 架構

功能

技能

  • 透過 docker 運行 web & mysql
  • web service 執行 rest api
  • mysql database 連接
  • pytest 做 unit test
  • 透過 migrate 更新 mysql 欄位資訊
  • MVC 架構
  • github ci/cd 自動測試
  • 透過 debugpy 在 docker 運行下 使用 vscode 做斷點提升 debug 效率
  • 透過 APIFlask 自動生成 api 文件
  • api error_handlers 統一回傳格式
  • koyeb 自動部署 web server 可參考
  • TiDB 雲端部署 mysql server 可參考

執行

docker compose (推薦)

確認 docker 資訊

docker info

沒有資訊的話先安裝

brew install --cask docker
# install 中途斷掉的話 reinstall
brew reinstall --cask docker

安裝成功確認有 docker info 後 執行 compose up

docker compose up --build

python

# 1. 建立名為 venv 的虛擬環境
python -m venv venv

# 2. 啟動虛擬環境
# Mac/Linux:
source venv/bin/activate
# Windows:
# venv\Scripts\activate

需先安裝

pip install -r requirements.txt

啟動資料庫

docker-compose up -d db
# 確保已啟動虛擬環境並安裝套件
python run.py
# 也可使用
flask run --port 5001

結果:App 跑在 http://localhost:5001。 原理:讀取 .env,透過 127.0.0.1:3306 連到 Docker 資料庫。

docker

docker build -t dessert-api .

可本地測試

docker run --rm \
  -p 5001:8000 \
  --env PORT=8000 \
  --env DATABASE_URL="mysql+pymysql://your_user:your_password@host.docker.internal:3306/your_db" \
  your-container-name 

unit test

簡單確認 API

curl -X POST http://127.0.0.1:5001/api/users/ \
     -H "Content-Type: application/json" \
     -d '{"name":"Amy","email":"amy@test.com"}'

成功會回傳

{
  "email": "amy@test.com",
  "id": 5,
  "name": "Amy",
  "phone": null
}

pytest

python 直接測試 pytest

dessert-backend % pytest
================================================== test session starts ==================================================
platform darwin -- Python 3.13.7, pytest-9.0.1, pluggy-1.6.0
rootdir: /Users/qq/dessert-backend
collected 6 items                                                                                                       

tests/test_user_routes.py ...                                                                                     [ 50%]
tests/test_user_service.py ...                                                                                    [100%]

=================================================== 6 passed in 0.17s =====

使用 docker pytest

dessert-backend % docker-compose exec web pytest                                                        
================================================================ test session starts ================================================================
platform linux -- Python 3.13.9, pytest-9.0.1, pluggy-1.6.0
rootdir: /app
collected 21 items                                                                                                                                  

tests/test_user_routes.py ...........                                                                                                         [ 52%]
tests/test_user_service.py ..........                                                                                                         [100%]

================================================================ 21 passed in 0.11s    

About

Python/Flask/APIFlask 甜點開店系統後台

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors