feat: docker本地调试:

- 1. import不同系统语法不一致
- 2. mysql允许远程访问
This commit is contained in:
eric
2022-12-18 21:12:02 +08:00
parent 4272acb677
commit 1423965912
25 changed files with 106 additions and 28 deletions

View File

@@ -0,0 +1,12 @@
{
"containers": [
{
"name": "fastapiweb",
"domain": "",
"ip": "",
"mode": "compose",
"containerId": "2eff8c8bd0d2909a8b806e8c12acc9fa0362494c45ea739b23c9c66cf59312b0"
}
],
"config": {}
}

View File

@@ -1,5 +1,5 @@
# 1、从官方 Python 基础镜像开始
FROM python:3.9
FROM python:3.10
# 2、将当前工作目录设置为 /code
# 这是放置 requirements.txt 文件和应用程序目录的地方
@@ -13,7 +13,7 @@ COPY ./requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
# 5、复制 FastAPI 项目代码
COPY ./app /code/app
COPY ./cloudcontainers/app /code/app
# 6、运行服务
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
CMD ["uvicorn", "cloudcontainers.app.main:app", "--host", "0.0.0.0", "--port", "80"]

10
Dockerfile.development Normal file
View File

@@ -0,0 +1,10 @@
# Auto-generated by weixin cloudbase vscode extension
FROM ccr.ccs.tencentyun.com/weixincloud/wxcloud-livecoding-toolkit:latest AS toolkit
FROM python:3.10
COPY --from=toolkit nodemon /usr/bin/nodemon
WORKDIR /code
COPY ./requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
COPY ./cloudcontainers/app /code/app
CMD [ "nodemon", "-x", "uvicorn cloudcontainers.app.main:app --host 0.0.0.0 --port 80", "-w", "/code", "-e", "java, js, mjs, json, ts, cs, py, go" ]

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,13 +0,0 @@
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db"
# SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db"
engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -2,7 +2,7 @@ from sqlalchemy.orm import Session
from sqlalchemy import select
import models, schemas
from . import models, schemas
# 删除

View File

@@ -0,0 +1,15 @@
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import pymysql
# SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db"
# 从docker访问ip需要 mysql服务端允许外部访问=> 参考地址:https://blog.csdn.net/weixin_42599091/article/details/125224850
SQLALCHEMY_DATABASE_URL = "mysql+pymysql://root:Xiao4669805@192.168.31.245:3306/test"
# SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} #sqlite
engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()

View File

@@ -7,8 +7,8 @@ from pydantic import BaseModel
from sqlalchemy.orm import Session
import crud, models, schemas
from database import SessionLocal, engine
from . import crud, models, schemas
from .database import SessionLocal, engine
models.Base.metadata.create_all(bind=engine)
@@ -73,6 +73,18 @@ async def root():
print('e')
return {"message": "触发飞书webhook"}
# 飞书通知
@app.post("/nihao")
async def root():
headers = {'Content-Type': 'application/json'}
# https://open.feishu.cn/open-apis/bot/v2/hook/5fb1b44c-3446-42ef-9b3a-dd37731567dd
try:
r = requests.post('https://open.feishu.cn/open-apis/bot/v2/hook/5fb1b44c-3446-42ef-9b3a-dd37731567dd',
data=json.dumps({'msg_type': 'text', "content": {"text": "cityNew有新用户注册"}}), headers=headers)
except(e):
print('e')
return {"message": "nihao"}
# post请求
@app.post("/testPost")
async def root():

View File

@@ -1,15 +1,15 @@
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship
from database import Base
from .database import Base
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
email = Column(String, unique=True, index=True)
hashed_password = Column(String)
email = Column(String(1000), unique=True, index=True)
hashed_password = Column(String(1000))
is_active = Column(Boolean, default=True)
items = relationship("Item", back_populates="owner")
@@ -19,8 +19,8 @@ class Item(Base):
__tablename__ = "items"
id = Column(Integer, primary_key=True, index=True)
title = Column(String, index=True)
description = Column(String, index=True)
title = Column(String(1000), index=True)
description = Column(String(1000), index=True)
owner_id = Column(Integer, ForeignKey("users.id"))
owner = relationship("User", back_populates="items")

24
docker-compose.yml Normal file
View File

@@ -0,0 +1,24 @@
version: '3'
services:
app:
build:
context: .
dockerfile: Dockerfile.development
volumes:
- .:/code
ports:
- '27081:80'
container_name: wxcloud_fastapiweb
labels:
- wxPort=27083
- hostPort=27081
- wxcloud=fastapiweb
- role=container
environment:
- MYSQL_USERNAME=
- MYSQL_PASSWORD=
- MYSQL_ADDRESS=
networks:
default:
external:
name: wxcb0

View File

@@ -12,18 +12,25 @@ cffi==1.15.0
charset-normalizer==2.0.12
click==8.1.3
cryptography==37.0.1
databases==0.6.2
distlib==0.3.6
dnspython==2.2.1
email-validator==1.1.3
fastapi==0.75.2
fastapi-crudrouter==0.8.5
fastapi-users==9.3.1
fastapi-users-db-sqlalchemy==3.0.1
fastapi-users==10.2.1
fastapi-users-db-sqlalchemy==4.0.3
filelock==3.8.2
future==0.18.2
greenlet==1.1.2
grpclib==0.4.2
h11==0.13.0
h11==0.12.0
h2==4.1.0
hpack==4.0.0
httpcore==0.14.7
httptools==0.5.0
httpx==0.22.0
httpx-oauth==0.6.0
hypercorn==0.13.2
hyperframe==6.0.1
idna==3.3
@@ -39,6 +46,8 @@ opengraph-py3==0.71
passlib==1.7.4
pikachuwechat==0.1.2
ping3==4.0.3
pipenv==2022.11.30
platformdirs==2.6.0
prettytable==3.3.0
priority==2.0.0
pycodestyle==2.10.0
@@ -46,13 +55,18 @@ pycparser==2.21
pydantic==1.9.0
pyecharts==1.9.1
pyee==9.0.4
PyJWT==2.3.0
PyJWT==2.6.0
PyMySQL==1.0.2
pypng==0.0.21
PyQRCode==1.2.1
python-dotenv==0.21.0
python-multipart==0.0.5
PyYAML==6.0
qrcode==7.3.1
quart==0.17.0
requests==2.27.1
rfc3986==1.5.0
schedule==1.1.0
simplejson==3.17.6
six==1.16.0
sniffio==1.2.0
@@ -65,6 +79,10 @@ tomli==2.0.1
typing_extensions==4.2.0
urllib3==1.26.9
uvicorn==0.17.6
uvloop==0.17.0
virtualenv==20.17.1
virtualenv-clone==0.5.7
watchgod==0.8.2
wcwidth==0.2.5
websockets==10.3
wechaty==0.8.39