From 4fd84430856911a96c5568ae2002ef48e6f3a66d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=A0=87?= Date: Tue, 29 Jul 2025 15:24:49 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=F0=9F=90=9B:add=20routes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.MD | 1 + assets/config.json | 5 +++++ main.py | 18 ++++++++++++++++++ requirements.txt | 2 ++ routes/__init__.py | 0 routes/config.py | 38 ++++++++++++++++++++++++++++++++++++++ routes/sync_message.py | 23 +++++++++++++++++++++++ 7 files changed, 87 insertions(+) create mode 100644 README.MD create mode 100644 assets/config.json create mode 100644 main.py create mode 100644 requirements.txt create mode 100644 routes/__init__.py create mode 100644 routes/config.py create mode 100644 routes/sync_message.py diff --git a/README.MD b/README.MD new file mode 100644 index 0000000..7eca38d --- /dev/null +++ b/README.MD @@ -0,0 +1 @@ +conda activate report-message \ No newline at end of file diff --git a/assets/config.json b/assets/config.json new file mode 100644 index 0000000..c30d7b4 --- /dev/null +++ b/assets/config.json @@ -0,0 +1,5 @@ +{ + "user_group": [ + "ceshi" + ] +} \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..e15906b --- /dev/null +++ b/main.py @@ -0,0 +1,18 @@ +import uvicorn +from fastapi import FastAPI +from routes import sync_message, config + +app = FastAPI() + +app.include_router(sync_message.router, prefix="/api/v1", tags=["SyncMessage"]) +app.include_router(config.router, prefix="/api/v1", tags=["config"]) + +@app.get("/") +async def read_root(): + """ + 根路径路由,欢迎信息。 + """ + return {"message": "Welcome to the FastAPI Application!"} + +if __name__ == "__main__": + uvicorn.run("main:app", host="0.0.0.0", port=18000, reload=False) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..09be09c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +fastapi==0.116.1 +uvicorn==0.35.0 \ No newline at end of file diff --git a/routes/__init__.py b/routes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/routes/config.py b/routes/config.py new file mode 100644 index 0000000..7e3dab6 --- /dev/null +++ b/routes/config.py @@ -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 diff --git a/routes/sync_message.py b/routes/sync_message.py new file mode 100644 index 0000000..41083cb --- /dev/null +++ b/routes/sync_message.py @@ -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!"} \ No newline at end of file