forked from jonatasbaldin/web-counter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
81 lines (58 loc) · 1.96 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
from flask import Flask, jsonify
import psycopg2
# Initiate the Flask app
app = Flask(__name__)
# Database connection parameters
db_params = {
'dbname': os.getenv('DB_NAME'),
'user': os.getenv('DB_USER'),
'password': os.getenv('DB_PASSWORD'),
'host': os.getenv('DB_HOST'),
}
# Creates the "pressed" table
def create_pressed_table():
try:
# Creates the database connection
conn = psycopg2.connect(**db_params)
cur = conn.cursor()
# Creates de pressed table if doesn't exists
cur.execute('CREATE TABLE IF NOT EXISTS pressed (count INTEGER);')
# Selects the first row from the "pressed" table
cur.execute('SELECT 1 FROM pressed LIMIT 1;')
# If no row exists, insert an initial row with count = 1
if cur.fetchone() is None:
cur.execute('INSERT INTO pressed (count) VALUES (1);')
# Commits the changes to the database
conn.commit()
# If there's an error, prints to the consol
except Exception as e:
print(str(e))
# Closes the database connection
finally:
if conn:
conn.close()
# Route to increment the count field from the "pressed" database
@app.route('/api/pressed', methods=['GET'])
def increment_pressed():
try:
conn = psycopg2.connect(**db_params)
cur = conn.cursor()
# Increments the "pressed" table
cur.execute('UPDATE pressed SET count = count + 1;')
conn.commit()
# Gets the updated count
cur.execute('SELECT count FROM pressed;')
count = cur.fetchone()[0]
return jsonify({'count': count})
# Returns an error if any
except Exception as e:
return jsonify({'error': str(e)})
# Closes the database connection
finally:
if conn:
conn.close()
if __name__ == '__main__':
# Create the "pressed" table if it doesn't exist
create_pressed_table()
app.run(host='0.0.0.0', port=5000)