diff --git a/.env.example b/.env.example index b1f7b90..69018db 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,4 @@ -DATABASE_NAME=DATABASE_NAME \ No newline at end of file +MONGO_URL=mongodb://localhost:27017/ +DATABASE_NAME=your_database_name +USERNAME= +PASSWORD= \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 97dfd21..f8d007d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ #base image -FROM python:3.8.6-slim-buster +FROM python:3.10-slim-buster ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 diff --git a/README.md b/README.md index 71c4a77..c12a300 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,28 @@ -## FastAPI MicroService with Docker, Nginx and MongoDB(Motor) +## FastAPI Micro Service with Docker, Nginx, and Asynchronous MongoDB (Motor) -### Create [virtual environment](https://docs.python.org/3/library/venv.html) and install requirements -```sh -pip install -r requirements.txt -``` +### Installation Instructions -### Run docker compose -- Make sure [docker](https://docs.docker.com/engine/install) and [docker-compose](https://docs.docker.com/compose/install/) installed +- Create a [virtual environment](https://docs.python.org/3/library/venv.html) +- Install the python Dependencies with `pip install -r requirements.txt` +- Copy the .env.example file as .env + ```sh + cp .env.example .env + ``` +- Ensure that you fill in all the valid environment properties in the .env file. + + +### Run Locally +To run the service locally, use the following command: + ```sh + uvicorn server:app --reload + ``` + +### Run with Docker +To run the service using Docker, use the following command: + ```sh + sudo docker-compose up -d --build + ``` + +### Run Docker Compose for Production Build +Ensure that you have [Docker]((https://docs.docker.com/engine/install)) and [Docker Compose](https://docs.docker.com/compose/install/) installed. -### Lets Run -- Docker run `sudo docker-compose up -d --build` -- Locally run `uvicorn src.servers.start:app --reload` diff --git a/docker-compose.yml b/docker-compose.yml index 5183e6c..d93faa9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,14 +14,14 @@ services: - servernet server: - container_name: service + container_name: core_server build: . restart: always - command: gunicorn -w 4 -k uvicorn.workers.UvicornWorker src.servers.start:app --bind 0.0.0.0:8000 + command: gunicorn -w 4 -k uvicorn.workers.UvicornWorker server:app --bind 0.0.0.0:8000 expose: - 8000 - # env_file: - # - .env + env_file: + - .env depends_on: - db links: diff --git a/requirements.txt b/requirements.txt index bc7ba6f..cb6d48e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ -fastapi==0.66.0 -uvicorn==0.14.0 -motor==2.4.0 -python-dotenv==0.18.0 -gunicorn==20.1.0 +fastapi==0.104.0 +uvicorn==0.23.2 +motor==3.3.1 +python-dotenv==1.0.0 diff --git a/src/servers/start.py b/server.py similarity index 90% rename from src/servers/start.py rename to server.py index 51df1c6..483321d 100644 --- a/src/servers/start.py +++ b/server.py @@ -19,4 +19,4 @@ if __name__ == "main": - app.run() + app.run(host="0.0.0.0",port=9000) diff --git a/src/config/database.py b/src/config/database.py index 1dc29a5..6dc5286 100644 --- a/src/config/database.py +++ b/src/config/database.py @@ -8,7 +8,7 @@ def db() -> dict: "db": { "url": env("MONGO_URL", "mongodb://localhost:27017/"), "name": env("DATABASE_NAME", "database_name"), - "user": env("USERNAME_NAME", ""), + "user": env("USERNAME", ""), "password": env("PASSWORD", "") } } diff --git a/src/servers/models/todo.py b/src/servers/models/todo.py index 2072df0..a436d59 100644 --- a/src/servers/models/todo.py +++ b/src/servers/models/todo.py @@ -6,7 +6,7 @@ class Todos(Repository): # async def index(self, add): - # await add([('', "")]) + # await add([('_id')]) def collection(self): return 'todos'