Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
root = true

[*]
indent_style = tab
indent_size = 2
charset = utf-8

[**/*.sql]
indent_style = space
indent_size = 4


[*.yaml]
indent_style = space
indent_size = 2

[*.yml]
indent_style = space
indent_size = 2

52 changes: 30 additions & 22 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
name: pylint

on:
on:
pull_request:
branches:
master
branches: master

jobs:
setup:
Expand All @@ -15,54 +14,63 @@ jobs:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run : |
run: |
python -m pip install --upgrade pip
pip install pylint sqlfluff

check_store_service:
check_database_scripts:
runs-on: ubuntu-latest
needs : setup
needs: setup
steps:
- uses: actions/checkout@v4
- name: Install dependencies
run : |
pip install sqlfluff

- name: Run sqlfluff for PostgreSQL
run : sqlfluff lint --dialect postgres database/init-scripts/*.sql

- name: Run sqlfluff for bank-service
run : sqlfluff lint --dialect sqlite bank-service/database/bank-schema.sql

check_store_service:
runs-on: ubuntu-latest
needs: setup
steps:
- uses: actions/checkout@v4
- name: Install dependencies
run: |
pip install -r store-service/requirements.txt
pip install pylint sqlfluff

- name: Run pylint on store-service
run : pylint store-service/*.py store-service/*/*.py

- name: Check SQL schema in store-service
run : sqlfluff lint --dialect sqlite store-service/database/store-schema.sql
run: pylint store-service/*.py store-service/*/*.py

check_bank_service:
runs-on: ubuntu-latest
needs : setup
needs: setup
steps:
- uses: actions/checkout@v4
- name: Install dependecies
run : |
run: |
pip install -r bank-service/requirements.txt
pip install pylint sqlfluff
pip install pylint

- name: Run pylint on bank-service
run : pylint bank-service/*.py bank-service/*/*.py
run: pylint bank-service/*.py bank-service/*/*.py

- name: Check SQL schema in bank-service
run : sqlfluff lint --dialect sqlite bank-service/database/bank-schema.sql

all_test_success:
runs-on: ubuntu-latest
needs :
needs:
- check_store_service
- check_bank_service
steps :
- check_database_scripts
steps:
- name: Decide whether the needed jobs succeeded or failed
uses: re-actors/alls-green@v1.2.2
with:
with:
jobs: ${{ toJSON(needs) }}

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
.vscode
*.csv

.env

*.py[co]
__pycache__

*.crt
*.key

venv

193 changes: 162 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,64 +1,195 @@
# Database Management Systems course project

## Project Theme : Online service for digital distribution of computer games and programs
## Project theme : Online service for digital distribution of computer games and programs

### Project Description
## Project Description

#### TODO: FOR_THE_FUTURE
GameHub is an educational project demonstrating the work of a distributed system using PostgreSQL, microservice architecture and Docker. The project includes:

- Game Store (store-service): allows users to view games, add them to the cart, and make purchases.
- Banking service: processes transactions and manages the balance of users.
- Database: stores data about users, games, studios, tags, purchases, and media content.

## Stack : Docker, Python, PostgreSQL, HTML, CSS, Javascript.

## Generate test data

### Requierements for generator:

- python3
- python-pillow

### To generate random test data use(modify before running constants in deGenerator.py file):

#### Generate test data
##### Requierements for generator:
* python3
* python-pillow
##### To generate random test data use(modify before running constants in deGenerator.py file):
```
python3 deGenerator.py
python3 tools/deGenerator.py
```

#### Current architecture
![logical_store_schema](./store-schema.png)
### This utility will generate a set of initializing data for the store's database and put it in a set of initializing csv files.

![logical_bank_schema](./bank-schema.png)
### Variable parameters

#### About bank-service api
#### Below is a list of variable generator parameters:

```
USERS_COUNT = 100 # amount of users generated by the rule "user_name:password" username<id>:username<id>, where <id> from 1 to USERS_COUNT
STUDIO_COUNT = 10 # amount of studios with names studio<id>, where <id> from 1 to STUDIO_COUNT
TAGS_COUNT = 20 # amount of tags with names tag<id>, where <id> from 1 to TAGS_COUNT
GAMES_COUNT = 100 # amount of games with names game<id> from random studio from above, where <id> from 1 to GAMES_COUNT
MAX_SCREENSHOT_COUNT = 8 # amount of max-screenshot for one game. A number up to MAX_SCREENSHOT_COUNT is selected for each game.
GAME_TAGS_COUNT = 1000 # amount of associations of random tags created earlier with random games created earlier.
PURCHASES_COUNT = 1000 # amount of created purchases. With chance 50% create purchases with ts = NULL ( game in cart, not in library ) for random users.
```

## Environment set

### You need to set environment variables into .env file like this

```
STORE_DB_NAME=store-db
STORE_DB_PSWD=store-pswd
STORE_DB_USER=store-user

BANK_DB_NAME=bank-db
BANK_DB_PSWD=bank-store
BANK_DB_USER=bank-user

DB_PORT=51488
```

## About bank-service api ( for more information, see the wiki page )

### How to get user balance ( example )

##### How to get user balance ( example )
```
curl --insecure -X GET -H 'Content-Type: application/json' -u "seregga:seregga" "https://127.0.0.1:5001/api/balance"
```

##### How to add account in our great bank ( example )
### How to add account in our great bank ( example )

```
curl --insecure -X POST -H 'Content-Type: application/json' -d '{"uuid" : "test", "password" : "test"}' "https://127.0.0.1:5001/api/add-account"
```

##### How to delete bank account ( example )
### How to delete bank account ( example )

```
curl --insecure -X POST -H 'Content-Type: application/json' -u "test:test" "https://127.0.0.1:5001/api/delete-account"
```

##### How to transfer money from one account to another ( example )
### How to transfer money from one account to another ( example )

```
curl --insecure -X POST -H 'Content-Type: application/json' -u "test:test" -d '{"uuid_to" : "seregga", "amount" : 500 }' "https://127.0.0.1:5001/api/transfer"
```

##### Return codes for bank api service
* 401 -- authorization failed
* 402 -- wrong requested data
* 404 -- wrong request
### Return codes for bank api service

- 401 : authorization failed
- 402 : wrong requested data
- 404 : wrong request

## About store-service API (for more information, see the wiki page)

## Possible ways to launch a project

### Running services in docker on a local machine.

#### Requirements list for docker running:

- docker
- docker-compose
- openssl ( create certificates for your services )
- curl ( interaction with bank service )

#### How to run service in docker:

```
# create test data (optional)
python tools/deGenerator.py

# store.crt and store.key files for store service
cd store-service/
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout store.key -out store.crt
cd ..

# bank.crt and bank.key files for bank service
cd bank-service/
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout bank.key -out bank.crt
cd ..

# run containers in daemon mode
docker compose up --build -d

# stop services
docker compose down

# clear database volume
docker compose down --volumes
```

### Running services on local machine (for example, in systemd-nspawn)

#### Running systemd-nspawn container (and creation)

```
sudo debootstrap noble ./ubuntu-sandbox
sudo mount --bind ./ubuntu-sandbox ./ubuntu-sandbox
sudo chroot ./ubuntu-sandbox

passwd # set password for root in container
exit

sudo systemd-nspawn -b -D ./ubuntu-sandbox
```

After that you need to change **/etc/resolve.conf** for **nameserver 8.8.8.8** and **/etc/apt/source.list** for :

deb http://archive.ubuntu.com/ubuntu/ noble main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ noble-updates main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ noble-security main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ noble-backports main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ noble-proposed main restricted universe multiverse

#### Install dependencies

```
# install utils for os
apt install -y python3 libssl-dev nginx python3-pip python3-venv postgresql
python3 -m venv /opt/venv
export PATH="/opt/venv/bin:$PATH"

# install python requirements.txt
CFLAGS="-I/usr/local/opt/openssl/include" LDFLAGS="-L/usr/local/opt/openssl/lib" UWSGI_PROFILE_OVERRIDE=ssl=true pip3 install --no-cache-dir -r store-service/requirements.txt
pip install --no-cache-dir -r bank-service/requirements.txt
```

#### Run services (it is important at this stage to already have initialized environment variables (as in the dotenv file!!!) :
```
# run bank service
cd bank-service
python app.py

#run store service
cd store-service
uwsgi --ini uwsgi.ini
```

### Development Stack : Docker, Python(backend + frontend), sqlite3
## Special utilities for testing this project.

### Requirements list for running services on the local server :
* docker
* docker-compose
* openssl ( for certificates )
* curl ( for interaction with bank service )
These utilities are stored in the tools directory.

## The composition of our team:
* Gubanov Peter (@gubanovpm)
* Khrol Ivan (@ent1r)
* Potapova Anna (@ann37)
- deGenerator.py : generator of test data for store-service with some variables parameters;
- DOSer.py : checking for resistance to basic DOS;
- tester.py : running a specific sql query to a specific database from python-psycopg3.

## Our team:

- Gubanov Peter (@gubanovpm)
- Khrol Ivan (@ent1r)
- Potapova Anna (@ann37)

Binary file removed bank-schema.png
Binary file not shown.
5 changes: 2 additions & 3 deletions bank-service/app.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import os
import base64

from flask import Flask

from routes.routes import routes_blueprint
from database.db import init_database

from flask import Flask

app = Flask(__name__)


# initial state of database
init_database()

Expand Down
3 changes: 3 additions & 0 deletions check-ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

python3 -c "import yaml; yaml.safe_load(open('.github/workflows/ci.yml'))"
15 changes: 15 additions & 0 deletions database/init-scripts/10-bank-schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE IF NOT EXISTS accounts (
id INTEGER PRIMARY KEY,
balance INTEGER NOT NULL DEFAULT 0,
uuid TEXT NOT NULL UNIQUE,
phash TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS transactions (
id INTEGER PRIMARY KEY,
ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
amount INTEGER NOT NULL,
user_id INTEGER NOT NULL REFERENCES accounts (id),

FOREIGN KEY (user_id) REFERENCES accounts
);
Loading