podman-compose/examples/hello-python/App/web.py

38 lines
790 B
Python
Raw Normal View History

2022-05-10 01:28:17 +02:00
import os
import asyncio # noqa: F401
2022-05-10 01:28:17 +02:00
import aioredis
from aiohttp import web
2022-05-10 01:32:08 +02:00
REDIS_HOST = os.environ.get("REDIS_HOST", "localhost")
REDIS_PORT = int(os.environ.get("REDIS_PORT", "6379"))
REDIS_DB = int(os.environ.get("REDIS_DB", "0"))
2022-05-10 01:28:17 +02:00
redis = aioredis.from_url(f"redis://{REDIS_HOST}:{REDIS_PORT}/{REDIS_DB}")
app = web.Application()
routes = web.RouteTableDef()
2022-05-10 01:32:08 +02:00
@routes.get("/")
2022-05-10 01:28:17 +02:00
async def hello(request):
counter = await redis.incr("mycounter")
return web.Response(text=f"counter={counter}")
2022-05-10 01:32:08 +02:00
@routes.get("/hello.json")
2022-05-10 01:28:17 +02:00
async def hello_json(request):
counter = await redis.incr("mycounter")
2022-05-10 01:32:08 +02:00
data = {"counter": counter}
2022-05-10 01:28:17 +02:00
return web.json_response(data)
2022-05-10 01:32:08 +02:00
2022-05-10 01:28:17 +02:00
app.add_routes(routes)
2022-05-10 01:32:08 +02:00
2022-05-10 01:28:17 +02:00
def main():
2022-06-21 20:48:45 +02:00
web.run_app(app, port=8080)
2022-05-10 01:28:17 +02:00
2022-05-10 01:32:08 +02:00
if __name__ == "__main__":
main()