Initial database connection and initialization.
This commit is contained in:
parent
4f4b87e6ca
commit
344ad4e47b
6 changed files with 80 additions and 18 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -3,3 +3,5 @@
|
||||||
Pipfile.lock
|
Pipfile.lock
|
||||||
**/.vscode/
|
**/.vscode/
|
||||||
**/.idea/
|
**/.idea/
|
||||||
|
pg_data/.pgdb_pass
|
||||||
|
pg_data/*/
|
|
@ -20,6 +20,8 @@ WORKDIR /usr/src/uwume
|
||||||
RUN chown -R uwume:uwume /usr/src/uwume
|
RUN chown -R uwume:uwume /usr/src/uwume
|
||||||
USER 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 from local and install packages
|
||||||
COPY Pipfile Pipfile
|
COPY Pipfile Pipfile
|
||||||
RUN pipenv install
|
RUN pipenv install
|
||||||
|
|
|
@ -1,19 +1,49 @@
|
||||||
---
|
---
|
||||||
version: '3'
|
version: "3"
|
||||||
services:
|
services:
|
||||||
uwume:
|
uwume:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: Dockerfile
|
dockerfile: Dockerfile
|
||||||
image: uwume:latest
|
image: uwume:latest
|
||||||
user: uwume
|
user: uwume
|
||||||
container_name: uwu_me
|
container_name: uwu_me
|
||||||
environment:
|
networks:
|
||||||
- FLASK_APP=/usr/src/uwume/app
|
- net
|
||||||
- FLASK_ENV=development
|
ports:
|
||||||
- FLASK_RUN_HOST=0.0.0.0
|
- 4444:4444
|
||||||
- FLASK_RUN_PORT=4444
|
links:
|
||||||
volumes:
|
- postgres
|
||||||
- ./uwume:/usr/src/uwume/app
|
volumes:
|
||||||
ports:
|
- ./uwume:/usr/src/uwume/app
|
||||||
- 4444:4444
|
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:
|
||||||
|
|
0
pg_data/.gitkeep
Normal file
0
pg_data/.gitkeep
Normal file
|
@ -1,7 +1,14 @@
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from os import urandom
|
from os import urandom
|
||||||
|
from os import environ
|
||||||
|
try:
|
||||||
|
import uwume.lib.databaseMethods
|
||||||
|
except:
|
||||||
|
from .lib.databaseMethods import initialize_database
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.secret_key = urandom(12).hex()
|
app.secret_key = urandom(12).hex()
|
||||||
|
|
||||||
from . import uwume
|
initialize_database()
|
||||||
|
|
||||||
|
from . import uwume
|
21
uwume/lib/databaseMethods.py
Normal file
21
uwume/lib/databaseMethods.py
Normal file
|
@ -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}')
|
Reference in a new issue