sprockets-postgres/docs/example.rst

45 lines
1.3 KiB
ReStructuredText
Raw Normal View History

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):
"""
The ``ApplicationMixin`` provides the foundation for the
``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:22:24 +00:00
It should be used in conjunction with ``sprockets.http.app.Application``
and not directly with ``tornado.web.Application``.
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)