forked from gmr/queries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-example.py
38 lines (29 loc) · 997 Bytes
/
test-example.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
import logging
import queries
from tornado import gen, ioloop, web
class ExampleHandler(web.RequestHandler):
def initialize(self):
self.session = queries.TornadoSession()
@gen.coroutine
def prepare(self):
try:
yield self.session.validate()
except queries.OperationalError as error:
logging.error('Error connecting to the database: %s', error)
raise web.HTTPError(503)
@gen.coroutine
def get(self):
try:
result = yield self.session.query('SELECT * FROM names')
except queries.OperationalError as error:
logging.error('Error connecting to the database: %s', error)
raise web.HTTPError(503)
self.finish({'data': result.items()})
result.free()
application = web.Application([
(r"/", ExampleHandler),
])
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
application.listen(8888)
ioloop.IOLoop.instance().start()