52 lines
1.2 KiB
Python
52 lines
1.2 KiB
Python
from fastapi import FastAPI
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fastapi.templating import Jinja2Templates
|
|
from fastapi import Request
|
|
|
|
# from elasticsearch import Elasticsearch
|
|
import sys
|
|
import elastic_transport
|
|
from searching import es_search, es_query
|
|
import json
|
|
import yaml
|
|
|
|
app = FastAPI(debug=True)
|
|
|
|
templates = Jinja2Templates(directory="./httpdemo")
|
|
# app.mount("/", StaticFiles(directory="/"))
|
|
|
|
|
|
@app.get("/")
|
|
def serve_home(request: Request, q: str = ""):
|
|
try:
|
|
resp = es_search(q)
|
|
query = es_query(q)
|
|
message = f"found ...? %d" % len(resp["hits"]["hits"])
|
|
except (
|
|
elastic_transport.ConnectionError,
|
|
elastic_transport.ConnectionTimeout,
|
|
) as e:
|
|
print(e, sys.stderr)
|
|
results = []
|
|
resp = {}
|
|
query = {}
|
|
message = f"cannot reach the search server! : %s" % e
|
|
else:
|
|
results = resp["hits"]["hits"]
|
|
|
|
templates.env.filters["json"] = lambda x: yaml.dump(dict(x))
|
|
return templates.TemplateResponse(
|
|
"index.html",
|
|
context={
|
|
"request": resp,
|
|
"results": results,
|
|
"message": message,
|
|
"query": query,
|
|
},
|
|
)
|
|
|
|
|
|
@app.get("test")
|
|
def test(request: Request):
|
|
return {"test": "test"}
|