add database connection

This commit is contained in:
iou1name 2020-11-17 12:46:58 -05:00
parent 14597ebe28
commit 86a896f78b
3 changed files with 19 additions and 3 deletions

View File

@ -7,10 +7,17 @@ eg. https://example.com/stickup.
""" """
server_domain = 'steelbea.me' server_domain = 'steelbea.me'
url_prefix = '/stickup' url_prefix = '/stickup'
buckler = { buckler = {
'url': "https://steelbea.me/buckler", 'url': "https://steelbea.me/buckler",
'app_id': 1, 'app_id': 1,
'app_key': """password""", 'app_key': r"""password""",
'login_url': "https://steelbea.me/buckler/login", 'login_url': "https://steelbea.me/buckler/login",
} }
mailserver_db = {
'database': 'mailserver',
'user': 'mailserver',
'password': r"""password""",
'host': 'localhost',
'port': 5432,
}

View File

@ -9,6 +9,7 @@ import jinja2
import aiohttp_jinja2 import aiohttp_jinja2
from aiohttp_jinja2 import render_template from aiohttp_jinja2 import render_template
import uvloop import uvloop
import asyncpg
import config import config
import buckler_aiohttp import buckler_aiohttp
@ -19,13 +20,17 @@ routes = web.RouteTableDef()
@routes.get('/', name='index') @routes.get('/', name='index')
async def index(request): async def index(request):
"""The index page.""" """The index page."""
async with request.app['pool'].acquire() as conn:
user_id = int(request.cookies.get('userid'))
email_addresses = await conn.fetch(
"SELECT email FROM virtual_users WHERE buckler_id = $1",
user_id)
return render_template('index.html', request, locals()) return render_template('index.html', request, locals())
async def init_app(): async def init_app():
"""Initializes the application.""" """Initializes the application."""
app = web.Application(middlewares=[buckler_aiohttp.buckler_session]) app = web.Application(middlewares=[buckler_aiohttp.buckler_session])
#app = web.Application()
aiohttp_jinja2.setup( aiohttp_jinja2.setup(
app, app,
trim_blocks=True, trim_blocks=True,
@ -33,6 +38,7 @@ async def init_app():
undefined=jinja2.StrictUndefined, undefined=jinja2.StrictUndefined,
loader=jinja2.FileSystemLoader('templates'), loader=jinja2.FileSystemLoader('templates'),
) )
app['pool'] = await asyncpg.create_pool(**config.mailserver_db)
app.router.add_routes(routes) app.router.add_routes(routes)
app_wrap = web.Application() app_wrap = web.Application()
app_wrap.add_subapp(config.url_prefix, app) app_wrap.add_subapp(config.url_prefix, app)

View File

@ -7,5 +7,8 @@
</head> </head>
<body> <body>
<h1>This is a stickup!</h1> <h1>This is a stickup!</h1>
{% for record in email_addresses %}
<div>Your email address is {{ record['email'] }}</div>
{% endfor %}
</body> </body>
</html> </html>