Compare commits
3 Commits
42860188c6
...
b375760aab
Author | SHA1 | Date |
---|---|---|
Leonora Tindall | b375760aab | |
Leonora Tindall | 20f219793d | |
Leonora Tindall | fa5268f608 |
|
@ -0,0 +1,3 @@
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
Create the database schema `docker-compose exec webserver python src/makedb.py`
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
FROM python:3
|
FROM python:3
|
||||||
RUN pip install -U Flask
|
RUN pip install -U Flask
|
||||||
ADD hello.py ./
|
RUN pip install psycopg2
|
||||||
CMD ["python", "hello.py"]
|
WORKDIR /
|
||||||
|
ADD ./src/ /src/
|
||||||
|
CMD ["python", "/src/webserver.py"]
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
DB_NAME = "codepage"
|
||||||
|
DB_USER = "codepage"
|
||||||
|
DB_PASS = "codepage"
|
||||||
|
DB_HOST = "database"
|
||||||
|
DB_PORT = 5432
|
|
@ -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()
|
||||||
|
|
|
@ -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)
|
||||||
|
);
|
||||||
|
""")
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
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():
|
||||||
return "Hello, world"
|
with getdb() as db:
|
||||||
|
return "Hello, world"
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(debug=True, host='0.0.0.0')
|
app.run(debug=True, host='0.0.0.0')
|
Loading…
Reference in New Issue