feat 🐛:add routes
commit
4fd8443085
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"user_group": [
|
||||||
|
"ceshi"
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
fastapi==0.116.1
|
||||||
|
uvicorn==0.35.0
|
||||||
@ -0,0 +1,38 @@
|
|||||||
|
import os
|
||||||
|
from typing import Union, Dict, List
|
||||||
|
|
||||||
|
from fastapi import APIRouter, HTTPException
|
||||||
|
import json
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
# 读取 JSON 文件
|
||||||
|
class DataLoader:
|
||||||
|
_data = None # 类变量,用于存储加载的 JSON 数据
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def load_json_data(file_path):
|
||||||
|
print(f"current fileop.py path: {file_path}")
|
||||||
|
if DataLoader._data is None:
|
||||||
|
try:
|
||||||
|
with open(file_path, "r") as file:
|
||||||
|
DataLoader._data = json.load(file)
|
||||||
|
except FileNotFoundError:
|
||||||
|
raise HTTPException(status_code=500, detail="File not found")
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
raise HTTPException(status_code=500, detail="Invalid JSON")
|
||||||
|
return DataLoader._data
|
||||||
|
|
||||||
|
|
||||||
|
# 定义一个路由来返回 JSON 数据
|
||||||
|
@router.get("/get_data")
|
||||||
|
async def get_data(
|
||||||
|
) :
|
||||||
|
current_file = os.path.abspath(__file__)
|
||||||
|
current_dir = os.path.dirname(current_file)
|
||||||
|
project_root = os.path.dirname(current_dir)
|
||||||
|
assets_dir = os.path.join(project_root, "assets")
|
||||||
|
config_path = os.path.join(assets_dir, "config.json")
|
||||||
|
data = DataLoader.load_json_data(config_path)
|
||||||
|
return data
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
from fastapi import APIRouter
|
||||||
|
|
||||||
|
# 创建一个 APIRouter 实例。
|
||||||
|
# APIRouter 类似于 FastAPI 实例,但它用于定义一组相关的路由。
|
||||||
|
# 它可以被“包含”到主 FastAPI 应用中。
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
# 定义获取所有 items 的路由
|
||||||
|
@router.get("/items/")
|
||||||
|
async def read_items():
|
||||||
|
"""
|
||||||
|
获取所有 item 的列表。
|
||||||
|
"""
|
||||||
|
return [{"item_id": "Foo", "name": "Foo Bar"}, {"item_id": "Baz", "name": "Baz Qux"}]
|
||||||
|
|
||||||
|
|
||||||
|
# 定义创建一个新 item 的 POST 路由
|
||||||
|
@router.post("/items/")
|
||||||
|
async def create_item():
|
||||||
|
"""
|
||||||
|
创建一个新 item。
|
||||||
|
"""
|
||||||
|
return {"status": "success", "message": "Item created successfully!"}
|
||||||
Loading…
Reference in New Issue