From 75033a4ed7bb008618116c2dd8d844e03d10fa79 Mon Sep 17 00:00:00 2001 From: Muayyad alsadi Date: Tue, 10 May 2022 02:28:17 +0300 Subject: [PATCH] add python demo example --- examples/hello-python/App/__init__.py | 0 examples/hello-python/App/web.py | 33 +++++++++++++++++++++++ examples/hello-python/Dockerfile | 12 +++++++++ examples/hello-python/README.md | 8 ++++++ examples/hello-python/docker-compose.yaml | 21 +++++++++++++++ examples/hello-python/requirements.txt | 3 +++ 6 files changed, 77 insertions(+) create mode 100644 examples/hello-python/App/__init__.py create mode 100644 examples/hello-python/App/web.py create mode 100644 examples/hello-python/Dockerfile create mode 100644 examples/hello-python/README.md create mode 100644 examples/hello-python/docker-compose.yaml create mode 100644 examples/hello-python/requirements.txt diff --git a/examples/hello-python/App/__init__.py b/examples/hello-python/App/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/examples/hello-python/App/web.py b/examples/hello-python/App/web.py new file mode 100644 index 0000000..5b56d9f --- /dev/null +++ b/examples/hello-python/App/web.py @@ -0,0 +1,33 @@ +import os +import asyncio + +import aioredis +from aiohttp import web + +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): + counter = await redis.incr("mycounter") + return web.Response(text=f"counter={counter}") + +@routes.get('/hello.json') +async def hello_json(request): + counter = await redis.incr("mycounter") + data = {'counter': counter} + return web.json_response(data) + +app.add_routes(routes) + +def main(): + web.run_app(app) + +if __name__=='__main__': + main() + diff --git a/examples/hello-python/Dockerfile b/examples/hello-python/Dockerfile new file mode 100644 index 0000000..b879d8f --- /dev/null +++ b/examples/hello-python/Dockerfile @@ -0,0 +1,12 @@ +FROM python:3.9-alpine + +WORKDIR /usr/src/app + +COPY requirements.txt ./ +RUN pip install --no-cache-dir -r requirements.txt + +COPY . . + +CMD [ "python", "-m", "App.web" ] +EXPOSE 8080 + diff --git a/examples/hello-python/README.md b/examples/hello-python/README.md new file mode 100644 index 0000000..c2e3b88 --- /dev/null +++ b/examples/hello-python/README.md @@ -0,0 +1,8 @@ +# Simple Python Demo +## A Redis counter + +``` +podman-compose up -d +curl localhost:8080/ +curl localhost:8080/hello.json +``` diff --git a/examples/hello-python/docker-compose.yaml b/examples/hello-python/docker-compose.yaml new file mode 100644 index 0000000..a4ace8b --- /dev/null +++ b/examples/hello-python/docker-compose.yaml @@ -0,0 +1,21 @@ +--- +version: '3' +volumes: + redis: +services: + redis: + read_only: true + image: docker.io/redis:alpine + command: ["redis-server", "--appendonly", "yes", "--notify-keyspace-events", "Ex"] + volumes: + - redis:/data + web: + read_only: true + build: + context: . + image: hello-py-aioweb + ports: + - 8080:8080 + environment: + REDIS_HOST: redis + diff --git a/examples/hello-python/requirements.txt b/examples/hello-python/requirements.txt new file mode 100644 index 0000000..c82e36e --- /dev/null +++ b/examples/hello-python/requirements.txt @@ -0,0 +1,3 @@ +aiohttp +aioredis +# aioredis[hiredis]