mirror of
https://github.com/LiteyukiStudio/LiteyukiBot.git
synced 2025-07-27 23:50:56 +00:00
🔧 更新 Dockerfile 和 docker-compose 文件,添加配置目录并引入新依赖,重构配置加载功能
Some checks failed
Pytest API Testing / Pytes-API-Testing (push) Failing after 21s
Some checks failed
Pytest API Testing / Pytes-API-Testing (push) Failing after 21s
This commit is contained in:
@ -0,0 +1,3 @@
|
||||
from .daemon import Daemon
|
||||
|
||||
__all__ = ["Daemon"]
|
20
liteyukibot/asgi/__init__.py
Normal file
20
liteyukibot/asgi/__init__.py
Normal file
@ -0,0 +1,20 @@
|
||||
import asyncio
|
||||
|
||||
from fastapi import FastAPI
|
||||
import hypercorn
|
||||
import hypercorn.run
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
return {"message": "Hello LiteyukiBot"}
|
||||
|
||||
|
||||
async def run_app():
|
||||
"""liteyukibot入口函数
|
||||
"""
|
||||
hypercorn.run.serve(app, config=hypercorn.Config.from_mapping(
|
||||
bind=["localhost:8000"],
|
||||
workers=1,
|
||||
))
|
@ -1,10 +1,11 @@
|
||||
|
||||
from typing import Any
|
||||
|
||||
import json
|
||||
import yaml
|
||||
import tomllib
|
||||
|
||||
config: dict[str, Any] = {} # 全局配置map
|
||||
flat_config: dict[str, Any] = {} # 扁平化配置map
|
||||
|
||||
def load_from_yaml(file_path: str) -> dict[str, Any]:
|
||||
"""从yaml文件中加载配置并返回字典
|
||||
@ -41,4 +42,57 @@ def load_from_toml(file_path: str) -> dict[str, Any]:
|
||||
dict[str, Any]: 配置字典
|
||||
"""
|
||||
with open(file_path, "rb") as file:
|
||||
return tomllib.load(file)
|
||||
return tomllib.load(file)
|
||||
|
||||
def merge_to_config(new_config: dict[str, Any], warn: bool=True) -> None:
|
||||
"""加载配置到全局配置字典,该函数有副作用,开发者尽量不要在多份配置文件中使用重复的配置项,否则会被覆盖
|
||||
|
||||
Args:
|
||||
new_config (dict[str, Any]): 新的字典
|
||||
warn (bool, optional): 是否启用重复键警告. 默认 True.
|
||||
"""
|
||||
global config, flat_config
|
||||
config.update(new_config)
|
||||
flat_config = flatten_dict(config)
|
||||
|
||||
def flatten_dict(d: dict[str, Any], parent_key: str = '', sep: str = '.') -> dict[str, Any]:
|
||||
"""将嵌套字典扁平化
|
||||
|
||||
Args:
|
||||
d (dict[str, Any]): 嵌套字典
|
||||
parent_key (str, optional): 父键名. 默认值为 ''
|
||||
sep (str, optional): 分隔符. 默认值为 '.'
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: 扁平化字典
|
||||
|
||||
Example:
|
||||
input_dict = {
|
||||
"server": {
|
||||
"host": "localhost",
|
||||
"port: 8080
|
||||
}
|
||||
}
|
||||
|
||||
output_dict = flatten_dict(input_dict)
|
||||
output_dict = {
|
||||
"server.host": "localhost",
|
||||
"server.port": 8080
|
||||
}
|
||||
"""
|
||||
items: list[tuple[str, Any]] = []
|
||||
for k, v in d.items():
|
||||
new_key = f"{parent_key}{sep}{k}" if parent_key else k
|
||||
if isinstance(v, dict):
|
||||
items.extend(flatten_dict(v, new_key, sep=sep).items())
|
||||
else:
|
||||
items.append((new_key, v))
|
||||
return dict(items)
|
||||
|
||||
def load_config_to_global(reset: bool = False) -> None:
|
||||
"""加载配置到全局配置字典
|
||||
|
||||
Args:
|
||||
reset (bool, optional): 是否重置配置项. 默认 False.
|
||||
"""
|
||||
|
Reference in New Issue
Block a user