Skip to content

Commit bba8f6d

Browse files
committed
Code base
Adding project Code base
1 parent 3931536 commit bba8f6d

25 files changed

+915
-0
lines changed

.gitignore

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# author: https://github.com/github/gitignore/blob/master/Python.gitignore
2+
# IDE
3+
.vscode
4+
.pylintrc
5+
6+
# Byte-compiled / optimized / DLL files
7+
__pycache__/
8+
*.py[cod]
9+
*$py.class
10+
11+
# C extensions
12+
*.so
13+
14+
# Distribution / packaging
15+
.Python
16+
build/
17+
develop-eggs/
18+
dist/
19+
downloads/
20+
eggs/
21+
.eggs/
22+
lib/
23+
lib64/
24+
parts/
25+
sdist/
26+
var/
27+
wheels/
28+
share/python-wheels/
29+
*.egg-info/
30+
.installed.cfg
31+
*.egg
32+
MANIFEST
33+
34+
# PyInstaller
35+
# Usually these files are written by a python script from a template
36+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
37+
*.manifest
38+
*.spec
39+
40+
# Installer logs
41+
pip-log.txt
42+
pip-delete-this-directory.txt
43+
44+
# Unit test / coverage reports
45+
htmlcov/
46+
.tox/
47+
.nox/
48+
.coverage
49+
.coverage.*
50+
.cache
51+
nosetests.xml
52+
coverage.xml
53+
*.cover
54+
*.py,cover
55+
.hypothesis/
56+
.pytest_cache/
57+
cover/
58+
59+
# Translations
60+
*.mo
61+
*.pot
62+
63+
# Django stuff:
64+
*.log
65+
local_settings.py
66+
db.sqlite3
67+
db.sqlite3-journal
68+
69+
# Flask stuff:
70+
instance/
71+
.webassets-cache
72+
73+
# Scrapy stuff:
74+
.scrapy
75+
76+
# Sphinx documentation
77+
docs/_build/
78+
79+
# PyBuilder
80+
.pybuilder/
81+
target/
82+
83+
# Jupyter Notebook
84+
.ipynb_checkpoints
85+
86+
# IPython
87+
profile_default/
88+
ipython_config.py
89+
90+
# pyenv
91+
# For a library or package, you might want to ignore these files since the code is
92+
# intended to run in multiple environments; otherwise, check them in:
93+
# .python-version
94+
95+
# pipenv
96+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
97+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
98+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
99+
# install all needed dependencies.
100+
#Pipfile.lock
101+
102+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
103+
__pypackages__/
104+
105+
# Celery stuff
106+
celerybeat-schedule
107+
celerybeat.pid
108+
109+
# SageMath parsed files
110+
*.sage.py
111+
112+
# Environments
113+
.env
114+
.venv
115+
env/
116+
venv/
117+
ENV/
118+
env.bak/
119+
venv.bak/
120+
121+
# Spyder project settings
122+
.spyderproject
123+
.spyproject
124+
125+
# Rope project settings
126+
.ropeproject
127+
128+
# mkdocs documentation
129+
/site
130+
131+
# mypy
132+
.mypy_cache/
133+
.dmypy.json
134+
dmypy.json
135+
136+
# Pyre type checker
137+
.pyre/
138+
139+
# pytype static type analyzer
140+
.pytype/
141+
142+
# Cython debug symbols
143+
cython_debug/

FastAPI-MongoDB.png

29 KB
Loading

FastAPI-MongoDB_test.png

15.6 KB
Loading

Makefile

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# usage: type in your terminal make run_tests_with_docker
2+
run_tests_with_docker:
3+
docker build --pull --rm -f "./docker/Dockerfile.dev" -t api:latest "." && \
4+
docker run -d p 8080:8080/tcp api:latest && \
5+
pytest -v tavern_tests/test_*.tavern.yaml
6+
7+
run_pytest_venv:
8+
# run pytest tavern api tests
9+
# require .venv/ to exist: python3 -m venv .venv
10+
. venv/bin/activate && \
11+
pip install -r requirements/dev.txt && \
12+
export ENVIRONMENT=DEV && \
13+
export MONGODB_HOST="localhost:27017" && \
14+
export MONGODB_DB_AUTH="admin" && \
15+
nohup python entry_point.py > output.log & &&\
16+
pytest -v tavern_tests/test_*.tavern.yaml
17+
18+
run_python_venv:
19+
# run the api from python .venv
20+
# require .venv/ to exist: python3 -m venv .venv
21+
. venv/bin/activate && \
22+
pip install -r requirements/dev.txt && \
23+
export ENVIRONMENT=DEV && \
24+
export MONGODB_HOST="localhost:27017" && \
25+
export MONGODB_DB_AUTH="admin" && \
26+
python entry_point.py
27+
28+
run_docker_dev:
29+
# build and run from docker/Dockerfile.dev
30+
docker build --pull --rm -f "./docker/Dockerfile.dev" -t api:latest "." && \
31+
docker run --it p 8080:8080/tcp api:latest
32+
33+
run_docker:
34+
# build and run from docker/Dockerfile
35+
docker build --pull --rm -f "./docker/Dockerfile" -t api:latest "." && \
36+
docker run --it p 8080:8080/tcp api:latest

