浏览代码

add dockerfile to rest api

Pi 1 年之前
父节点
当前提交
857d57aa8c
共有 3 个文件被更改,包括 15 次插入5 次删除
  1. 6 0
      rest/Dockerfile
  2. 8 5
      rest/pyapi.py
  3. 1 0
      rest/requirements.txt

+ 6 - 0
rest/Dockerfile

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

+ 8 - 5
rest/pyapi.py

@@ -4,10 +4,13 @@ from bottle import route, request, run, template, response, abort
 import psycopg
 from psycopg import Cursor, sql
 import os
-user = os.getenv('USER')
-password = os.getenv('PASSWORD')
-host = 'host=localhost'
-conn: Cursor = psycopg.connect(f"{host} dbname=pgdb user={user} {password}")
+host = f"host={os.getenv('HOST')}"
+db = f"dbname={os.getenv('DB', 'pgdb')}"
+user = f"user={os.getenv('USER', 'pgdb')}"
+password = f"password={os.getenv('PASSWORD','')}"
+if not password.split('=',1)[1]:
+    password = ''
+conn: Cursor = psycopg.connect(f"{host} {db} {user} {password}")
 random_statement = """SELECT
   query_to_xml('SELECT category, translation, reference, txt
     FROM pg_random_view_default_if_null
@@ -151,4 +154,4 @@ def specified():
     response.content_type = 'application/xhtml+xml; charset=utf-8'
     return f"{heading}{xml[0]}{specified_schema}" + '\n'.join(xml[1:])
 
-run(host='192.168.0.20', port=11888)
+run(host='0.0.0.0', port=11888)

+ 1 - 0
rest/requirements.txt

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