|
@@ -1,59 +0,0 @@
|
|
-import os
|
|
|
|
-import sys
|
|
|
|
-import time
|
|
|
|
-from pathlib import Path
|
|
|
|
-
|
|
|
|
-import uvicorn
|
|
|
|
-import subprocess
|
|
|
|
-
|
|
|
|
-from fastapi import FastAPI, UploadFile, File, Form
|
|
|
|
-from fastapi import Request
|
|
|
|
-from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
-from starlette.middleware.sessions import SessionMiddleware
|
|
|
|
-from starlette.responses import JSONResponse, RedirectResponse, FileResponse
|
|
|
|
-
|
|
|
|
-# =============== 基础配置 ===============
|
|
|
|
-
|
|
|
|
-app = FastAPI(title="project name ", description="通用系统 ", version="v 0.0.0")
|
|
|
|
-
|
|
|
|
-# 添加 session 中间键,使项目中可以使用session
|
|
|
|
-app.add_middleware(SessionMiddleware, secret_key='123456hhh')
|
|
|
|
-app.add_middleware(
|
|
|
|
- CORSMiddleware,
|
|
|
|
- allow_origins=["*", ],
|
|
|
|
- allow_credentials=True,
|
|
|
|
- allow_methods=["*"],
|
|
|
|
- allow_headers=["*"],
|
|
|
|
-)
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-@app.exception_handler(Exception)
|
|
|
|
-async def validation_exception_handler(request, exc):
|
|
|
|
- """请求校验异常捕获; application/json """
|
|
|
|
- return JSONResponse({'message': "服务器内部错误", 'status_code': 500})
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-@app.middleware("http")
|
|
|
|
-async def add_process_time_header(request: Request, call_next):
|
|
|
|
- """接口响应中间键; 当前只支持 http 请求"""
|
|
|
|
- start_time = time.time()
|
|
|
|
- response = await call_next(request)
|
|
|
|
- process_time = time.time() - start_time
|
|
|
|
- response.headers["API-Process-Time"] = f"{process_time} seconds"
|
|
|
|
- return response
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-# =============== 代码 ===============
|
|
|
|
-
|
|
|
|
-@app.get("/getDataFromJson")
|
|
|
|
-def get_data_from_json():
|
|
|
|
- # 读取本地data.json,获取一级列表
|
|
|
|
- import json
|
|
|
|
- with open('data.json', 'r') as f:
|
|
|
|
- data = json.load(f)
|
|
|
|
- return data
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-if __name__ == '__main__':
|
|
|
|
- # uvicorn call_sh:app --reload --port 19999
|
|
|
|
- uvicorn.run(app='main:app', host="0.0.0.0", port=9999, reload=True)
|
|
|