55 lines
1.4 KiB
Python
55 lines
1.4 KiB
Python
from flask import Flask, render_template, request
|
|
import sqlite3 as sql
|
|
import xml.etree.ElementTree as ET
|
|
|
|
tree = ET.parse('config.xml') # parseer xml
|
|
root = tree.getroot()
|
|
|
|
# laad config items in python variabelen
|
|
flask_debug = bool(root.find('flask_debug').text == 'True') # vind node in xml en als waarde 'True' is zet variablele op True
|
|
db_location = root.find('sqlite_location').text
|
|
|
|
# initialiseer flask
|
|
app = Flask(__name__)
|
|
|
|
# initialiseer sqlite
|
|
conn = sql.connect(db_location)
|
|
|
|
def test_db():
|
|
cur.execute(''' SELECT name FROM sqlite_master WHERE type='table' AND name='hosts' ''')
|
|
|
|
if cur.fetchone()[0] == 0: # als tabel niet bestaat
|
|
print('tabel hosts bestaat niet, database wordt opnieuw aangemaakt')
|
|
|
|
|
|
def create_db_struct():
|
|
cur.execute(''' CREATE TABLE cijfers (cijfer INTEGER) ''') # maak tabel aan met 1 kolom
|
|
con.commit() # commit alle wijzigingen
|
|
|
|
|
|
def insert_data(data):
|
|
"""
|
|
simpele functie om de gekregen dict te inserten in de sqlite database
|
|
"""
|
|
|
|
|
|
|
|
#dit is de document root
|
|
@app.route('/')
|
|
def index():
|
|
return root
|
|
|
|
# POST DATA
|
|
@app.route('/api/v1/host/post', methods=['POST'])
|
|
def host_post():
|
|
data = request.get_json() # sla json data op in dict
|
|
return '{"status": 200}' # return een json dict met waarde status 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=flask_debug) # start flask |