File size: 2,575 Bytes
6d1fe81
 
f5cf708
 
 
 
 
 
 
 
 
daa9d8a
f5cf708
d6b9283
6d1fe81
e77218b
 
 
daa9d8a
f5cf708
daa9d8a
 
 
e77218b
f5cf708
 
daa9d8a
 
f5cf708
daa9d8a
 
 
 
 
 
6d1fe81
 
e77218b
 
 
daa9d8a
6d1fe81
daa9d8a
 
 
 
 
 
 
 
 
 
 
 
 
 
f5cf708
daa9d8a
 
 
 
 
 
 
e77218b
 
f5cf708
 
702187d
f5cf708
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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