Commit 4e2dfd87 authored by 崔为之's avatar 崔为之 💪🏽

Update project

parent e520934a
...@@ -73,29 +73,42 @@ class FlaskElasticsearch: ...@@ -73,29 +73,42 @@ class FlaskElasticsearch:
return getattr(ctx.elasticsearch, item) return getattr(ctx.elasticsearch, item)
@staticmethod @staticmethod
def _get_config(): def get_es_config() -> dict:
"""Retrieves Elasticsearch configuration from the current Flask application context.""" """Get Elasticsearch configuration from the current Flask application context."""
with current_app.app_context(): with current_app.app_context():
if current_app: if current_app:
config_helper = ConfigHelper(current_app) config_helper = ConfigHelper(current_app)
cfg = config_helper.Elasticsearch return config_helper.Elasticsearch
host = cfg.Host or 'localhost'
port = int(cfg.Port) or 9200
user = cfg.User or None
password = cfg.Password or None
use_ssl = cfg.UseSsl == 'True' or False
verify_certs = cfg.VerifyCerts == 'False'
ca_certs = cfg.CaCerts or None
options = dict(
hosts=[{'host': host, 'port': port}],
http_auth=None if not user else (user, password),
use_ssl=use_ssl,
verify_certs=verify_certs,
ca_certs=ca_certs
)
return options
else: else:
logger.error('Attempted to access application configuration outside of application context.') logger.error('Attempted to access application configuration outside of application context.')
raise RuntimeError('Attempted to access application configuration outside of application context.') raise RuntimeError('Attempted to access application configuration outside of application context.')
@staticmethod
def build_es_options(cfg) -> dict:
"""
Build Elasticsearch connection options from the configuration.
:param cfg: Elasticsearch's configuration from the Flask application context.
"""
host = cfg.get('Host', 'localhost')
port = int(cfg.get('Port', 9200))
user = cfg.get('User')
password = cfg.get('Password')
use_ssl = cfg.get('UseSsl', 'False') == 'True'
verify_certs = cfg.get('VerifyCerts', 'False') == 'True'
ca_certs = cfg.get('CaCerts')
options = {
'hosts': [{'host': host, 'port': port}],
'http_auth': (user, password) if user and password else None,
'use_ssl': use_ssl,
'verify_certs': verify_certs,
'ca_certs': ca_certs
}
return options
def _get_config(self) -> dict:
"""Combines getting Elasticsearch configuration and building options."""
cfg = self.get_es_config()
return self.build_es_options(cfg)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment