Explorar o código

fastapi+minio——finally

clxHardstudy hai 1 ano
pai
achega
5e71ab8351
Modificáronse 8 ficheiros con 23 adicións e 34 borrados
  1. 7 9
      Dockerfile
  2. 1 1
      README.md
  3. 0 0
      app/__init__.py
  4. 4 4
      models/model.py
  5. 5 2
      requirements.txt
  6. 4 17
      routers/files.py
  7. 1 1
      test/test_files.py
  8. 1 0
      utils/jwt.py

+ 7 - 9
Dockerfile

@@ -1,15 +1,13 @@
 FROM python:3.10
 FROM python:3.10
 
 
-WORKDIR /src
+WORKDIR /code
 
 
-COPY ./requirements.txt ./src/requirements.txt
+COPY ./requirements.txt /code2/requirements.txt
 
 
-RUN set -eux \
-    && apk add --no-cache --virtual .build-deps build-base \
-    libressl-dev libffi-dev gcc musl-dev python3-dev \
-    postgresql-dev \
-    && pip install --upgrade pip setuptools wheel \
-    && pip install -r /src/requirements.txt \
+RUN pip install --no-cache-dir --upgrade -r /code2/requirements.txt
 
 
 
 
-COPY . /src/
+COPY . /code/
+
+CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
+

+ 1 - 1
README.md

@@ -7,7 +7,7 @@ A simple example of using Fast API in Python.
 ## Clone the project
 ## Clone the project
 
 
 ```
 ```
-git clone https://github.com/marciovrl/fastapi-example.git
+git clone git@gitee.com:cao-lixu/fastap_minio.git
 ```
 ```
 
 
 ## Run local
 ## Run local

+ 0 - 0
app/__init__.py


+ 4 - 4
models/model.py

@@ -32,10 +32,10 @@ class UserInDB(User):
     hashed_password: str
     hashed_password: str
 
 
 fake_users_db = {
 fake_users_db = {
-    "johndoe": {
-        "username": "johndoe",
-        "full_name": "John Doe",
-        "email": "johndoe@example.com",
+    "clx": {
+        "username": "clx",
+        "full_name": "Lixu Cao",
+        "email": "clx@example.com",
         "hashed_password": "$2b$12$EixZaYVK1fsbw1ZfbX3OXePaWxn96p36WQoeG6Lruj3vjPGga31lW",
         "hashed_password": "$2b$12$EixZaYVK1fsbw1ZfbX3OXePaWxn96p36WQoeG6Lruj3vjPGga31lW",
         "disabled": False,
         "disabled": False,
     }
     }

+ 5 - 2
requirements.txt

@@ -1,6 +1,9 @@
 fastapi
 fastapi
 minio
 minio
 cacheout
 cacheout
-jose
+python-jose
 passlib
 passlib
-pydantic
+uvicorn
+bcrypt
+python-dotenv
+python-multipart

+ 4 - 17
routers/files.py

@@ -4,19 +4,14 @@ import time
 from datetime import timedelta
 from datetime import timedelta
 import datetime
 import datetime
 from io import BytesIO
 from io import BytesIO
-
-import pytest
 from fastapi import Depends, FastAPI, HTTPException, status, UploadFile, APIRouter, File
 from fastapi import Depends, FastAPI, HTTPException, status, UploadFile, APIRouter, File
 from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
 from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
-from httpx import AsyncClient
 from starlette.responses import StreamingResponse, FileResponse
 from starlette.responses import StreamingResponse, FileResponse
-
-from config.config import MinioOperate ,SetCache
+from config.config import MinioOperate, SetCache
 from models.model import *
 from models.model import *
 from utils.jwt import authenticate_user, ACCESS_TOKEN_EXPIRE_MINUTES, create_access_token, get_current_active_user, \
 from utils.jwt import authenticate_user, ACCESS_TOKEN_EXPIRE_MINUTES, create_access_token, get_current_active_user, \
     fake_users_db
     fake_users_db
 
 
-
 # 创建minio对象
 # 创建minio对象
 minio_class = MinioOperate()
 minio_class = MinioOperate()
 # 连接minio
 # 连接minio
@@ -25,10 +20,11 @@ minio_client = minio_class.link_minio()
 minio_class.create_bucket(["file", "image"])
 minio_class.create_bucket(["file", "image"])
 
 
 # 初始化缓存
 # 初始化缓存
-cache = SetCache(maxsize=128,ttl=10)
+cache = SetCache(maxsize=128, ttl=100)
 
 
 router = APIRouter()
 router = APIRouter()
 
 
+
 @router.post("/token", response_model=Token)
 @router.post("/token", response_model=Token)
 async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
 async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
     user = authenticate_user(fake_users_db, form_data.username, form_data.password)
     user = authenticate_user(fake_users_db, form_data.username, form_data.password)
@@ -104,6 +100,7 @@ async def download_file(uid: str):
     # return response
     # return response
     return {"status": 200, "data": [uid], "msg": ""}
     return {"status": 200, "data": [uid], "msg": ""}
 
 
+
 # 删除  鉴权 current_user: User = Depends(get_current_active_user)
 # 删除  鉴权 current_user: User = Depends(get_current_active_user)
 @router.delete("/file/{uid}")
 @router.delete("/file/{uid}")
 async def delete_file(uid: str):
 async def delete_file(uid: str):
@@ -118,13 +115,3 @@ async def delete_file(uid: str):
         return {"status": 200, "data": [], "msg": "Delete Success!"}
         return {"status": 200, "data": [], "msg": "Delete Success!"}
     except:
     except:
         return {"status": 404, "data": [], "msg": "Not Found"}
         return {"status": 404, "data": [], "msg": "Not Found"}
-
-
-
-
-
-
-
-
-
-

+ 1 - 1
test/test_files.py

@@ -264,7 +264,7 @@ async def delete_file(uid: str):
         return {"status": 404, "data": [], "msg": "Not Found"}
         return {"status": 404, "data": [], "msg": "Not Found"}
 
 
 
 
-client = TestClient(app)
+client = TestClient()
 
 
 
 
 
 

+ 1 - 0
utils/jwt.py

@@ -13,6 +13,7 @@ load_dotenv()
 SECRET_KEY = os.getenv("SECRET_KEY")
 SECRET_KEY = os.getenv("SECRET_KEY")
 ALGORITHM = os.getenv("ALGORITHM")
 ALGORITHM = os.getenv("ALGORITHM")
 ACCESS_TOKEN_EXPIRE_MINUTES = os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES")
 ACCESS_TOKEN_EXPIRE_MINUTES = os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES")
+print(SECRET_KEY)
 
 
 # schemes 加密方式,默认第一个
 # schemes 加密方式,默认第一个
 pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
 pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")