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

40 lines
923 B
Python
Raw Normal View History

# pylint: disable=import-error
# pylint: disable=unused-import
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("/")
async def hello(request): # pylint: disable=unused-argument
2022-05-10 01:28:17 +02:00
counter = await redis.incr("mycounter")
return web.Response(text=f"counter={counter}")
2022-05-10 01:32:08 +02:00
@routes.get("/hello.json")
async def hello_json(request): # pylint: disable=unused-argument
2022-05-10 01:28:17 +02:00
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()