This commit is contained in:
philip306 2020-07-31 22:36:12 -04:00
parent 6bb4bfaeed
commit 9330d7a103
7 changed files with 109 additions and 8 deletions

2
.dockerignore Normal file
View File

@ -0,0 +1,2 @@
.git
.dockerignore

View File

@ -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"]
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

View File

@ -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

View File

@ -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 = ""

View File

@ -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):

12
docker-compose.yml Normal file
View File

@ -0,0 +1,12 @@
version: '2'
services:
web:
build: .
ports:
- "8000:8000"
volumes:
- .:/countapi
depends_on:
- redis
redis:
image: redis

View File

@ -1,3 +1,4 @@
fastapi==0.60.1
redis==3.5.3
requests==2.24.0
uvicorn==0.11.6