README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# FastAPI MongoDB Async RestAPI
2+
3+
### A Blueprint of a production-ready project.
4+
5+
![alt text](./FastAPI-MongoDB.png)
6+
7+
> This project is based on [Michaldev](https://github.com/michaldev)'s Github [fastapi-async-mongodb](https://github.com/michaldev/fastapi-async-mongodb).</br>
8+
> Headers, tokens and other security features has been ignored for simplicity.
9+
> How to setup a [local Mongodb](https://zellwk.com/blog/local-mongodb/) for testing purposes.
10+
11+
# Features:
12+
13+
- Framework: [Fastapi](https://github.com/tiangolo/fastapi)
14+
- DB: [MongoDB](https://github.com/mongodb/mongo)
15+
- dependencies:
16+
- [Pydantic](https://github.com/samuelcolvin/pydantic)
17+
- [Motor](https://github.com/mongodb/motor)
18+
- [uvicorn](https://github.com/encode/uvicorn)
19+
- [sentry](https://github.com/getsentry/sentry)
20+
- [prometheus](https://github.com/prometheus/prometheus)
21+
- [tavern](https://github.com/taverntesting/tavern)
22+
- [Pytest](https://github.com/pytest-dev/pytest)
23+
24+
## Usage:
25+
26+
> Check ./Makefile content to understand how to run the app manually.
27+
28+
- [Makefile](https://www.gnu.org/software/make/manual/html_node/Simple-Makefile.html):
29+
- `make run_pytest_venv # run tests from a virtual environment (.venv)`
30+
- `make run_docker_dev # create and run the api from ./docker/Dockerfile.dev`
31+
32+
## Project Structure:
33+
34+
```
35+
├───api
36+
│ ├───db # db connection and mongodb api
37+
│ ├───models # pydantic models
38+
│ ├───public # public api namespaces
39+
│ │ ├───health # health check
40+
│ │ └───user # CRUD endpoints
41+
│ └───utils # app utilities
42+
│ │ └───logger # custom logger
43+
│ └───config.py # app config
44+
│ └───main.py # app main
45+
├───docker # dockerfiles
46+
├───requirements # dependencies
47+
└───tavern_tests # api tests
48+
└───entry_point.py # app entry point
49+
└───Makefile # commands helper
50+
```

api/__init__.py

Whitespace-only changes.

api/config.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
import secrets
3+
from functools import lru_cache
4+
from typing import Optional
5+
6+
from pydantic import BaseSettings, HttpUrl
7+
8+
9+
def database_uri():
10+
MONGODB_HOST = os.getenv("MONGODB_HOST", "localhost:27017")
11+
MONGODB_DB_AUTH = os.getenv("MONGODB_DB_AUTH", "admin")
12+
MONGODB_USERNAME = os.getenv("MONGODB_USERNAME")
13+
MONGODB_PASSWORD = os.getenv("MONGODB_PASSWORD")
14+
15+
if os.getenv("ENVIRONMENT") != "DEV":
16+
MONGODB_CLIENT_SETUP = f"mongodb://{MONGODB_USERNAME}:{MONGODB_PASSWORD}@{MONGODB_HOST}/{MONGODB_DB_AUTH}?tls=true"
17+
else:
18+
MONGODB_CLIENT_SETUP = f"mongodb://{MONGODB_HOST}/{MONGODB_DB_AUTH}?tls=false"
19+
return MONGODB_CLIENT_SETUP
20+
21+
22+
class Settings(BaseSettings):
23+
PROJECT_NAME: str = (
24+
f"FastApi Mongo Async API - {os.getenv('ENVIRONMENT', 'DEV').capitalize()}"
25+
)
26+
VERSION: str = "v1.0.0"
27+
DESCRIPTION: str = (
28+
"A Production Ready FastAPI API using Asyncronous MongoDB Connection"
29+
)
30+
SECRET_KET: str = secrets.token_urlsafe(32)
31+
SENTRY_DSN: Optional[HttpUrl] = os.getenv("SENTRY_DSN")
32+
DEBUG: bool = bool(os.getenv("DEBUG", "False"))
33+
ENVIRONMENT: str = os.getenv("ENVIRONMENT", "DEV").capitalize()
34+
DB_URI = database_uri()
35+
36+
class Config:
37+
case_sensitive = True
38+
39+
40+
@lru_cache
41+
def get_config():
42+
return Settings()

api/db/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from api.db.database_manager import DatabaseManager
2+
from api.db.mongo_manager import MongoManager
3+
4+
db = MongoManager()
5+
6+
7+
async def get_database() -> DatabaseManager:
8+
return db

api/db/database_manager.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from abc import abstractmethod
2+
from typing import List
3+
4+
from api.models import User
5+
6+
7+
class DatabaseManager:
8+
"""
9+
This class is meant to be extended from
10+
./mongo_manager.py which will be the actual connection to mongodb.
11+
"""
12+
13+
@property
14+
def client(self):
15+
raise NotImplementedError
16+
17+
@property
18+
def db(self):
19+
raise NotImplementedError
20+
21+
# database connect and close connections
22+
@abstractmethod
23+
async def connect_to_database(self, path: str):
24+
pass
25+
26+
@abstractmethod
27+
async def close_database_connection(self):
28+
pass
29+
30+
# to be used from /api/public endpoints
31+
@abstractmethod
32+
async def user_get_total(self) -> int:
33+
pass
34+
35+
@abstractmethod
36+
async def user_get_actives(self) -> int:
37+
pass
38+
39+
@abstractmethod
40+
async def user_get_all(self) -> List[User]:
41+
pass
42+
43+
@abstractmethod
44+
async def user_get_one(self, user_id: str) -> List[User]:
45+
pass
46+
47+
@abstractmethod
48+
async def user_insert_one(self, user: User) -> List[User]:
49+
pass
50+
51+
@abstractmethod
52+
async def user_update_one(self, user: User) -> List[User]:
53+
pass
54+
55+
@abstractmethod
56+
async def user_delete_one(self, user: User) -> List[User]:
57+
pass

0 commit comments

Comments
 (0)