This commit is contained in:
kingChan 2024-08-20 15:40:31 +08:00
parent 953d9f3376
commit a7f80d1478
4 changed files with 65 additions and 4 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea

View File

@ -3,9 +3,13 @@ import os
class Config:
# SECRET_KEY = os.environ.get('SECRET_KEY') or 'your_secret_key'
# SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'postgresql://username:password@localhost:5432/yourdbname'
# SQLALCHEMY_TRACK_MODIFICATIONS = False
SECRET_KEY = '8665e31a29ab7e12cb0f92c1dbobj1e3a6a15230fb17'
SQLALCHEMY_DATABASE_URI = 'postgresql://postgres:postgres@db:5432/mini12306_python'
# SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or
# 'postgresql://username:password@localhost:5432/your dbname'
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or ('postgresql://postgres:123456@localhost:54321'
'/mini12306_python')
SQLALCHEMY_TRACK_MODIFICATIONS = False

View File

@ -5,6 +5,9 @@ services:
context: ./
dockerfile: ./Dockerfile
restart: always
environment:
- DATABASE_URL=postgresql://postgres:postgres@db:5432/mini12306_python
- REDIS_URL=redis://redis:6379/0
ports:
- "3002:3002"
depends_on:

View File

@ -1,4 +1,53 @@
### 创建迁移脚本
### 环境安装
1. 安装python 3.12以上版本
2. 使用 pip 安装项目所需要的包
```
pip config set global.index-url http://mirrors.aliyun.com/pypi/simple
pip config set install.trusted-host mirrors.aliyun.com
pip install -r requirements.txt
```
3. 安装 postgresql并创建数据库
4. 执行迁移
```
flask db upgrade
```
5. 运行项目
```
flask run
```
也可以使用
```
gunicorn --bind 0.0.0.0:5000 app:app // windows环境上无法运行该指令
```
### docker部署
1. 找一台服务器安装docker环境
2. 使用镜像导入功能导入镜像如不导入镜像则docker会自动去下载由于国内环境问题大多数情况都无法进行自动下载
```
docker load > ./docker-images/postgres_15.tar
docker load > ./docker-images/redis_alpine.tar
```
3. 构建容器
```
docker-compose build
```
4. 运行容器
```
docker-compose up -d
```
---
### Flask-Migrate 使用方法
首先,初始化 Alembic 目录migrations/ 文件夹):
```
flask db init
@ -8,11 +57,15 @@ flask db init
```
flask db migrate -m "Initial migration"
```
这将根据你定义的模型生成迁移脚本。
执行迁移
```
flask db upgrade
```
断点调试
---
### 断点调试
```
pdb.set_trace()
```