Przeglądaj źródła

dockerize REST API

Pi 1 rok temu
rodzic
commit
0a6c6f745c
3 zmienionych plików z 117 dodań i 4 usunięć
  1. 3 3
      app/rest/Dockerfile
  2. 113 1
      app/rest/pyapi.py
  3. 1 0
      app/rest/requirements.txt

+ 3 - 3
app/rest/Dockerfile

@@ -1,6 +1,6 @@
 FROM python:3-slim
 WORKDIR /usr/src/app
-COPY  requirements.txt ./
+COPY app/rest/requirements.txt ./
 RUN pip install --no-cache-dir -r requirements.txt
-COPY pyapi.py .
-CMD [ "python", "./pyapi.py" ]
+COPY app ./app
+CMD [ "python", "-m", "app.rest.pyapi" ]

+ 113 - 1
app/rest/pyapi.py

@@ -55,4 +55,116 @@ def trend():
     response.content_type = 'image/svg+xml; charset=utf-8'
     return f.getvalue()
 
-run(host='127.0.0.1', port=6772)
+heading = """<?xml version="1.0" encoding="UTF-8"?>
+<?xml-stylesheet type="text/xsl" href="/grocery/style/table"?>
+"""
+
+style = """
+<xsl:stylesheet version="1.0"
+    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+    xmlns="http://www.w3.org/1999/xhtml"
+>
+
+  <xsl:output method="xml"
+      doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
+      doctype-public="-//W3C/DTD XHTML 1.0 Strict//EN"
+      indent="yes"/>
+
+  <xsl:template match="/*">
+    <xsl:variable name="schema" select="//xsd:schema"/>
+    <xsl:variable name="tabletypename"
+                  select="$schema/xsd:element[@name=name(current())]/@type"/>
+    <xsl:variable name="rowtypename"
+                  select="$schema/xsd:complexType[@name=$tabletypename]/xsd:sequence/xsd:element[@name='row']/@type"/>
+
+    <html>
+      <head>
+        <title><xsl:value-of select="name(current())"/></title>
+        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/purecss@2.1.0/build/pure-min.css" integrity="sha384-yHIFVG6ClnONEA5yB5DJXfW2/KC173DIQrYoZMEtBvGzmf0PKiGyNEqe9N6BNDBH" crossorigin="anonymous"/>
+        <link rel="stylesheet" href="https://shandan.one/css/grids-responsive-min.css"/>
+        <link rel="stylesheet" href="https://shandan.one/css/responsive-visibility-collapse.css"/>
+      </head>
+      <body>
+        <table class="pure-table pure-table-bordered pure-table-striped">
+          <tr>
+            <xsl:for-each select="$schema/xsd:complexType[@name=$rowtypename]/xsd:sequence/xsd:element/@name">
+              <th><xsl:value-of select="."/></th>
+            </xsl:for-each>
+          </tr>
+
+          <xsl:for-each select="row">
+            <tr>
+              <xsl:for-each select="*">
+                <td><xsl:value-of select="."/></td>
+              </xsl:for-each>
+            </tr>
+          </xsl:for-each>
+        </table>
+      </body>
+    </html>
+  </xsl:template>
+
+</xsl:stylesheet>
+"""
+@route('/grocery/style/table')
+def groups():
+    response.content_type = 'application/xhtml+xml; charset=utf-8'
+    return style
+
+@route('/grocery/groups')
+def groups():
+    try:
+        with conn.cursor() as cur:
+            xml = cur.execute("""
+SELECT query_to_xml_and_xmlschema('SELECT
+  g.name AS "Group"
+FROM groups g
+ORDER BY 1', false, false, ''::text)
+""").fetchone()[0].splitlines()
+    finally:
+        conn.commit()
+    #response.set_header('Access-Control-Allow-Origin', 'https://shandan.one')
+    #response.set_header('Access-Control-Allow-Origin', '*')
+    #response.set_header('Access-Control-Allow-Methods', 'GET')
+    #response.set_header('Access-Control-Allow-Headers', 'text/xml, application/xml, application/xhtml+xml, text/xsl, application/rss+xml, application/atom+xml')
+    response.content_type = 'application/xhtml+xml; charset=utf-8'
+    return f"{heading}" + '\n'.join(xml)
+
+@route('/grocery/categories')
+def groups():
+    try:
+        with conn.cursor() as cur:
+            xml = cur.execute("""
+SELECT query_to_xml_and_xmlschema('SELECT
+  c.name AS "Category",
+  g.name AS "Group"
+FROM categories c
+JOIN groups g ON c.group_id = g.id
+ORDER BY 1, 2', false, false, ''::text)
+""").fetchone()[0].splitlines()
+    finally:
+        conn.commit()
+    response.content_type = 'application/xhtml+xml; charset=utf-8'
+    return f"{heading}" + '\n'.join(xml)
+
+@route('/grocery/products')
+def groups():
+    try:
+        with conn.cursor() as cur:
+            xml = cur.execute("""
+SELECT query_to_xml_and_xmlschema('SELECT
+  p.name AS "Product",
+  c.name AS "Category",
+  g.name AS "Group"
+FROM products p
+JOIN categories c ON p.category_id = c.id
+JOIN groups g ON c.group_id = g.id
+ORDER BY 1, 2, 3', false, false, ''::text)
+""").fetchone()[0].splitlines()
+    finally:
+        conn.commit()
+    response.content_type = 'application/xhtml+xml; charset=utf-8'
+    return f"{heading}" + '\n'.join(xml)
+
+run(host='0.0.0.0', port=6772)

+ 1 - 0
app/rest/requirements.txt

@@ -1,2 +1,3 @@
+seaborn
 psycopg[binary]
 bottle