Skip to content

nvminh162-archive/flask-example

Repository files navigation

Flask + MongoDB CRUD Demo

Ứng dụng demo API REST CRUD với Flask và MongoDB Atlas — phục vụ học tập.

Cấu trúc thư mục

flask-example/
├── app/
│   ├── __init__.py          # Application factory
│   ├── extensions.py        # Kết nối MongoDB
│   ├── routes/
│   │   └── student_routes.py
│   ├── services/
│   │   └── student_service.py
│   └── utils/
│       └── serializers.py
├── postman/
│   └── Flask_MongoDB_CRUD.postman_collection.json
├── config.py
├── run.py
├── requirements.txt
└── .env.example

Cài đặt

python -m venv venv
venv\Scripts\activate
pip install -r requirements.txt
copy .env.example .env

Chỉnh file .env với connection string MongoDB của bạn.

Chạy server

python run.py

Server chạy tại: http://localhost:5001

API Endpoints

Method URL Mô tả
GET / Health check
GET /api/students Lấy danh sách
GET /api/students/{id} Lấy theo ID
POST /api/students Tạo mới
PUT /api/students/{id} Cập nhật toàn bộ
PATCH /api/students/{id} Cập nhật một phần
DELETE /api/students/{id} Xóa

Body mẫu (POST / PUT)

{
  "name": "Nguyen Van Minh",
  "email": "minh@example.com",
  "age": 22,
  "major": "Computer Science"
}

Test với Postman

  1. Mở Postman → Import → chọn postman/Flask_MongoDB_CRUD.postman_collection.json
  2. Chạy Create Student trước (request sẽ tự lưu student_id)
  3. Test các request còn lại

Lưu ý bảo mật

  • Không commit file .env lên Git
  • Nếu đã lộ connection string, đổi mật khẩu user trên MongoDB Atlas

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages