forked from spinframework/spin-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
29 lines (22 loc) · 1020 Bytes
/
Copy pathapp.py
File metadata and controls
29 lines (22 loc) · 1020 Bytes
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
from spin_sdk import http, postgres
from spin_sdk.http import Request, Response
from spin_sdk.postgres import RowSet, DbValue
def format_value(db_value: DbValue) -> str:
if hasattr(db_value, "value"):
return str(db_value.value)
return "NULL"
def format_rowset(rowset: RowSet) -> str:
lines = []
col_names = [col.name for col in rowset.columns]
lines.append(" | ".join(col_names))
lines.append("-" * len(lines[0]))
for row in rowset.rows:
values = [format_value(v) for v in row]
lines.append(" | ".join(values))
return "\n".join(lines)
class WasiHttpHandler030Rc20260315(http.Handler):
async def handle_request(self, request: Request) -> Response:
with await postgres.open("user=postgres dbname=spin_dev host=localhost sslmode=disable password=password") as db:
rowset = db.query("SELECT * FROM test", [])
result = format_rowset(rowset)
return Response(200, {"content-type": "text/plain"}, bytes(result, "utf-8"))