-
-
Save lwzm/7f5a6f0f70e0423e3c6163cbdb62b6df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from os import getenv | |
from json import dumps | |
from elasticsearch import Elasticsearch | |
from elasticsearch.helpers import scan | |
from starlette.applications import Starlette | |
from starlette.requests import Request | |
from starlette.responses import Response, StreamingResponse | |
from starlette.routing import Route | |
es_host = getenv("ES", "es") | |
es = Elasticsearch(es_host) | |
def cat_indices(request: Request): | |
return Response(es.cat.indices(v=True), media_type='text/plain') | |
def dump_index(request: Request): | |
name = request.path_params["name"] | |
if not es.indices.exists(name): | |
return Response(status_code=404) | |
async def _iter(): | |
chunk = "" | |
for i in scan(es, index=name): | |
chunk += dumps(i["_source"], ensure_ascii=False) + "\n" | |
if len(chunk) > 4096: | |
if await request.is_disconnected(): | |
break | |
yield chunk | |
chunk = "" | |
if chunk: | |
yield chunk | |
return StreamingResponse(_iter(), media_type='text/plain') | |
app = Starlette(routes=[ | |
Route('/', cat_indices), | |
Route('/{name:path}', dump_index), | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment