-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
164 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,36 @@ | ||
FROM python:3.12-slim | ||
|
||
RUN apt-get update && apt-get install make -yqq \ | ||
RUN apt-get update && apt-get install -yqq \ | ||
make \ | ||
postgresql-15 \ | ||
sudo | ||
sudo \ | ||
curl | ||
|
||
RUN pip install poetry | ||
|
||
ENV POETRY_VIRTUALENVS_IN_PROJECT=true | ||
|
||
COPY ./pg_hba.conf /etc/postgresql/15/main/pg_hba.conf | ||
|
||
WORKDIR /app | ||
|
||
COPY . . | ||
COPY pyproject.toml . | ||
|
||
RUN poetry install | ||
|
||
#USER postgres | ||
COPY . . | ||
|
||
COPY init.sql /docker-entrypoint-initdb.d/ | ||
|
||
# postgres config | ||
RUN echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/15/main/pg_hba.conf && \ | ||
echo "listen_addresses='*'" >> /etc/postgresql/15/main/postgresql.conf | ||
|
||
# create docker user and db | ||
RUN service postgresql start && \ | ||
su postgres -c "psql --command \"CREATE USER docker WITH SUPERUSER PASSWORD 'docker';\"" && \ | ||
su postgres -c "createdb -O docker docker" && \ | ||
service postgresql stop | ||
|
||
COPY run.sh ./run.sh | ||
RUN chmod +x ./run.sh | ||
|
||
#COPY ./run.sh . | ||
#CMD ./run.sh | ||
CMD ./run.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
#!/bin/bash | ||
|
||
set -m | ||
set -e | ||
|
||
/usr/lib/postgresql/15/bin/postgres \ | ||
-D /var/lib/postgresql/15/main \ | ||
-c config_file=/etc/postgresql/15/main/postgresql.conf & | ||
service postgresql start | ||
|
||
sleep 5 && psql -a -f init.sql && make run | ||
until su postgres -c "pg_isready"; do | ||
echo "Waiting for postgres..." | ||
sleep 2 | ||
done | ||
|
||
fg %1 | ||
su postgres -c "psql -d docker -a -f /docker-entrypoint-initdb.d/init.sql" | ||
|
||
make prod |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,47 @@ | ||
import json | ||
import sys | ||
import uuid | ||
from flask import session | ||
import psycopg2 | ||
from psycopg2.extras import RealDictCursor | ||
|
||
|
||
class UserRepository(): | ||
def __init__(self): | ||
if 'user' not in session: | ||
session['user'] = {} | ||
class UserRepository: | ||
def __init__(self, db_url): | ||
self.db_url = db_url | ||
|
||
def get_connection(self): | ||
return psycopg2.connect(self.db_url) | ||
|
||
def get_content(self): | ||
return session['user'].values() | ||
with self.get_connection() as conn: | ||
with conn.cursor(cursor_factory=RealDictCursor) as cur: | ||
cur.execute("SELECT * FROM users") | ||
return cur.fetchall() | ||
|
||
def find(self, id): | ||
try: | ||
return session['user'][id] | ||
except KeyError: | ||
sys.stderr.write(f'Wrong user id: {id}') | ||
raise | ||
with self.get_connection() as conn: | ||
with conn.cursor(cursor_factory=RealDictCursor) as cur: | ||
cur.execute("SELECT * FROM users WHERE id = %s", (id,)) | ||
return cur.fetchone() | ||
|
||
def destroy(self, id): | ||
del session['user'][id] | ||
def save(self, user_data): | ||
with self.get_connection() as conn: | ||
with conn.cursor() as cur: | ||
if 'id' not in user_data: | ||
# New user | ||
cur.execute( | ||
"INSERT INTO users (name, email) VALUES (%s, %s) RETURNING id", | ||
(user_data['name'], user_data['email']) | ||
) | ||
user_data['id'] = cur.fetchone()[0] | ||
else: | ||
# Existing user | ||
cur.execute( | ||
"UPDATE users SET name = %s, email = %s WHERE id = %s", | ||
(user_data['name'], user_data['email'], user_data['id']) | ||
) | ||
conn.commit() | ||
return user_data['id'] | ||
|
||
def save(self, user): | ||
if not (user.get('name') and user.get('email')): | ||
raise Exception(f'Wrong data: {json.loads(user)}') | ||
if not user.get('id'): | ||
user['id'] = str(uuid.uuid4()) | ||
session['user'][user['id']] = user | ||
session['user'] = session['user'] | ||
return user['id'] | ||
def destroy(self, id): | ||
with self.get_connection() as conn: | ||
with conn.cursor() as cur: | ||
cur.execute("DELETE FROM users WHERE id = %s", (id,)) | ||
conn.commit() |