iot-with-document-store-db

git clone git://git.codymlewis.com/iot-with-document-store-db.git
Log | Files | Refs | README

Server.py (847B)


      1 #!/usr/bin/env python3
      2 # -*- coding: utf-8 -*-
      3 
      4 from json.encoder import JSONEncoder
      5 import requests
      6 
      7 from flask import Flask, render_template
      8 app = Flask(__name__)
      9 
     10 
     11 DB_ADDR = f"http://127.0.0.1:5984/sensors"
     12 
     13 JSONENCODER = JSONEncoder()
     14 
     15 # NODE_DATA = get_node_data()
     16 
     17 
     18 @app.route("/")
     19 def home():
     20     return render_template('index.html', title="Map")
     21 
     22 
     23 @app.route("/nodes/<node_id>")
     24 def show_node(node_id):
     25     data = requests.get(f"{DB_ADDR}/{node_id}").json()
     26     return render_template('node.html', title=f"{node_id}", data=data)
     27 
     28 
     29 @app.route("/node-data")
     30 def get_node_data():
     31     data = requests.post(f"{DB_ADDR}/_find", data=JSONENCODER.encode({
     32         "selector": {
     33             "id": {
     34                 "$gt": None
     35             }
     36         }
     37     }), headers={"Content-Type": "application/json"}).json()
     38     return JSONENCODER.encode(data)