Browse Source

add rest endpoint for generating random prayer

Pi 1 năm trước cách đây
mục cha
commit
571736c638
2 tập tin đã thay đổi với 26 bổ sung0 xóa
  1. 25 0
      rest/pyapi.py
  2. 1 0
      rest/requirements.txt

+ 25 - 0
rest/pyapi.py

@@ -0,0 +1,25 @@
+# Copyright (c) Daniel Sheffield 2022
+# All rights reserved.
+from bottle import route, run, template, response
+import psycopg
+from psycopg import Cursor
+import os
+user = os.getenv('USER')
+password = os.getenv('PASSWORD')
+host = 'host=localhost'
+conn: Cursor = psycopg.connect(f"{host} dbname=pgdb user={user} {password}")
+statement = """SELECT
+  table_to_xml_and_xmlschema('pg_random_view_default_if_null'::regclass,
+  false, false, ''::text);"""
+heading = """<?xml version="1.0" encoding="UTF-8"?>
+<?xml-stylesheet type="text/xsl" href="https://shandan.one/xml/pg_view_style.xsl"?>
+"""
+
+@route('/pyapi/random')
+def index():
+    with conn.cursor() as cur:
+        xml = cur.execute(statement).fetchone()[0]
+    response.content_type = 'application/xhtml+xml; charset=utf-8'
+    return f"{heading}{xml}"
+
+run(host='192.168.0.20', port=11888)

+ 1 - 0
rest/requirements.txt

@@ -0,0 +1 @@
+bottle