From 344ad4e47b11a00495c98fdabe3a59c056e085a4 Mon Sep 17 00:00:00 2001 From: Alexander Huddleston Date: Sun, 8 Mar 2020 00:51:02 -0600 Subject: [PATCH] Initial database connection and initialization. --- .gitignore | 2 ++ Dockerfile | 2 ++ docker-compose.yaml | 64 ++++++++++++++++++++++++++---------- pg_data/.gitkeep | 0 uwume/__init__.py | 9 ++++- uwume/lib/databaseMethods.py | 21 ++++++++++++ 6 files changed, 80 insertions(+), 18 deletions(-) create mode 100644 pg_data/.gitkeep create mode 100644 uwume/lib/databaseMethods.py diff --git a/.gitignore b/.gitignore index 25c92ec..bfbf707 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ Pipfile.lock **/.vscode/ **/.idea/ +pg_data/.pgdb_pass +pg_data/*/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index d43ca7c..0761b9e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,6 +20,8 @@ WORKDIR /usr/src/uwume RUN chown -R uwume:uwume /usr/src/uwume USER uwume +COPY pg_data/.pgdb_pass /var/lib/postgresql/data/pg_data/.pgdb_pass + # Copy Pipfile from local and install packages COPY Pipfile Pipfile RUN pipenv install diff --git a/docker-compose.yaml b/docker-compose.yaml index c4508c4..1934dee 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,19 +1,49 @@ --- -version: '3' +version: "3" services: - uwume: - build: - context: . - dockerfile: Dockerfile - image: uwume:latest - user: uwume - container_name: uwu_me - environment: - - FLASK_APP=/usr/src/uwume/app - - FLASK_ENV=development - - FLASK_RUN_HOST=0.0.0.0 - - FLASK_RUN_PORT=4444 - volumes: - - ./uwume:/usr/src/uwume/app - ports: - - 4444:4444 \ No newline at end of file + uwume: + build: + context: . + dockerfile: Dockerfile + image: uwume:latest + user: uwume + container_name: uwu_me + networks: + - net + ports: + - 4444:4444 + links: + - postgres + volumes: + - ./uwume:/usr/src/uwume/app + environment: + FLASK_APP: /usr/src/uwume/app + FLASK_ENV: development + FLASK_RUN_HOST: 0.0.0.0 + FLASK_RUN_PORT: 4444 + POSTGRES_PASSWORD_FILE: /var/lib/postgresql/data/pg_data/.pgdb_pass + POSTGRES_DB: uwume_prod + POSTGRES_USER: uwume + POSTGRES_HOST: postgres + POSTGRES_PORT: 5432 + PGDATA: /var/lib/postgresql/data/pg_data/pgdb + restart: unless-stopped + postgres: + image: postgres:alpine + networks: + - net + ports: + - 5432:5432 + volumes: + - ./pg_data:/var/lib/postgresql/data/pg_data/ + environment: + POSTGRES_PASSWORD_FILE: /var/lib/postgresql/data/pg_data/.pgdb_pass + POSTGRES_DB: uwume_prod + POSTGRES_USER: uwume + POSTGRES_HOST: localhost + POSTGRES_PORT: 5432 + PGDATA: /var/lib/postgresql/data/pg_data/pgdb +networks: + net: +volumes: + pg_data: diff --git a/pg_data/.gitkeep b/pg_data/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/uwume/__init__.py b/uwume/__init__.py index add7ff2..4e485e9 100644 --- a/uwume/__init__.py +++ b/uwume/__init__.py @@ -1,7 +1,14 @@ from flask import Flask from os import urandom +from os import environ +try: + import uwume.lib.databaseMethods +except: + from .lib.databaseMethods import initialize_database app = Flask(__name__) app.secret_key = urandom(12).hex() -from . import uwume +initialize_database() + +from . import uwume \ No newline at end of file diff --git a/uwume/lib/databaseMethods.py b/uwume/lib/databaseMethods.py new file mode 100644 index 0000000..f85a9c9 --- /dev/null +++ b/uwume/lib/databaseMethods.py @@ -0,0 +1,21 @@ +from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String, Boolean +from os import environ + +pg_pass_file = open(environ['POSTGRES_PASSWORD_FILE']) +pg_pass = pg_pass_file.readline().strip('\n') +engine = create_engine( + f"postgresql+psycopg2://{environ['POSTGRES_USER']}:{pg_pass}@{environ['POSTGRES_HOST']}:{environ['POSTGRES_PORT']}/{environ['POSTGRES_DB']}") + + +def initialize_database(): + conn = engine.connect() + md = MetaData() + users = Table('users', md, + Column('id', Integer()), + Column('name', String(255)), + Column('pass', String(255)), + Column('count', Integer())) + try: + md.create_all(engine) + except Exception as e: + print(f'{e}')