Giter Site home page Giter Site logo

drorata / docker-flask-mongodb-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from danionescu0/docker-flask-mongodb-example

0.0 1.0 0.0 7 KB

Uses docker compose with a python flask microservice and mongodb instance to make a sample application

Dockerfile 6.19% Python 93.81%

docker-flask-mongodb-example's Introduction

Purpose

A working demo usage of docker, docker-compose, mongodb, python3, docker-compose, mosquitto:

First usecase an api that generates random numbers and lists them

Second one deals with crud operations CRUD (create, read, update, delete) application over a user collection

The third one will use a MQTT server (Mosquitto) to allow to publish sensor updates over MQTT The updates will be saved in mongodb (/demo/sensors). It will also compute a running average for each sensor and publish it to a separate topic

The applications will run in parallel using docker-compose

  1. random_demo will run on port 80
  2. the crud on port 81
  3. MQTT service will run on default port 1883

Technollogies involved

Docker, docker-compose, python, flask microframework [Mosquitto MQTT] (https://mosquitto.org/)

Run the microservice

docker-compose up

Using the microservice

For applications 1 and 2 after we start the server using the command above, we'll be testing the requests using linux [curl][https://curl.haxx.se/docs/manpage.html]

For the application 3 we'll be using mosquitto cli to test the pub-sub it's working

To install mosquitto cli tool:

sudo apt-get install mosquitto-clients
  1. Random service

The random number collection has only one documents with '_is' lasts and an "items" key that will be a capped array

  • Generate a random number between 10 and 100:
curl -i "http://localhost/random/10/100"
  • View last 5 generated numbers list:
curl -i "http://localhost/random-list"
  1. CRUD service

The user collections contains _id which is the userid, a name and a email:

  • PUT request, this request will add a user with a userid, name and email:

example: add a new user with name dan email [email protected] and userid 189

curl -X PUT -d [email protected] -d name=dan http://localhost:81/users/189
  • POST request, this request will modify a user name or email or both:

example, modify the email of the user with id 10

curl -X POST -d [email protected]  http://localhost:81/users/10

  • GET request, this request will output as json the id, name and email of the user

example for with id 1:

curl -i "http://localhost:81/users/1"
  • DELETE request, this request will delete a user with a specified id

example for with id 1:

curl -X DELETE -i "http://localhost:81/users/1"
  1. MQTT service
  • To update a sensor with id: "some_sensor" and value "some_value" use:
mosquitto_pub -h localhost  -p 1883  -d -t some_sensor -m some_value

This will publish to mosquitto in a topic named "some_sensor" and value "some_value".

Our python script will listen to this topic too, and save in the mongo sensors collections the value for the sensor in a capped array.

After it writes the new values, it reads the last 5 values and computes an average (running average) and publish it to topic "average/some_sensor"

  • To get updates for sensor "some_sensor" use:
mosquitto_sub -h localhost  -p 1883 -d -t some_sensor

This will just listen for updates for the topic "some_sensor"

  • To get updates for the average 5 values for sensor "some_sensor" use:
mosquitto_sub -h localhost  -p 1883 -d -t averages/some_sensor

This will just listen for updates for the topic "averages/some_sensor" and get a running average for the sensor "some_sensor". Each time someone publishes a new value for a sensor, the python script will calculate the average values of last 5 values and publishes it

docker-flask-mongodb-example's People

Contributors

danionescu0 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.