通过Flask应用处理对Kibana和ElasticSearch的请求

时间:2019-04-01 18:29:53

标签: elasticsearch nginx flask

我正在尝试在仅打开端口443的实例上配置ElasticSearch系统,并在Docker和Docker Compose上使用Elastic堆栈,该堆栈公开给端口5601(Kibana)和9200(ElasticSearch)。我想使用Flask来进行以下操作:

  1. 处理仅开放端口443的问题,因为我希望用户能够同时使用ES和Kibana
  2. 处理授权
  3. 向搜索系统(自定义端点)添加更多功能

Nginx用作Web服务器:

server {
    listen 443;
    listen [::]:443 ipv6only=on;

    server_name XXX.XXX.XXX.XXX;

    location / {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

我尝试过这样配置Flask应用程序:

import requests
from elasticsearch import Elasticsearch
from flask import Response, redirect, request, stream_with_context, url_for

es = Elasticsearch([{'host': 'localhost', 'port': 9200}])

kibana_server_baseurl = 'http://localhost:5601/'
elastic_server_baseurl = 'http://localhost:9200/'

@app.route('/')
@requires_auth
def index():
    return 'Elastic Search System'

@app.route('/kibana', methods=['POST', 'GET'])
@requires_auth
def kibana():
    req = requests.get(kibana_server_baseurl, stream=True)
    return Response(stream_with_context(req.iter_content()),
                    content_type=req.headers['content-type'])

@app.route('/elastic', methods=['POST', 'GET'])
@requires_auth
def kibana():
    req = requests.get(elastic_server_baseurl, stream=True)
    return Response(stream_with_context(req.iter_content()),
                    content_type=req.headers['content-type'])

@app.route('/custom', methods=['POST', 'GET'])
@requires_auth
def custom_func():
    req = request.get_json()
    embedding_vector = get_embedding(req['sentence'])
    query = construct_query(embedding_vector)
    return es.search(index=req['index_name'], doc_type='_doc', body=query)

这会导致以下错误:

Could not build url for endpoint'http://localhost:5601/'

并这样:

@app.route('/kibana', methods=['POST', 'GET'])
@requires_auth
def kibana():
    req = requests.get(kibana_server_baseurl, stream=True)
    return req.content()

毫不奇怪,这会导致无限的Kibana加载动画。

给定端口限制,有什么方法可以实现我想要的?

0 个答案:

没有答案