Add database connection handling with dummy usage

This commit is contained in:
Leonora Tindall 2019-09-09 17:03:34 -05:00
parent fa5268f608
commit 20f219793d
Signed by: nora
GPG Key ID: 99041B68DBC02DAC
5 changed files with 27 additions and 4 deletions

View File

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

View File

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

5
webserver/src/config.py Normal file
View File

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

15
webserver/src/database.py Normal file
View File

@ -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
)

View File

@ -1,9 +1,11 @@
from flask import Flask from flask import Flask
from database import getdb
app = Flask(__name__) app = Flask(__name__)
@app.route("/") @app.route("/")
def hello(): def hello():
with getdb() as db:
return "Hello, world" return "Hello, world"
if __name__ == '__main__': if __name__ == '__main__':