-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.py
64 lines (47 loc) · 1.62 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# from datetime import datetime
import os
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DB_URL']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class TinyWebDB(db.Model):
__tablename__ = 'tinywebdb'
tag = db.Column(db.String, primary_key=True, nullable=False)
value = db.Column(db.String, nullable=False)
# The 'date' column is needed for deleting older entries, so not really required
# date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
db.create_all()
db.session.commit()
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/storeavalue', methods=['POST'])
def store_a_value():
tag = request.form['tag']
value = request.form['value']
if tag:
# Prevent Duplicate Key error by updating the existing tag
existing_tag = TinyWebDB.query.filter_by(tag=tag).first()
if existing_tag:
existing_tag.value = value
db.session.commit()
else:
data = TinyWebDB(tag=tag, value=value)
db.session.add(data)
db.session.commit()
return jsonify(['STORED', tag, value])
return 'Invalid Tag!'
@app.route('/getvalue', methods=['POST'])
def get_value():
tag = request.form['tag']
if tag:
value = TinyWebDB.query.filter_by(tag=tag).first().value
return jsonify(['VALUE', tag, value])
return 'Invalid Tag!'
@app.route('/deleteentry')
def delete_entry():
return 'Not implemented!'
if __name__ == '__main__':
app.run()