File: web.py

package info (click to toggle)
podman-compose 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,996 kB
  • sloc: python: 10,946; sh: 107; javascript: 48; makefile: 13
file content (39 lines) | stat: -rw-r--r-- 1,057 bytes parent folder | download
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
# pylint: disable=import-error
# pylint: disable=unused-import
import asyncio  # noqa: F401
import os

import aioredis  # type: ignore[import-not-found]
from aiohttp import web  # type: ignore[import-not-found]

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"))

redis = aioredis.from_url(f"redis://{REDIS_HOST}:{REDIS_PORT}/{REDIS_DB}")
app = web.Application()
routes = web.RouteTableDef()


@routes.get("/")
async def hello(request: web.Request) -> web.Response:  # pylint: disable=unused-argument
    counter = await redis.incr("mycounter")
    return web.Response(text=f"counter={counter}")


@routes.get("/hello.json")
async def hello_json(request: web.Request) -> web.Response:  # pylint: disable=unused-argument
    counter = await redis.incr("mycounter")
    data = {"counter": counter}
    return web.json_response(data)


app.add_routes(routes)


def main() -> None:
    web.run_app(app, port=8080)


if __name__ == "__main__":
    main()