2020-04-08 22:15:50 +00:00
|
|
|
Example Web Application
|
|
|
|
=======================
|
|
|
|
The following code provides a simple example for using the
|
|
|
|
|
|
|
|
.. code-block:: python
|
2020-04-08 22:22:24 +00:00
|
|
|
|
2020-04-08 22:15:50 +00:00
|
|
|
import sprockets.http
|
|
|
|
import sprockets_postgres as postgres
|
|
|
|
from sprockets.http import app
|
|
|
|
|
|
|
|
|
2020-04-08 22:22:24 +00:00
|
|
|
class RequestHandler(postgres.RequestHandlerMixin, web.RequestHandler):
|
2020-04-08 22:15:50 +00:00
|
|
|
|
|
|
|
GET_SQL = """\
|
|
|
|
SELECT foo_id, bar, baz, qux
|
|
|
|
FROM public.foo
|
|
|
|
WHERE foo_id = %(foo_id)s;"""
|
|
|
|
|
|
|
|
async def get(self, foo_id: str) -> None:
|
|
|
|
result = await self.postgres_execute(self.GET_SQL, {'foo_id': foo_id})
|
|
|
|
await self.finish(result.row)
|
|
|
|
|
|
|
|
|
|
|
|
class Application(postgres.ApplicationMixin, app.Application):
|
|
|
|
"""
|
2020-04-10 20:34:39 +00:00
|
|
|
The :class:`sprockets_postgres.ApplicationMixin` provides the foundation
|
|
|
|
for the :class:`sprockets_postgres.RequestHandlerMixin` to properly function
|
|
|
|
and will automatically setup the pool to connect to PostgreSQL and will
|
|
|
|
shutdown the connections cleanly when the application stops.
|
2020-04-08 22:15:50 +00:00
|
|
|
|
2020-04-10 20:34:39 +00:00
|
|
|
It should be used in conjunction with :class:`sprockets.http.app.Application`
|
2020-04-10 20:42:29 +00:00
|
|
|
and not directly with :class:`tornado.web.Application`.
|
2020-04-08 22:22:24 +00:00
|
|
|
|
2020-04-08 22:15:50 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def make_app(**settings):
|
|
|
|
return Application([
|
|
|
|
web.url(r'/foo/(?P<foo_id>.*)', FooRequestHandler)
|
|
|
|
], **settings)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sprockets.http.run(make_app)
|