-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
64 lines (48 loc) · 1.41 KB
/
app.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from aiohttp import web
from routes import routes
import jinja2
import aiohttp_jinja2
import os
import aiohttp_cors
import aioredis
import logging
from dotenv import load_dotenv
from middlerwares.auth import auth_middlerware
from middlerwares.ip_block import ip_block_middleware
load_dotenv()
async def on_startup(app):
try:
# Connect to Redis
url = os.getenv('REDIS_URL')
app['redis'] = await aioredis.from_url(url)
logging.info("Redis connected")
except Exception as e:
logging.error(f"Could not connect to redis: {e}")
async def on_cleanup(app):
# Close the Redis connection
app['redis'].close()
await app['redis'].wait_closed()
app = web.Application()
app.on_startup.append(on_startup)
app.on_cleanup.append(on_cleanup)
app.middlewares.append(ip_block_middleware)
app.middlewares.append(auth_middlerware)
aiohttp_jinja2.setup(
app, loader=jinja2.FileSystemLoader(os.path.join(os.getcwd(), "templates"))
)
cors = aiohttp_cors.setup(app, defaults={
"*": aiohttp_cors.ResourceOptions(
allow_credentials=True,
expose_headers="*",
allow_headers="*",
allow_methods="*",
)
})
router = app.router
routes.setup_routes(router)
# Configure CORS on all routes.
for route in list(app.router.routes()):
cors.add(route)
web.run_app(app,host='0.0.0.0', port=8000)
if __name__ == '__main__':
web.run_app(app)