From bb791bd32111c4694a37768742005f37006be266 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20P=C3=A9rez=20Arcas?= Date: Mon, 30 Mar 2026 08:29:23 +0000 Subject: [PATCH] Eliminar app.py --- app.py | 93 ---------------------------------------------------------- 1 file changed, 93 deletions(-) delete mode 100644 app.py diff --git a/app.py b/app.py deleted file mode 100644 index 4831f8b..0000000 --- a/app.py +++ /dev/null @@ -1,93 +0,0 @@ -from flask import Flask, request, jsonify -from flask_cors import CORS -import os -import psycopg2 -import time - -app = Flask(__name__) -CORS(app) - -DB_HOST = os.getenv("DB_HOST", "db") -DB_NAME = os.getenv("DB_NAME", "notesdb") -DB_USER = os.getenv("DB_USER", "notesuser") -DB_PASSWORD = os.getenv("DB_PASSWORD", "notessecret") -DB_PORT = os.getenv("DB_PORT", "5432") - -def get_connection(): - return psycopg2.connect( - host=DB_HOST, - database=DB_NAME, - user=DB_USER, - password=DB_PASSWORD, - port=DB_PORT - ) - -def init_db(): - for _ in range(20): - try: - conn = get_connection() - cur = conn.cursor() - cur.execute(""" - CREATE TABLE IF NOT EXISTS notes ( - id SERIAL PRIMARY KEY, - content TEXT NOT NULL - ); - """) - conn.commit() - cur.close() - conn.close() - return - except Exception: - time.sleep(2) - raise Exception("Database not ready") - -@app.route("/api/health", methods=["GET"]) -def health(): - return jsonify({"status": "ok"}) - -@app.route("/api/notes", methods=["GET"]) -def get_notes(): - conn = get_connection() - cur = conn.cursor() - cur.execute("SELECT id, content FROM notes ORDER BY id DESC;") - rows = cur.fetchall() - cur.close() - conn.close() - return jsonify([{"id": r[0], "content": r[1]} for r in rows]) - -@app.route("/api/notes", methods=["POST"]) -def add_note(): - data = request.get_json() - content = data.get("content", "").strip() - - if not content: - return jsonify({"error": "Content is required"}), 400 - - conn = get_connection() - cur = conn.cursor() - cur.execute("INSERT INTO notes (content) VALUES (%s) RETURNING id;", (content,)) - note_id = cur.fetchone()[0] - conn.commit() - cur.close() - conn.close() - - return jsonify({"id": note_id, "content": content}), 201 - -@app.route("/api/notes/", methods=["DELETE"]) -def delete_note(note_id): - conn = get_connection() - cur = conn.cursor() - cur.execute("DELETE FROM notes WHERE id = %s RETURNING id;", (note_id,)) - deleted = cur.fetchone() - conn.commit() - cur.close() - conn.close() - - if deleted is None: - return jsonify({"error": "Note not found"}), 404 - - return jsonify({"message": "Note deleted successfully"}) - -if __name__ == "__main__": - init_db() - app.run(host="0.0.0.0", port=5000) \ No newline at end of file