-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathexample.py
More file actions
41 lines (32 loc) · 967 Bytes
/
example.py
File metadata and controls
41 lines (32 loc) · 967 Bytes
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
# @Author: Huang Sizhe <huangsizhe>
# @Date: 08-Apr-2017
# @Email: hsz1273327@gmail.com
# @Last modified by: huangsizhe
# @Last modified time: 08-Apr-2017
# @License: MIT
from sanic import Sanic
from sanic.response import json
from sanic_mongo import Mongo
app = Sanic(__name__)
mongo_uri = "mongodb://{host}:{port}/{database}".format(
database='test',
port=27017,
host='localhost'
)
Mongo.SetConfig(app,test=mongo_uri)
Mongo(app)
@app.get('/objects')
async def get(request):
docs = await app.mongo['test'].test_col.find().to_list(length=100)
for doc in docs:
doc['id'] = str(doc['_id'])
del doc['_id']
return json(docs)
@app.post('/objects')
async def new(request):
doc = request.json
print(type(app.mongo['test']))
object_id = await app.mongo['test']["test_col"].save(doc)
return json({'object_id': str(object_id)})
if __name__ == "__main__":
app.run(host='127.0.0.1', port=8000,debug=True)