| 1 |
|
| 2 |
|
| 3 |
import asyncio |
| 4 |
import os |
| 5 |
|
| 6 |
import aioredis |
| 7 |
from aiohttp import web |
| 8 |
|
| 9 |
REDIS_HOST = os.environ.get("REDIS_HOST", "localhost") |
| 10 |
REDIS_PORT = int(os.environ.get("REDIS_PORT", "6379")) |
| 11 |
REDIS_DB = int(os.environ.get("REDIS_DB", "0")) |
| 12 |
|
| 13 |
redis = aioredis.from_url(f"redis://{REDIS_HOST}:{REDIS_PORT}/{REDIS_DB}") |
| 14 |
app = web.Application() |
| 15 |
routes = web.RouteTableDef() |
| 16 |
|
| 17 |
|
| 18 |
@routes.get("/") |
| 19 |
async def hello(request: web.Request) -> web.Response: |
| 20 |
counter = await redis.incr("mycounter") |
| 21 |
return web.Response(text=f"counter={counter}") |
| 22 |
|
| 23 |
|
| 24 |
@routes.get("/hello.json") |
| 25 |
async def hello_json(request: web.Request) -> web.Response: |
| 26 |
counter = await redis.incr("mycounter") |
| 27 |
data = {"counter": counter} |
| 28 |
return web.json_response(data) |
| 29 |
|
| 30 |
|
| 31 |
app.add_routes(routes) |
| 32 |
|
| 33 |
|
| 34 |
def main() -> None: |
| 35 |
web.run_app(app, port=8080) |
| 36 |
|
| 37 |
|
| 38 |
if __name__ == "__main__": |
| 39 |
main() |