From 9330d7a103a6ad92a08244c7e98aed680f21dfe1 Mon Sep 17 00:00:00 2001 From: philip306 Date: Fri, 31 Jul 2020 22:36:12 -0400 Subject: [PATCH] asuh --- .dockerignore | 2 ++ Dockerfile | 17 +++++++--- README.md | 79 +++++++++++++++++++++++++++++++++++++++++++++- app/conf/config.py | 2 +- app/main.py | 4 +-- docker-compose.yml | 12 +++++++ requirements.txt | 1 + 7 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 .dockerignore create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..d824784 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +.git +.dockerignore \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index f4b5410..5c26532 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,14 @@ -FROM python:3.7 -RUN pip install fastapi uvicorn -EXPOSE 80 +FROM ubuntu + +COPY requirements.txt / + +RUN apt-get update \ + && DEBIAN_FRONTEND=noninteractive apt-get install -y python3.6 python3-pip + +RUN pip3 install -r /requirements.txt + +EXPOSE 8000 + COPY ./app /app -CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"] \ No newline at end of file + +CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] diff --git a/README.md b/README.md index c410c01..8dc7f68 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,80 @@ # CountAPI -This project is a clone of the functionality available at https://countapi.xyz/ implemented with FastAPI and Redis +This project is a clone of the functionality available at https://countapi.xyz/ implemented with Python using [FastAPI](https://fastapi.tiangolo.com/) and [Redis](https://redis.io/). If you find this useful, consider donating to [Mlomb](https://countapi.xyz/#donate), the creator of countapi.xyz + +## Documentation + +Thanks to FastAPI there is swagger/OpenAPI doc endpoints included automatically and after deployment will be available at /docs and /redoc + +## Deployment + +First clone the repo locally + +``` git clone https://github.com/philip306/countapi.git ``` + +Install the prerequisites: + +```pip install -r /requirements.txt``` + +Update config.py to point to your redis host/ip + +```redishost: str = 'redis'``` + +Start Uvicorn: + +```uvicorn app.main:app --port 8000``` + +Navigate to http://127.0.0.1:8000 in a browser + +### Docker + +First clone the repo locally + +``` git clone https://github.com/philip306/countapi.git ``` + +Update config.py to point to your redis host/ip + +```redishost: str = 'redis'``` + +From within the count api directory build the docker image + +```docker build -t countapi:0.1 .``` + +Run the image you just created + +```docker run -p 8000:8000 --detach --name countapi countapi:0.1``` + +Navigate to http://127.0.0.1:8000 in a browser + +### Docker Compose + +Using ```docker-compose``` will using the deployment outlined in docker-compose.yml which will deploy a second container with a standard redis image + +First clone the repo locally + +``` git clone https://github.com/philip306/countapi.git ``` + +Update config.py to point to your redis host/ip + +```redishost: str = 'redis'``` + +From within the count api directory build the docker image + +```docker build -t countapi:0.1 .``` + +Launch two separate containers with a redis image and a countapi image you just created + +```docker-compose up``` + +Navigate to http://127.0.0.1:8000 in a browser + +### AWS Lambda and Elasticache + +_Coming soon_ + +## Testing + +```pytest``` will execute the tests outlined in tests/test_main.py. Currently very low coverage. + +## Known Issues +Currently there is no TTL set on the keys, so the key will exist indefinitely. You can clean them up manually if needed with redis-cli> flushdb diff --git a/app/conf/config.py b/app/conf/config.py index 35f76fe..8f61177 100644 --- a/app/conf/config.py +++ b/app/conf/config.py @@ -3,7 +3,7 @@ from pydantic import BaseSettings class Settings(BaseSettings): app_name: str = "CountAPI" ttlsetting: int = 100000 - redishost: str = '192.168.25.12' + redishost: str = 'redis' port: int = 6379 db: int = 0 redispass: str = "" diff --git a/app/main.py b/app/main.py index 48ef235..3ab6f64 100644 --- a/app/main.py +++ b/app/main.py @@ -2,7 +2,7 @@ from fastapi import FastAPI, HTTPException import redis import uuid -from conf.config import settings +from app.conf.config import settings pool = redis.ConnectionPool(host=settings.redishost, port=settings.port, db=settings.db) r = redis.Redis(connection_pool=pool) @@ -10,7 +10,7 @@ app = FastAPI() @app.get("/") async def root(): - return {"msg": "Hello World"} + return {"msg": "CountAPI is up!"} @app.get("/get/{key}") async def getkey(key: str): diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..51eed15 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,12 @@ +version: '2' +services: + web: + build: . + ports: + - "8000:8000" + volumes: + - .:/countapi + depends_on: + - redis + redis: + image: redis \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index ea423af..f880a49 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ fastapi==0.60.1 redis==3.5.3 requests==2.24.0 +uvicorn==0.11.6 \ No newline at end of file