-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathapp.py
More file actions
143 lines (122 loc) · 4.78 KB
/
Copy pathapp.py
File metadata and controls
143 lines (122 loc) · 4.78 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import os
import click
from flask import Flask, request, jsonify
import psycopg2
from psycopg2.extras import RealDictCursor
app = Flask(__name__)
DB_HOST = os.getenv("DB_HOST")
DB_NAME = os.getenv("DB_NAME")
DB_USER = os.getenv("DB_USER")
DB_PASSWORD = os.getenv("DB_PASSWORD")
def get_db_connection():
conn = psycopg2.connect(
host=DB_HOST,
database=DB_NAME,
user=DB_USER,
password=DB_PASSWORD
)
return conn
def init_db():
print("Tentando inicializar a tabela 'flags'...")
try:
conn = get_db_connection()
cur = conn.cursor()
cur.execute("""
CREATE TABLE IF NOT EXISTS flags (
id SERIAL PRIMARY KEY,
name VARCHAR(100) UNIQUE NOT NULL,
is_enabled BOOLEAN NOT NULL DEFAULT false,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
""")
conn.commit()
cur.close()
conn.close()
print("Tabela 'flags' inicializada com sucesso.")
except psycopg2.OperationalError as e:
print(f"Erro de conexão ao inicializar o banco de dados: {e}")
except Exception as e:
print(f"Um erro inesperado ocorreu durante a inicialização do DB: {e}")
@app.cli.command("init-db")
def init_db_command():
init_db()
@app.route('/health', methods=['GET'])
def health_check():
return jsonify({"status": "ok"}), 200
@app.route('/flags', methods=['POST'])
def create_flag():
data = request.get_json()
if not data or 'name' not in data:
return jsonify({"error": "O campo 'name' é obrigatório"}), 400
name = data['name']
is_enabled = data.get('is_enabled', False)
try:
conn = get_db_connection()
cur = conn.cursor()
cur.execute("INSERT INTO flags (name, is_enabled) VALUES (%s, %s)", (name, is_enabled))
conn.commit()
except psycopg2.IntegrityError:
return jsonify({"error": f"A flag '{name}' já existe"}), 409
except Exception as e:
return jsonify({"error": "Erro interno no servidor ao criar a flag", "details": str(e)}), 500
finally:
if 'cur' in locals() and not cur.closed:
cur.close()
if 'conn' in locals() and not conn.closed:
conn.close()
return jsonify({"message": f"Flag '{name}' criada com sucesso"}), 201
@app.route('/flags', methods=['GET'])
def get_flags():
try:
conn = get_db_connection()
cur = conn.cursor(cursor_factory=RealDictCursor)
cur.execute("SELECT name, is_enabled FROM flags ORDER BY name")
flags = cur.fetchall()
except Exception as e:
return jsonify({"error": "Erro interno no servidor ao buscar as flags", "details": str(e)}), 500
finally:
if 'cur' in locals() and not cur.closed:
cur.close()
if 'conn' in locals() and not conn.closed:
conn.close()
return jsonify(flags), 200
@app.route('/flags/<string:name>', methods=['GET'])
def get_flag_status(name):
try:
conn = get_db_connection()
cur = conn.cursor(cursor_factory=RealDictCursor)
cur.execute("SELECT name, is_enabled FROM flags WHERE name = %s", (name,))
flag = cur.fetchone()
except Exception as e:
return jsonify({"error": "Erro interno no servidor ao buscar a flag", "details": str(e)}), 500
finally:
if 'cur' in locals() and not cur.closed:
cur.close()
if 'conn' in locals() and not conn.closed:
conn.close()
if flag:
return jsonify(flag), 200
return jsonify({"error": "Flag não encontrada"}), 404
@app.route('/flags/<string:name>', methods=['PUT'])
def update_flag(name):
data = request.get_json()
if data is None or 'is_enabled' not in data or not isinstance(data['is_enabled'], bool):
return jsonify({"error": "O campo 'is_enabled' (booleano) é obrigatório"}), 400
is_enabled = data['is_enabled']
try:
conn = get_db_connection()
cur = conn.cursor()
cur.execute("UPDATE flags SET is_enabled = %s WHERE name = %s", (is_enabled, name))
if cur.rowcount == 0:
return jsonify({"error": "Flag não encontrada"}), 404
conn.commit()
except Exception as e:
return jsonify({"error": "Erro interno no servidor ao atualizar a flag", "details": str(e)}), 500
finally:
if 'cur' in locals() and not cur.closed:
cur.close()
if 'conn' in locals() and not conn.closed:
conn.close()
return jsonify({"message": f"Flag '{name}' atualizada"}), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)