Compare commits

...

3 Commits

Author SHA1 Message Date
Leonora Tindall b375760aab
Create basic database schema, and creation script
makedb should only have idempotent migrations, so it can be run on an
existing database.
2019-09-09 17:43:55 -05:00
Leonora Tindall 20f219793d
Add database connection handling with dummy usage 2019-09-09 17:03:34 -05:00
Leonora Tindall fa5268f608
Move webserver source into a subdirectory. 2019-09-09 16:44:55 -05:00
7 changed files with 57 additions and 6 deletions

View File

@ -0,0 +1,3 @@
## Setup
Create the database schema `docker-compose exec webserver python src/makedb.py`

View File

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

View File

@ -1,4 +1,6 @@
FROM python:3
RUN pip install -U Flask
ADD hello.py ./
CMD ["python", "hello.py"]
RUN pip install psycopg2
WORKDIR /
ADD ./src/ /src/
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

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

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

20
webserver/src/makedb.py Normal file
View File

@ -0,0 +1,20 @@
from database import getdb
with getdb() as db:
# Initial database schema
db.execute("""
CREATE TABLE IF NOT EXISTS users (
id INT PRIMARY KEY NOT NULL,
dispname TEXT NOT NULL,
pwhash TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS codes (
id INT PRIMARY KEY NOT NULL,
username INT NOT NULL,
language TEXT NOT NULL,
title TEXT NOT NULL,
contents TEXT NOT NULL,
FOREIGN KEY (username) REFERENCES users(id)
);
""")

View File

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