-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
172 lines (137 loc) · 5.3 KB
/
main.py
File metadata and controls
172 lines (137 loc) · 5.3 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import os
from datetime import datetime, timezone
from flask import Flask, jsonify, request
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
# ---------------------------------------------------------------------------
# Config
# ---------------------------------------------------------------------------
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv(
"DATABASE_URL", "sqlite:///dev.db"
)
# Railway Postgres URLs start with postgres:// — SQLAlchemy 1.4+ needs postgresql://
if app.config["SQLALCHEMY_DATABASE_URI"].startswith("postgres://"):
app.config["SQLALCHEMY_DATABASE_URI"] = app.config[
"SQLALCHEMY_DATABASE_URI"
].replace("postgres://", "postgresql://", 1)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY", "change-me-in-production")
CORS(app)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
# ---------------------------------------------------------------------------
# Models
# ---------------------------------------------------------------------------
class Item(db.Model):
__tablename__ = "items"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(120), nullable=False)
description = db.Column(db.Text, default="")
created_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc))
updated_at = db.Column(
db.DateTime,
default=lambda: datetime.now(timezone.utc),
onupdate=lambda: datetime.now(timezone.utc),
)
def to_dict(self):
return {
"id": self.id,
"name": self.name,
"description": self.description,
"created_at": self.created_at.isoformat(),
"updated_at": self.updated_at.isoformat(),
}
# ---------------------------------------------------------------------------
# Auto-create tables (no migration needed for quick start)
# ---------------------------------------------------------------------------
with app.app_context():
db.create_all()
# ---------------------------------------------------------------------------
# Error handlers
# ---------------------------------------------------------------------------
@app.errorhandler(404)
def not_found(e):
return jsonify({"error": "Resource not found"}), 404
@app.errorhandler(400)
def bad_request(e):
return jsonify({"error": str(e)}), 400
@app.errorhandler(500)
def internal_error(e):
return jsonify({"error": "Internal server error"}), 500
# ---------------------------------------------------------------------------
# Health / Root
# ---------------------------------------------------------------------------
@app.get("/")
def root():
return jsonify({
"service": "Flask REST API Boilerplate",
"status": "running",
"endpoints": {
"health": "/health",
"items": "/api/items",
"item": "/api/items/<id>",
},
})
@app.get("/health")
def health():
return jsonify({"status": "healthy", "timestamp": datetime.now(timezone.utc).isoformat()})
# ---------------------------------------------------------------------------
# CRUD — /api/items
# ---------------------------------------------------------------------------
@app.get("/api/items")
def list_items():
page = request.args.get("page", 1, type=int)
per_page = request.args.get("per_page", 20, type=int)
per_page = min(per_page, 100)
pagination = Item.query.order_by(Item.created_at.desc()).paginate(
page=page, per_page=per_page, error_out=False
)
return jsonify({
"items": [i.to_dict() for i in pagination.items],
"total": pagination.total,
"page": pagination.page,
"pages": pagination.pages,
})
@app.post("/api/items")
def create_item():
data = request.get_json(silent=True) or {}
if not data.get("name"):
return jsonify({"error": "Field 'name' is required"}), 400
item = Item(name=data["name"], description=data.get("description", ""))
db.session.add(item)
db.session.commit()
return jsonify(item.to_dict()), 201
@app.get("/api/items/<int:item_id>")
def get_item(item_id):
item = db.session.get(Item, item_id)
if not item:
return jsonify({"error": "Item not found"}), 404
return jsonify(item.to_dict())
@app.put("/api/items/<int:item_id>")
def update_item(item_id):
item = db.session.get(Item, item_id)
if not item:
return jsonify({"error": "Item not found"}), 404
data = request.get_json(silent=True) or {}
if "name" in data:
item.name = data["name"]
if "description" in data:
item.description = data["description"]
db.session.commit()
return jsonify(item.to_dict())
@app.delete("/api/items/<int:item_id>")
def delete_item(item_id):
item = db.session.get(Item, item_id)
if not item:
return jsonify({"error": "Item not found"}), 404
db.session.delete(item)
db.session.commit()
return jsonify({"message": "Deleted", "id": item_id})
# ---------------------------------------------------------------------------
# Run
# ---------------------------------------------------------------------------
if __name__ == "__main__":
port = int(os.getenv("PORT", 8080))
app.run(host="0.0.0.0", port=port, debug=os.getenv("FLASK_DEBUG", "0") == "1")