Skip to content

Simple app uses Flask and MongoDB as backend and Nginx as frontend

Notifications You must be signed in to change notification settings

borisgu/flask-simple-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Webapp using MongoDB and Flask

This is a simple example of Python Flask and MongoDB usage. The Database Management System is MongoDB. The python-MongoDB connector is PyMongo. Frontend app is Nginx used as a reverse proxy.

Prerequisite

  • Machine with docker and docker-compose installed (Linux is preferred)

Usage

Clone this repo From the repo root directory run

$ source .env
$ docker-compose up -d

It will pull all the relevant images, configure the needed networks, volumes and bring up all the services.

To change the images version edit the .env file and set it to the wanted version.

Creating a User for Your MongoDB Database

The docker-compose does not create the user that will be used by the flask app. To create a new user, first start an interactive shell on the mongodb container:

$ docker exec -it mongodb bash

Once inside the container, log in to the MongoDB root administrative account:

# mongo -u mongodbuser -p

You will be prompted for the password that you entered as the value for the MONGO_INITDB_ROOT_PASSWORD variable in the docker-compose.yml file. The password can be changed by setting a new value for the MONGO_INITDB_ROOT_PASSWORD in the mongodb service, in which case you will have to re-run the docker-compose up -d command.

Execute the use command to switch to the flaskdb database:

mongodb> use flaskdb

Next, create a new user that will be allowed to access this database:

mongodb> db.createUser({user: 'flaskuser', pwd: 'your password', roles: [{role: 'readWrite', db: 'flaskdb'}]})
mongodb> exit

This command creates a user named flaskuser with readWrite access to the flaskdb database.

Log in to the authenticated database with the following command:

# mongo -u flaskuser -p your password --authenticationDatabase flaskdb

Now that you have added the user, log out of the database.

mongodb> exit

And finally, exit the container:

# exit

Running the Flask App

Now that your services are configured and running, you can test your application by navigating to http://your_server_ip in a browser.

TODO

  • Refine the search output
  • Create init script for MongoDB to create users as part of docker-compose up process.

About

Simple app uses Flask and MongoDB as backend and Nginx as frontend

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •