Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
42b1751cdd | ||
|
|
1423965912 | ||
|
|
4272acb677 | ||
|
|
9f901a46ca | ||
|
|
b5f0ae2658 | ||
|
|
22a766d0ad |
12
.cloudbase/container/debug.json
Normal file
12
.cloudbase/container/debug.json
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"containers": [
|
||||||
|
{
|
||||||
|
"name": "fastapiweb",
|
||||||
|
"domain": "",
|
||||||
|
"ip": "",
|
||||||
|
"mode": "compose",
|
||||||
|
"containerId": "2eff8c8bd0d2909a8b806e8c12acc9fa0362494c45ea739b23c9c66cf59312b0"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"config": {}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
# 1、从官方 Python 基础镜像开始
|
# 1、从官方 Python 基础镜像开始
|
||||||
FROM python:3.9
|
FROM python:3.10
|
||||||
|
|
||||||
# 2、将当前工作目录设置为 /code
|
# 2、将当前工作目录设置为 /code
|
||||||
# 这是放置 requirements.txt 文件和应用程序目录的地方
|
# 这是放置 requirements.txt 文件和应用程序目录的地方
|
||||||
@@ -13,7 +13,7 @@ COPY ./requirements.txt /code/requirements.txt
|
|||||||
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
||||||
|
|
||||||
# 5、复制 FastAPI 项目代码
|
# 5、复制 FastAPI 项目代码
|
||||||
COPY ./app /code/app
|
COPY ./cloudcontainers/app /code/app
|
||||||
|
|
||||||
# 6、运行服务
|
# 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
10
Dockerfile.development
Normal 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.
@@ -1,8 +0,0 @@
|
|||||||
from fastapi import FastAPI
|
|
||||||
|
|
||||||
app = FastAPI()
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
|
||||||
async def root():
|
|
||||||
return {"message": "Hello World"}
|
|
||||||
0
cloudcontainers/__init__.py
Normal file
0
cloudcontainers/__init__.py
Normal file
BIN
cloudcontainers/__pycache__/__init__.cpython-310.pyc
Normal file
BIN
cloudcontainers/__pycache__/__init__.cpython-310.pyc
Normal file
Binary file not shown.
0
cloudcontainers/app/__init__.py
Normal file
0
cloudcontainers/app/__init__.py
Normal file
BIN
cloudcontainers/app/__pycache__/crud.cpython-310.pyc
Normal file
BIN
cloudcontainers/app/__pycache__/crud.cpython-310.pyc
Normal file
Binary file not shown.
BIN
cloudcontainers/app/__pycache__/database.cpython-310.pyc
Normal file
BIN
cloudcontainers/app/__pycache__/database.cpython-310.pyc
Normal file
Binary file not shown.
BIN
cloudcontainers/app/__pycache__/main.cpython-310.pyc
Normal file
BIN
cloudcontainers/app/__pycache__/main.cpython-310.pyc
Normal file
Binary file not shown.
BIN
cloudcontainers/app/__pycache__/models.cpython-310.pyc
Normal file
BIN
cloudcontainers/app/__pycache__/models.cpython-310.pyc
Normal file
Binary file not shown.
BIN
cloudcontainers/app/__pycache__/schemas.cpython-310.pyc
Normal file
BIN
cloudcontainers/app/__pycache__/schemas.cpython-310.pyc
Normal file
Binary file not shown.
56
cloudcontainers/app/crud.py
Normal file
56
cloudcontainers/app/crud.py
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from sqlalchemy import select
|
||||||
|
|
||||||
|
|
||||||
|
from . import models, schemas
|
||||||
|
|
||||||
|
|
||||||
|
# 删除
|
||||||
|
def delete_user(db: Session, user_id: int):
|
||||||
|
db.query(models.User).where(models.User.id == user_id).delete()
|
||||||
|
db.commit()
|
||||||
|
# db.refresh(db_user)
|
||||||
|
|
||||||
|
|
||||||
|
# 更新
|
||||||
|
def update_user(db: Session, user_id: int):
|
||||||
|
db_test=db.query(models.User).filter(models.User.id == user_id).update({"email":'111'},synchronize_session=False)
|
||||||
|
db.commit()
|
||||||
|
# db_test=db.query(models.User).filter(models.User.id == user_id).first()
|
||||||
|
# db_test.email='12345@qq.com'
|
||||||
|
# db.commit()
|
||||||
|
|
||||||
|
|
||||||
|
# 查询
|
||||||
|
def get_user(db: Session, user_id: int):
|
||||||
|
return db.query(models.User).filter(models.User.id == user_id).first()
|
||||||
|
|
||||||
|
|
||||||
|
def get_user_by_email(db: Session, email: str):
|
||||||
|
return db.query(models.User).filter(models.User.email == email).first()
|
||||||
|
|
||||||
|
|
||||||
|
def get_users(db: Session, skip: int = 0, limit: int = 100):
|
||||||
|
return db.query(models.User).offset(skip).limit(limit).all()
|
||||||
|
|
||||||
|
# 新增
|
||||||
|
def create_user(db: Session, user: schemas.UserCreate):
|
||||||
|
|
||||||
|
fake_hashed_password = user.password + "notreallyhashed"
|
||||||
|
db_user = models.User(email=user.email, hashed_password=fake_hashed_password,is_active=False)
|
||||||
|
db.add(db_user)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_user)
|
||||||
|
return db_user
|
||||||
|
|
||||||
|
|
||||||
|
def get_items(db: Session, skip: int = 0, limit: int = 100):
|
||||||
|
return db.query(models.Item).offset(skip).limit(limit).all()
|
||||||
|
|
||||||
|
|
||||||
|
def create_user_item(db: Session, item: schemas.ItemCreate, user_id: int):
|
||||||
|
db_item = models.Item(**item.dict(), owner_id=user_id)
|
||||||
|
db.add(db_item)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_item)
|
||||||
|
return db_item
|
||||||
17
cloudcontainers/app/database.py
Normal file
17
cloudcontainers/app/database.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
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()
|
||||||
114
cloudcontainers/app/main.py
Normal file
114
cloudcontainers/app/main.py
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
from fastapi import FastAPI,Header,Depends,HTTPException
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
|
from enum import Enum
|
||||||
|
from typing import Union
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from . import crud, models, schemas
|
||||||
|
from .database import SessionLocal, engine
|
||||||
|
models.Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
# Dependency
|
||||||
|
def get_db():
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
yield db
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
class User(BaseModel):
|
||||||
|
user_id: str
|
||||||
|
|
||||||
|
class ModelName(str, Enum):
|
||||||
|
alexnet = "alexnet"
|
||||||
|
resnet = "resnet"
|
||||||
|
lenet = "lenet"
|
||||||
|
|
||||||
|
class Item(BaseModel):
|
||||||
|
name: str
|
||||||
|
description: Union[str, None] = None
|
||||||
|
price: float
|
||||||
|
tax: Union[float, None] = None
|
||||||
|
|
||||||
|
@app.post("/delete_user")
|
||||||
|
async def delete(user:User,db: Session = Depends(get_db)):
|
||||||
|
db_user = crud.delete_user(db,user_id=user.user_id)
|
||||||
|
# print('db_user'+db_user)
|
||||||
|
return {"message": "Hello delete_user"}
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/update_user")
|
||||||
|
async def update(user:User,db: Session = Depends(get_db)):
|
||||||
|
db_user = crud.update_user(db,user_id=user.user_id)
|
||||||
|
return {"message": "Hellupdate_usero World"}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
async def root():
|
||||||
|
return {"message": "Hello World"}
|
||||||
|
|
||||||
|
@app.post("/users/", response_model=schemas.User)
|
||||||
|
def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
|
||||||
|
db_user = crud.get_user_by_email(db, email=user.email)
|
||||||
|
if db_user:
|
||||||
|
raise HTTPException(status_code=400, detail="Email already registered")
|
||||||
|
return crud.create_user(db=db, user=user)
|
||||||
|
|
||||||
|
# 飞书通知
|
||||||
|
@app.get("/feishu")
|
||||||
|
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": "触发飞书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():
|
||||||
|
return {"message": "test-post"}
|
||||||
|
|
||||||
|
# 路径参数
|
||||||
|
@app.get("/path/{pathName}")
|
||||||
|
async def root(pathName:ModelName):
|
||||||
|
return {"message": pathName}
|
||||||
|
|
||||||
|
# 查询参数
|
||||||
|
@app.get("/items/")
|
||||||
|
async def read_item(skip: int = 0, limit: Union[int, None] = None):
|
||||||
|
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
|
||||||
|
# return fake_items_db[skip : skip + limit]
|
||||||
|
return f'{skip}+{limit}'
|
||||||
|
|
||||||
|
# 请求体
|
||||||
|
@app.post("/postitems/")
|
||||||
|
async def create_item(item: Item):
|
||||||
|
return item
|
||||||
|
|
||||||
|
|
||||||
|
# header参数
|
||||||
|
@app.get("/items/header")
|
||||||
|
async def read_items(user_agent: Union[str, None] = Header(default=None)):
|
||||||
|
return {"User-Agent": user_agent}
|
||||||
26
cloudcontainers/app/models.py
Normal file
26
cloudcontainers/app/models.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
|
||||||
|
from sqlalchemy.orm import relationship
|
||||||
|
|
||||||
|
from .database import Base
|
||||||
|
|
||||||
|
|
||||||
|
class User(Base):
|
||||||
|
__tablename__ = "users"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
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")
|
||||||
|
|
||||||
|
|
||||||
|
class Item(Base):
|
||||||
|
__tablename__ = "items"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, 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")
|
||||||
35
cloudcontainers/app/schemas.py
Normal file
35
cloudcontainers/app/schemas.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class ItemBase(BaseModel):
|
||||||
|
title: str
|
||||||
|
description: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class ItemCreate(ItemBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Item(ItemBase):
|
||||||
|
id: int
|
||||||
|
owner_id: int
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
||||||
|
|
||||||
|
|
||||||
|
class UserBase(BaseModel):
|
||||||
|
email: str
|
||||||
|
|
||||||
|
|
||||||
|
class UserCreate(UserBase):
|
||||||
|
password: str
|
||||||
|
|
||||||
|
|
||||||
|
class User(UserBase):
|
||||||
|
id: int
|
||||||
|
is_active: bool
|
||||||
|
items: list[Item] = []
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
||||||
BIN
cloudcontainers/app/sql_app.db
Normal file
BIN
cloudcontainers/app/sql_app.db
Normal file
Binary file not shown.
24
docker-compose.yml
Normal file
24
docker-compose.yml
Normal 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
|
||||||
@@ -12,18 +12,25 @@ cffi==1.15.0
|
|||||||
charset-normalizer==2.0.12
|
charset-normalizer==2.0.12
|
||||||
click==8.1.3
|
click==8.1.3
|
||||||
cryptography==37.0.1
|
cryptography==37.0.1
|
||||||
|
databases==0.6.2
|
||||||
|
distlib==0.3.6
|
||||||
dnspython==2.2.1
|
dnspython==2.2.1
|
||||||
email-validator==1.1.3
|
email-validator==1.1.3
|
||||||
fastapi==0.75.2
|
fastapi==0.75.2
|
||||||
fastapi-crudrouter==0.8.5
|
fastapi-crudrouter==0.8.5
|
||||||
fastapi-users==9.3.1
|
fastapi-users==10.2.1
|
||||||
fastapi-users-db-sqlalchemy==3.0.1
|
fastapi-users-db-sqlalchemy==4.0.3
|
||||||
|
filelock==3.8.2
|
||||||
future==0.18.2
|
future==0.18.2
|
||||||
greenlet==1.1.2
|
greenlet==1.1.2
|
||||||
grpclib==0.4.2
|
grpclib==0.4.2
|
||||||
h11==0.13.0
|
h11==0.12.0
|
||||||
h2==4.1.0
|
h2==4.1.0
|
||||||
hpack==4.0.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
|
hypercorn==0.13.2
|
||||||
hyperframe==6.0.1
|
hyperframe==6.0.1
|
||||||
idna==3.3
|
idna==3.3
|
||||||
@@ -39,6 +46,8 @@ opengraph-py3==0.71
|
|||||||
passlib==1.7.4
|
passlib==1.7.4
|
||||||
pikachuwechat==0.1.2
|
pikachuwechat==0.1.2
|
||||||
ping3==4.0.3
|
ping3==4.0.3
|
||||||
|
pipenv==2022.11.30
|
||||||
|
platformdirs==2.6.0
|
||||||
prettytable==3.3.0
|
prettytable==3.3.0
|
||||||
priority==2.0.0
|
priority==2.0.0
|
||||||
pycodestyle==2.10.0
|
pycodestyle==2.10.0
|
||||||
@@ -46,13 +55,18 @@ pycparser==2.21
|
|||||||
pydantic==1.9.0
|
pydantic==1.9.0
|
||||||
pyecharts==1.9.1
|
pyecharts==1.9.1
|
||||||
pyee==9.0.4
|
pyee==9.0.4
|
||||||
PyJWT==2.3.0
|
PyJWT==2.6.0
|
||||||
|
PyMySQL==1.0.2
|
||||||
pypng==0.0.21
|
pypng==0.0.21
|
||||||
PyQRCode==1.2.1
|
PyQRCode==1.2.1
|
||||||
|
python-dotenv==0.21.0
|
||||||
python-multipart==0.0.5
|
python-multipart==0.0.5
|
||||||
|
PyYAML==6.0
|
||||||
qrcode==7.3.1
|
qrcode==7.3.1
|
||||||
quart==0.17.0
|
quart==0.17.0
|
||||||
requests==2.27.1
|
requests==2.27.1
|
||||||
|
rfc3986==1.5.0
|
||||||
|
schedule==1.1.0
|
||||||
simplejson==3.17.6
|
simplejson==3.17.6
|
||||||
six==1.16.0
|
six==1.16.0
|
||||||
sniffio==1.2.0
|
sniffio==1.2.0
|
||||||
@@ -65,6 +79,10 @@ tomli==2.0.1
|
|||||||
typing_extensions==4.2.0
|
typing_extensions==4.2.0
|
||||||
urllib3==1.26.9
|
urllib3==1.26.9
|
||||||
uvicorn==0.17.6
|
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
|
wcwidth==0.2.5
|
||||||
websockets==10.3
|
websockets==10.3
|
||||||
wechaty==0.8.39
|
wechaty==0.8.39
|
||||||
|
|||||||
Reference in New Issue
Block a user