Skip to content

johnscode/py-fastapi-mongo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

py-fastapi-mongo

a boilerplate microservice for REST using python, fastapi, and mongo

setup

python

pip install "fastapi[standard]"
pip install uvicorn motor pymongo

Be sure to use the quotes around fastapi[standard]

database

Be sure to have mongo and mongosh installed on your local system

Seed the database

mongosh
[email protected]>use inventory
[email protected]>db.items.insertOne({name: "widget",count:0,partnum:"00-001"})

This will create the inventory db and the items collection. Change the values to suit your data model.

Running

Once setup is done, you should be able to run the server using:

fastapi dev main.py 

Use the --reload option, if you want to the server to restart after you have changed files.

fastapi dev main.py --reload

Now point your browser to http://localhost:8000/items. You should receive a page showing json output similar to the following:

[{"name":"widget","count":0,"partnum":"00-001","id":"66ae9e533ed60220cf41de0f"}]

You can also view the API docs for this service by pointing your browser to http://127.0.0.1:8000/docs

You should see something similar to: FastApi generated docs

Note that you can test your api using these api docs. For example, you may add items using the POST /items endpoint.

Changing the port

If you want to run it on another port use the --port option when invoking

PORT=3000 fastapi dev main.py --reload --port 3000

About

a boilerplate microservice for REST using python, fastapi, and mongo

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages