feat: 连接挂载elasticsearch
This commit is contained in:
parent
28fc1090a5
commit
e32bb8e740
@ -151,7 +151,7 @@ async def get_captcha(request: Request):
|
|||||||
)
|
)
|
||||||
register_enabled = (
|
register_enabled = (
|
||||||
True
|
True
|
||||||
if await request.app.state.redis.get(f'{RedisKeyConfig.SYSTEM_CONFIG.key}:register_enabled')
|
if await request.app.state.redis.get(f'{RedisKeyConfig.SYSTEM_CONFIG.key}:account_register_enabled')
|
||||||
== 'true'
|
== 'true'
|
||||||
else False
|
else False
|
||||||
)
|
)
|
||||||
|
4
app.py
4
app.py
@ -25,6 +25,7 @@ from api.server import serverAPI
|
|||||||
from api.user import userAPI
|
from api.user import userAPI
|
||||||
from config.database import init_db, close_db
|
from config.database import init_db, close_db
|
||||||
from config.env import AppConfig
|
from config.env import AppConfig
|
||||||
|
from config.get_ElasticSearch import ElasticSearch
|
||||||
from config.get_redis import Redis
|
from config.get_redis import Redis
|
||||||
from exceptions.handle import handle_exception
|
from exceptions.handle import handle_exception
|
||||||
from middlewares.handle import handle_middleware
|
from middlewares.handle import handle_middleware
|
||||||
@ -38,9 +39,12 @@ async def lifespan(app: FastAPI):
|
|||||||
logger.info(f'{AppConfig.app_name}启动成功')
|
logger.info(f'{AppConfig.app_name}启动成功')
|
||||||
await init_db()
|
await init_db()
|
||||||
await Redis.init_system_config(app)
|
await Redis.init_system_config(app)
|
||||||
|
logger.info(f'系统配置初始化成功')
|
||||||
|
app.state.es = await ElasticSearch.init_elasticsearch()
|
||||||
yield
|
yield
|
||||||
await close_db()
|
await close_db()
|
||||||
await Redis.close_redis_pool(app)
|
await Redis.close_redis_pool(app)
|
||||||
|
await app.state.es.close()
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
|
@ -403,6 +403,32 @@ class CachePathConfig:
|
|||||||
PATHSTR = 'caches'
|
PATHSTR = 'caches'
|
||||||
|
|
||||||
|
|
||||||
|
class ElasticSearchSettings(BaseSettings):
|
||||||
|
"""
|
||||||
|
ElasticSearch配置
|
||||||
|
"""
|
||||||
|
ES_HOST: str = '127.0.0.1'
|
||||||
|
"""
|
||||||
|
ElasticSearch 连接路径
|
||||||
|
"""
|
||||||
|
ES_PORT: int = 9200
|
||||||
|
"""
|
||||||
|
ElasticSearch 连接端口
|
||||||
|
"""
|
||||||
|
ES_USER: str = 'elastic'
|
||||||
|
"""
|
||||||
|
ElasticSearch 连接用户名
|
||||||
|
"""
|
||||||
|
ES_PASSWORD: str = 'changeme'
|
||||||
|
"""
|
||||||
|
ElasticSearch 连接密码
|
||||||
|
"""
|
||||||
|
ES_INDEX: str = 'test'
|
||||||
|
"""
|
||||||
|
ElasticSearch 索引名称
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
class GetConfig:
|
class GetConfig:
|
||||||
"""
|
"""
|
||||||
获取配置类,用于集中管理和获取应用的所有配置。
|
获取配置类,用于集中管理和获取应用的所有配置。
|
||||||
@ -484,6 +510,16 @@ class GetConfig:
|
|||||||
# 实例化地图配置
|
# 实例化地图配置
|
||||||
return MapSettings()
|
return MapSettings()
|
||||||
|
|
||||||
|
@lru_cache()
|
||||||
|
def get_elasticsearch_config(self) -> 'ElasticSearchSettings':
|
||||||
|
"""
|
||||||
|
获取 ElasticSearch 配置。
|
||||||
|
- 返回 ElasticSearchConfig 的实例。
|
||||||
|
- 使用 lru_cache 缓存结果,避免重复实例化。
|
||||||
|
"""
|
||||||
|
# 实例化 ElasticSearch 配置
|
||||||
|
return ElasticSearchSettings()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse_cli_args():
|
def parse_cli_args():
|
||||||
"""
|
"""
|
||||||
@ -538,3 +574,6 @@ EmailConfig = get_config.get_email_config()
|
|||||||
|
|
||||||
# 地图配置
|
# 地图配置
|
||||||
MapConfig = get_config.get_map_config()
|
MapConfig = get_config.get_map_config()
|
||||||
|
|
||||||
|
# ElasticSearch 配置
|
||||||
|
ElasticSearchConfig = get_config.get_elasticsearch_config()
|
||||||
|
44
config/get_ElasticSearch.py
Normal file
44
config/get_ElasticSearch.py
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
# _*_ coding : UTF-8 _*_
|
||||||
|
# @Time : 2025/02/13 17:28
|
||||||
|
# @UpdateTime : 2025/02/13 17:28
|
||||||
|
# @Author : sonder
|
||||||
|
# @File : get_ElasticSearch.py
|
||||||
|
# @Software : PyCharm
|
||||||
|
# @Comment : 本程序
|
||||||
|
|
||||||
|
from elasticsearch import AsyncElasticsearch
|
||||||
|
from config.env import ElasticSearchConfig
|
||||||
|
from utils.log import logger
|
||||||
|
|
||||||
|
|
||||||
|
class ElasticSearch:
|
||||||
|
"""
|
||||||
|
ElasticSearch工具类
|
||||||
|
"""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def init_elasticsearch(cls):
|
||||||
|
"""
|
||||||
|
初始化elasticsearch
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
# 创建异步Elasticsearch客户端
|
||||||
|
es = AsyncElasticsearch(
|
||||||
|
hosts=[{
|
||||||
|
"scheme": "http", # 传递scheme
|
||||||
|
"host": ElasticSearchConfig.ES_HOST, # 传递主机名
|
||||||
|
"port": ElasticSearchConfig.ES_PORT, # 传递端口
|
||||||
|
}],
|
||||||
|
http_auth=(ElasticSearchConfig.ES_USER, ElasticSearchConfig.ES_PASSWORD), # 传递http_auth
|
||||||
|
)
|
||||||
|
|
||||||
|
# 检查连接是否成功
|
||||||
|
if await es.ping():
|
||||||
|
logger.success(f"ElasticSearch初始化成功: {ElasticSearchConfig.ES_HOST}:{ElasticSearchConfig.ES_PORT}")
|
||||||
|
else:
|
||||||
|
logger.warning("ElasticSearch初始化失败:无法ping通服务器")
|
||||||
|
es = None
|
||||||
|
return es
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"ElasticSearch初始化失败: {e}")
|
||||||
|
return None
|
Loading…
x
Reference in New Issue
Block a user