api-mapper / app.py
tanbushi's picture
modify users, use cf db
f5cf708
raw
history blame
2.58 kB
# uvicorn app:app --host 0.0.0.0 --port 7860 --reload
from pathlib import Path
from global_state import set
# 获取当前文件的父目录的绝对路径,即:project_root
parent_dir = Path(__file__).resolve().parent
set('project_root', parent_dir)
print('\n\n\n======================')
from fastapi import FastAPI, HTTPException, Response, Depends
from fastapi.responses import HTMLResponse
# from starlette.requests import Request
from routers.webtools_v1 import router as webtools_router
from routers.users_v1 import router as users_router
from routers.openai_v1 import router as openai_router
# from db import Db
from dotenv import load_dotenv
import os
print('\n\n\n======================')
# 当在本地使用.env 文件时加载.env文件
load_dotenv()
# 获取环境变量,本地使用 load_dotenv 和 hf 里直接使用环境变量配置时,都可使用下面的语句
DB_PATH = os.getenv("DB_PATH")
# 依赖注入,获取 DB_PATH
def get_db_path():
return DB_PATH
app = FastAPI()
app.include_router(webtools_router, prefix="/airs/v1", tags=["webtools"])
app.include_router(users_router, prefix="/airs/v1", tags=["users"])
app.include_router(openai_router, prefix="/airs/v1", tags=["openai"])
@app.get("/")
def greet_json(db_path: str = Depends(get_db_path)):
return {"Hello": "World!"}
@app.get("/init_db")
def init_db():
sql = """
CREATE TABLE api_keys (
id INTEGER PRIMARY KEY AUTOINCREMENT,
api_key TEXT NOT NULL,
type TEXT NOT NULL,
status INTEGER NOT NULL,
idx INTEGER NOT NULL,
dest_api_key TEXT NOT NULL
);
"""
# Db('api_keys.db').execute_query(sql)
# def create_user_api_key(user_id, api_key, type, status, idx, dest_api_key):
# sql = f"""
# INSERT INTO api_keys (api_key, type, status, idx, dest_api_key)
# """
# Db('api_keys.db').execute_query(sql)
# create_user_api_key('i_am_tanbushi', 'llm', '1', '0', 'dest_api_key')
@app.get("/list_files")
def list_files():
# return "cur_path"
directory = os.getcwd()
# 遍历目录树
retstr="第一行\n第二行\n第三行\n"
# retstr = retstr + '1' + "\n"
# retstr = retstr + '2' + "\n"
# retstr = retstr + '3' + "\n"
print(retstr)
# for dirpath, dirnames, filenames in os.walk(directory):
# for filename in filenames:
# retstr = retstr + os.path.join(dirpath, filename) +'\\n'
# print(os.path.join(dirpath, filename))
return HTMLResponse(f"<pre>{retstr}</pre>")
return HTMLResponse(content=f"<pre>{retstr}</pre>", media_type="text/html")
return retstr