-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
63 lines (50 loc) · 1.59 KB
/
main.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
import urllib.parse
import motor.motor_asyncio as async_motor
from sanic import Sanic, response
app = Sanic(__name__)
@app.route("/api/foo/bar")
async def test(request):
conn = request.app.config['db']
my_collection = 'collection_names'
data = list(
jsonify(doc) for doc in await conn[my_collection].find({}).sort("_id", 1).to_list(100)
)
return response.json({'status': 200, 'data': data}, status=200)
@app.listener('before_server_start')
async def register_db(app, loop):
# Create a database connection pool
connection_uri = "mongodb://{username}:{password}@{host}:{port}/".format(
username="foo", password=urllib.parse.quote("bar"),
host="localhost", port=27017
)
database_name = "foobar"
app.config['db'] = async_motor.AsyncIOMotorClient(
connection_uri,
# in milliseconds
maxIdleTimeMS=10000,
# minimal pool size
minPoolSize=10,
# maximal pool size
maxPoolSize=50,
# connection timeout in miliseconds
connectTimeoutMS=10000,
# boolean
retryWrites=True,
# wait queue in miliseconds
waitQueueTimeoutMS=10000,
# in miliseconds
serverSelectionTimeoutMS=10000
)[database_name]
@app.listener('after_server_stop')
async def close_connection(app, loop):
app.config['db'].close()
def jsonify(doc):
"""
Parse _id to id and convert to string
"""
doc['id'] = str(doc['_id'])
del doc['_id']
return doc
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000,
access_log=True, debug=True)