-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
133 lines (99 loc) · 3.62 KB
/
Copy pathapp.py
File metadata and controls
133 lines (99 loc) · 3.62 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
import os
from datetime import datetime
from flask import Flask, jsonify, render_template, request
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import func
def build_database_url() -> str:
explicit_url = os.getenv("DATABASE_URL")
if explicit_url:
return explicit_url
db_user = os.getenv("DB_USER", "todo")
db_password = os.getenv("DB_PASSWORD", "todo")
db_host = os.getenv("DB_HOST", "localhost")
db_port = os.getenv("DB_PORT", "5432")
db_name = os.getenv("DB_NAME", "tododb")
return f"postgresql+psycopg2://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}"
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = build_database_url()
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
db_initialized = False
class Todo(db.Model):
__tablename__ = "todos"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(255), nullable=False)
description = db.Column(db.Text, nullable=True)
is_done = db.Column(db.Boolean, nullable=False, default=False)
created_at = db.Column(db.DateTime(timezone=True), nullable=False, server_default=func.now())
updated_at = db.Column(
db.DateTime(timezone=True),
nullable=False,
server_default=func.now(),
onupdate=datetime.utcnow,
)
def to_dict(self) -> dict:
return {
"id": self.id,
"title": self.title,
"description": self.description,
"is_done": self.is_done,
"created_at": self.created_at.isoformat() if self.created_at else None,
"updated_at": self.updated_at.isoformat() if self.updated_at else None,
}
@app.before_request
def ensure_tables_exist() -> None:
global db_initialized
if not db_initialized:
db.create_all()
db_initialized = True
@app.get("/healthz")
def healthz():
try:
db.session.execute(db.text("SELECT 1"))
return jsonify({"status": "ok"}), 200
except Exception as exc:
return jsonify({"status": "error", "detail": str(exc)}), 500
@app.get("/")
def index():
return render_template("index.html")
@app.get("/todos")
def list_todos():
todos = Todo.query.order_by(Todo.id.asc()).all()
return jsonify([todo.to_dict() for todo in todos]), 200
@app.post("/todos")
def create_todo():
data = request.get_json(silent=True) or {}
title = (data.get("title") or "").strip()
description = data.get("description")
if not title:
return jsonify({"error": "title is required"}), 400
todo = Todo(title=title, description=description)
db.session.add(todo)
db.session.commit()
return jsonify(todo.to_dict()), 201
@app.patch("/todos/<int:todo_id>")
def update_todo(todo_id: int):
todo = Todo.query.get_or_404(todo_id)
data = request.get_json(silent=True) or {}
if "title" in data:
title = (data.get("title") or "").strip()
if not title:
return jsonify({"error": "title cannot be empty"}), 400
todo.title = title
if "description" in data:
todo.description = data.get("description")
if "is_done" in data:
is_done = data.get("is_done")
if not isinstance(is_done, bool):
return jsonify({"error": "is_done must be boolean"}), 400
todo.is_done = is_done
db.session.commit()
return jsonify(todo.to_dict()), 200
@app.delete("/todos/<int:todo_id>")
def delete_todo(todo_id: int):
todo = Todo.query.get_or_404(todo_id)
db.session.delete(todo)
db.session.commit()
return "", 204
if __name__ == "__main__":
app.run(host="0.0.0.0", port=int(os.getenv("PORT", "8080")))