-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.py
55 lines (40 loc) · 1.18 KB
/
simple.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import json
from aiohttp import web
import jinja2
import aiohttp_jinja2
import search_utils
class CustomEncoder(json.JSONEncoder):
def default(self, obj):
try:
iterable = iter(obj)
except TypeError:
pass
else:
return list(sorted(iterable))
return json.JSONEncoder.default(self, obj)
async def search(request):
data = await request.json()
response = web.StreamResponse(
status=200, reason="OK", headers={"Content-Type": "text/plain"},
)
await response.prepare(request)
author_names = data["name"].split(";")
async for suggestion in search_utils.suggest_authors(author_names):
await response.write(
(json.dumps(suggestion, cls=CustomEncoder) + "\n").encode("utf-8")
)
await response.write_eof()
return response
@aiohttp_jinja2.template("index.html")
async def index(request):
return {}
app = web.Application()
app.add_routes(
[
web.get("/", index),
web.post("/search", search),
web.static("/static", "./front/static"),
]
)
aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader("./front/templates"))
web.run_app(app)