Browse Source

Add database connection handling with dummy usage

master
Leonora Tindall 2 years ago
parent
commit
20f219793d
Signed by: nora GPG Key ID: 99041B68DBC02DAC
  1. 6
      database/Dockerfile
  2. 1
      webserver/Dockerfile
  3. 5
      webserver/src/config.py
  4. 15
      webserver/src/database.py
  5. 4
      webserver/src/webserver.py

6
database/Dockerfile

@ -1,7 +1,7 @@
FROM postgres:11
ENV POSTGRES_USER flittr
ENV POSTGRES_PASSWORD flittr
ENV POSTGRES_DB flittr
ENV POSTGRES_USER codepage
ENV POSTGRES_PASSWORD codepage
ENV POSTGRES_DB codepage
ENV PGDATA /var/lib/postgresql/data/pgdata
VOLUME /var/lib/postgresql/data/pgdata

1
webserver/Dockerfile

@ -1,5 +1,6 @@
FROM python:3
RUN pip install -U Flask
RUN pip install psycopg2
WORKDIR /
ADD ./src/ /src/
CMD ["python", "/src/webserver.py"]

5
webserver/src/config.py

@ -0,0 +1,5 @@
DB_NAME = "codepage"
DB_USER = "codepage"
DB_PASS = "codepage"
DB_HOST = "database"
DB_PORT = 5432

15
webserver/src/database.py

@ -0,0 +1,15 @@
"""
Database handling code for the CodePage
"""
import psycopg2
import config
def getdb():
return psycopg2.connect(
dbname=config.DB_NAME,
user=config.DB_USER,
password=config.DB_PASS,
host=config.DB_HOST,
port=config.DB_PORT
)

4
webserver/src/webserver.py

@ -1,10 +1,12 @@
from flask import Flask
from database import getdb
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, world"
with getdb() as db:
return "Hello, world"
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')

Loading…
Cancel
Save