-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.py
74 lines (55 loc) · 2.14 KB
/
web.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/env python3
""" Web server """
from multiprocessing import cpu_count
from motor.motor_asyncio import AsyncIOMotorClient
from sanic import Sanic
from sanic.response import json
from config import (
BIND_ADDRESS,
DATABASE_NAME,
HTTP_PORT,
MONGO_SERVER,
log
)
from sanic_paginate import Pagination
app = Sanic(__name__)
db = None
@app.route('/adverts/', methods=['GET'])
@app.route('/adverts/<advert_id>', methods=['GET'])
async def adverts(req, advert_id=None):
"""Paged navigation of adverts using aiocache to lessen database hits."""
page, per_page, offset = Pagination.get_page_args(req)
# hide system fields
fields = {'_id': 0, 'checksum': 0, 'created': 0, 'last_fetched': 0}
if advert_id:
data = await db.adverts.find({'advert_id': advert_id}, fields).skip(offset).to_list(per_page)
total = len(data)
else:
data = await db.adverts.find({}, fields).skip(offset).to_list(per_page)
total = len(data)
pagination = Pagination(req, total=total, data=data)
return json(pagination)
@app.route('/raw_adverts/', methods=['GET'])
@app.route('/raw_adverts/<advert_id>', methods=['GET'])
async def raw_adverts(req, advert_id=None):
""" Paged navigation of raw_adverts - using aiocache to lessen database hits. """
page, per_page, offset = Pagination.get_page_args(req)
# hide system fields
fields = {'_id': 0, 'checksum': 0, 'created': 0, 'last_fetched': 0}
if advert_id:
data = await db.feeds.find({'advert_id': advert_id}, fields).skip(offset).to_list(per_page)
total = len(data)
else:
data = await db.feeds.find({}, fields).skip(offset).to_list(per_page)
total = len(data)
pagination = Pagination(req, total=total, data=data)
return json(pagination)
@app.listener('after_server_start')
async def init_connections(sanic, loop):
"""Bind the database to Sanic's event loop."""
global db
motor = AsyncIOMotorClient(MONGO_SERVER, io_loop=loop)
db = motor[DATABASE_NAME]
if __name__ == '__main__':
log.debug("Beginning run.")
app.run(host=BIND_ADDRESS, port=HTTP_PORT, workers=cpu_count(), debug=True)