Skip to content

Commit

Permalink
Merge pull request #8 from hexlet-components/cookies
Browse files Browse the repository at this point in the history
Cookies
  • Loading branch information
sgmdlt authored Aug 14, 2024
2 parents 0f1de69 + 3b16190 commit e64776f
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 46 deletions.
16 changes: 12 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
FROM python:3.12-slim

RUN apt-get update && apt-get install make -yq
RUN apt-get update && apt-get install make -yqq \
make \
postgresql-15 \
sudo

RUN pip install poetry

ENV POETRY_VIRTUALENVS_IN_PROJECT=true

COPY ./pg_hba.conf /etc/postgresql/15/main/pg_hba.conf

WORKDIR /app

COPY poetry.lock .
COPY . .

RUN poetry install

COPY . .
#USER postgres

CMD ["bash", "-c", "make prod"]
#COPY ./run.sh .
#CMD ./run.sh
22 changes: 20 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,25 @@ test:
check: test lint

run:
poetry run flask --app example --debug run --port 8000
poetry run flask --app example --debug run --host 0.0.0.0 --port $(PORT)

prod:
poetry run gunicorn --workers=4 --bind 0.0.0.0:$(PORT) hello_world:app --log-file -
poetry run gunicorn --workers=4 --bind 0.0.0.0:$(PORT) example:app --log-file -

compose:
docker compose up

compose-setup: compose-build
docker compose run app make install

compose-build:
docker compose build

compose-down:
docker compose down -v

compose-dev:
docker compose run app make run

compose-bash:
docker compose run app bash
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
app:
build:
context: .
ports:
- 8000:8000
command: make prod
environment:
DATABASE_URL: postgres://postgres:password@db:5432/postgres
5 changes: 5 additions & 0 deletions init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL
);
11 changes: 11 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

set -m

/usr/lib/postgresql/15/bin/postgres \
-D /var/lib/postgresql/15/main \
-c config_file=/etc/postgresql/15/main/postgresql.conf &

sleep 5 && psql -a -f init.sql && make run

fg %1
51 changes: 18 additions & 33 deletions user_repository.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,32 @@
import json
import sys
import uuid
from flask import session

class UserRepository:

class UserRepository():
def __init__(self):
self.users = json.load(open("./users.json", 'r'))
if 'user' not in session:
session['user'] = {}

def get_content(self):
return self.users
return session['user'].values()

def find(self, id):
try:
for user in self.users:
if str(id) == str(user['id']):
return user
return session['user'][id]
except KeyError:
sys.stderr.write(f'Wrong post id: {id}')
sys.stderr.write(f'Wrong user id: {id}')
raise

def save(self, user_data):
# repository should know nothing about validation in outer layer
if not (user_data.get('name') and user_data.get('email')):
raise Exception(f'Wrong data: {json.dumps(user_data)}')

if 'id' not in user_data:
new_user = user_data.copy()
new_user['id'] = str(uuid.uuid4())
self.users.append(new_user)
else:
for i, existing_user in enumerate(self.users):
if str(user_data['id']) == str(existing_user['id']):
self.users[i] = user_data
break
else:
raise Exception(f"User with id {user_data['id']} not found")

with open("./users.json", "w") as f:
json.dump(self.users, f)

return user_data['id']

def destroy(self, id):
current_user = self.find(id)
self.users.remove(current_user)
with open("./users.json", "w") as f:
json.dump(self.users, f)
del session['user'][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']
7 changes: 0 additions & 7 deletions users.json

This file was deleted.

0 comments on commit e64776f

Please sign in to comment.