Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: akmamun/fastapi-mongo
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.0.1
Choose a base ref
...
head repository: akmamun/fastapi-mongo
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 5 commits
  • 8 files changed
  • 2 contributors

Commits on Oct 26, 2023

  1. update packages and simplify start command

    mohammad.mamun committed Oct 26, 2023
    Copy the full SHA
    41fd6bd View commit details
  2. fixed env format

    mohammad.mamun committed Oct 26, 2023
    Copy the full SHA
    6719880 View commit details
  3. update readme

    akmamun authored Oct 26, 2023
    Copy the full SHA
    3fcd592 View commit details
  4. Copy the full SHA
    b3fc679 View commit details
  5. Update README.md

    akmamun authored Oct 26, 2023
    Copy the full SHA
    0892856 View commit details
Showing with 44 additions and 24 deletions.
  1. +4 −1 .env.example
  2. +1 −1 Dockerfile
  3. +28 −10 README.md
  4. +4 −4 docker-compose.yml
  5. +4 −5 requirements.txt
  6. +1 −1 src/servers/start.py → server.py
  7. +1 −1 src/config/database.py
  8. +1 −1 src/servers/models/todo.py
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
DATABASE_NAME=DATABASE_NAME
MONGO_URL=mongodb://localhost:27017/
DATABASE_NAME=your_database_name
USERNAME=
PASSWORD=
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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
38 changes: 28 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
## 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
```sh
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`
8 changes: 4 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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:
9 changes: 4 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/servers/start.py → server.py
Original file line number Diff line number Diff line change
@@ -19,4 +19,4 @@


if __name__ == "main":
app.run()
app.run(host="0.0.0.0",port=9000)
2 changes: 1 addition & 1 deletion src/config/database.py
Original file line number Diff line number Diff line change
@@ -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", "")
}
}
2 changes: 1 addition & 1 deletion src/servers/models/todo.py
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@

class Todos(Repository):
# async def index(self, add):
# await add([('', "")])
# await add([('_id')])

def collection(self):
return 'todos'