A boilerplate using flask and mongodb
- Understanding of flask, uWSGI
- Understanding of docker
- Understanding of mongodb
- Understanding of flask-mongoengine (A document mapper library)
- install and start docker
- clone this repo
- goto root directory of repo
- run this > docker-compose up
this will create two container >
-
app container
flask-mongodb-boilerplate_web_1
-
mongodb container
flask-mongodb-boilerplate_mongo_1
If you check the docker-compose file you will see, that I have mentioned MONGO_INITDB_ROOT_USERNAME, MONGO_INITDB_ROOT_PASSWORD.
So this is a username and password for admin(default) database.
Now you have to create a new database for your application.
For example in this boilerplate I am using sample
database.
Now follow these steps to create user for sample database(ex.sample)(You have to do this only once)
- docker exec -it flask-mongodb-boilerplate_mongo_1 bash (Goto mongo container shell)
- mongo (open mongo shell)
- use admin (switch to admin database, need authentication to create new db)
- db.auth(username, password); > use username and password that you have mentioned in docker-compose file
- use sample (now switch to new database> sample)
- db.createUser( { user: "username for sample db", pwd: 'password for sample db'(), roles: [ "readWrite", "dbAdmin" ] } ) Use this command to create user for you sample db
Now update app/settings/configs.py file and update MONGODB_SETTINGS with your new db, username and password
Now you can access api http://localhost:5000/
Sample link - http://localhost:5000/api/sample/health/
Write your test cases then run
pytest tests/ --capture=tee-sys
http://docs.mongoengine.org/guide/defining-documents.html
https://docs.mongodb.com/manual/mongo/
- Add TESTING ENV and sample TESTING
- Add Documentation link about boilerplate structure