Đây là một template dự án Flask với cấu trúc tối ưu cho các dự án vừa và nhỏ.
├── app/
│ ├── api/ # API endpoints
│ ├── auth/ # Xác thực và phân quyền
│ ├── dashboard/ # Dashboard UI và routes
│ ├── models/ # Database models
│ ├── services/ # Business logic
│ ├── utils/ # Utility functions
│ ├── templates/ # HTML templates
│ └── static/ # Static files
├── config/ # Configuration files
├── migrations/ # Database migrations
├── tests/ # Test files
├── .env # Environment variables
├── manage.py # Script quản lý dự án
├── requirements.txt # Project dependencies
└── run.py # Application entry point
- Tạo môi trường ảo:
python -m venv venv
source venv/bin/activate # Linux/Mac
venv\Scripts\activate # Windows
- Cài đặt dependencies:
pip install -r requirements.txt
- Tạo file .env và cập nhật các biến môi trường:
cp .env.example .env
- Khởi tạo database:
python manage.py init-db
- Tạo tài khoản admin (tùy chọn):
python manage.py create-admin <username> <password> <email>
python run.py
Ứng dụng sẽ chạy tại http://localhost:5000
Dự án sử dụng file manage.py
để quản lý các tác vụ thường xuyên:
# Khởi tạo database
python manage.py init-db
# Tạo tài khoản admin
python manage.py create-admin <username> <password> <email>
# Liệt kê tất cả người dùng
python manage.py list-users
GET /api/health
: Kiểm tra trạng thái APIGET /api/nodes
: Lấy danh sách các nodesPOST /api/nodes
: Tạo node mớiGET /api/nodes/<id>
: Lấy thông tin chi tiết của một nodePUT /api/nodes/<id>
: Cập nhật thông tin nodeDELETE /api/nodes/<id>
: Xoá node
GET /auth/login
: Hiển thị form đăng nhậpPOST /auth/login
: Xử lý đăng nhậpPOST /auth/register
: Đăng ký tài khoản mớiGET /auth/profile
: Xem thông tin tài khoảnPUT /auth/profile
: Cập nhật thông tin tài khoản
pytest
Dự án sử dụng:
- Factory Pattern cho việc khởi tạo ứng dụng
- Blueprint cho việc tổ chức routes
- SQLAlchemy cho ORM
- Flask-JWT-Extended cho authentication
- Flask-Migrate cho database migrations