Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
9ad0f42
alpha architecture
adariax May 30, 2022
884bd18
some django staff
adariax May 30, 2022
da41d34
Setup rust project
Jun 3, 2022
d68d196
docker (for rust and db)
adariax Jun 3, 2022
bc4c02d
Create database connection trait
Jun 6, 2022
e454270
[Rust] Implement basic database access trait
Jun 9, 2022
f17b15b
[Rust] Setup rocket server
Jun 9, 2022
9f025f7
[Rust] Server module and index
Jun 9, 2022
2a99c7b
[Rust] Rework sql access & finish server
Jun 9, 2022
fb16fa9
[Rust] Setup end-to-end testing
Jun 10, 2022
df660ea
Webapp file system structure creation
IlnurHA Jun 10, 2022
64fc0b1
Mapper sketch:
IlnurHA Jun 10, 2022
9fb3bc7
[Rust] Game creation and joining
Jun 10, 2022
e0d817e
Add templates
mpvnlv Jun 10, 2022
2bc5284
Change styles of main page
mpvnlv Jun 10, 2022
c77a1ba
Page of user
mpvnlv Jun 10, 2022
e828f29
[Rust] Setup game answer
Jun 10, 2022
8d9a9f4
Repository sketch added
IlnurHA Jun 10, 2022
8e8d4b1
add styles to registration page (need fixes)
Jun 11, 2022
c3a45f4
fix adaptive layout in reg_page
Jun 11, 2022
2ad52e4
Main: Resources added (User, Quiz, Question, Answer)
IlnurHA Jun 11, 2022
9792fa4
(Previous commit repeatition)
IlnurHA Jun 11, 2022
b71ea18
Testing API:
IlnurHA Jun 11, 2022
999defa
Resources, repository:
IlnurHA Jun 11, 2022
cc77a4e
flask_app is ready to start app
IlnurHA Jun 14, 2022
e3cabef
Repetition of the previous commit: flask_app is ready to start app
IlnurHA Jun 14, 2022
77ff637
Merge pull request #14 from InnoSWP/feature/models
adariax Jun 14, 2022
d2a605e
Merge pull request #13 from InnoSWP/feature/BKND-rust
adariax Jun 14, 2022
fc6bb44
Merge pull request #12 from InnoSWP/feature/FRND-init-templates
adariax Jun 14, 2022
a2aa097
python in docker
adariax Jun 14, 2022
ce040b2
Fix rust server db uri and docker
Jun 14, 2022
6c4fc47
docker for python
adariax Jun 16, 2022
0a8c593
Merge branch 'develop' of https://github.com/ADari-Ka/Intaby into dev…
adariax Jun 16, 2022
7099722
python docker fix
adariax Jun 16, 2022
ccfd1cd
Orm, repository amendings (little changes).
IlnurHA Jun 18, 2022
bb7639d
Test_api:
IlnurHA Jun 20, 2022
497e75e
Webhandlers for routes:
IlnurHA Jun 21, 2022
8b912cc
Structure of Model changed:
IlnurHA Jun 23, 2022
e14411b
Quiz and User Resources changes: \nAll requests require user credenti…
IlnurHA Jun 23, 2022
ccfe5bd
Beggining of Front-end and Back-end sides connection
IlnurHA Jun 25, 2022
2a4b573
Qui Resource
IlnurHA Jun 27, 2022
4b7f597
Repository:
IlnurHA Jun 28, 2022
3dc1f22
Orm:
IlnurHA Jun 28, 2022
f22670d
User reigtration:
IlnurHA Jun 29, 2022
8c44e5c
pyanalyze code
IlnurHA Jun 30, 2022
ee0a122
quiz_id info for api request
IlnurHA Jul 2, 2022
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/__pycache__
**/.idea
venv/
36 changes: 36 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
version: "3.8"

services:
rust_service:
build: rust-server/
ports:
- 8080:8080
depends_on:
- postgres_db

python_service:
build: web-app/
environment:
FLASK_ENV: development
FLASK_APP: app_start.py
ports:
- 8888:8888
depends_on:
- postgres_db

postgres_db:
image: postgres
container_name: database
restart: always
environment:
POSTGRES_DB: test
POSTGRES_USER: test
POSTGRES_PASSWORD: test
ports:
- 5432:5432
volumes:
- postgres_db:/var/lib/postgresql/data


volumes:
postgres_db:
1 change: 1 addition & 0 deletions rust-server/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
2 changes: 2 additions & 0 deletions rust-server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
Cargo.lock
16 changes: 16 additions & 0 deletions rust-server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "rust-server"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.8.5"
rocket = { version = "=0.5.0-rc.2", features = ["json"] }
async-trait = "0.1.56"

[dependencies.sqlx]
version = "0.5.13"
default-features = false
features = ["runtime-tokio-native-tls", "macros", "offline", "any", "postgres", "mysql", "sqlite", "mssql"]
7 changes: 7 additions & 0 deletions rust-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM rust:1.61

COPY ./ ./
COPY Cargo.toml Cargo.toml

RUN cargo build --release
CMD ["./target/release/rust-server"]
11 changes: 11 additions & 0 deletions rust-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Building

Install rust using rustup [here](https://www.rust-lang.org/tools/install)

Build the project using

```rust
cargo build --release
```

The executable file will be generated at `target/release/`
38 changes: 38 additions & 0 deletions rust-server/src/database/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use async_trait::async_trait;
pub use sqlx::any::AnyPool as DBPool;

pub mod sql;

pub type QuizId = u64;

/// The type of error that may occur when interacting
/// with the database
#[derive(Debug)]
pub enum DBError {
/// A game could not be created because another
/// with confliting properties already exists
QuizNotFound(QuizId),
/// The database is corrupted and could not be
/// processed properly
DBCorrupt(String),
/// Some other error occured
Other(Box<dyn std::error::Error>),
}

pub type DBResult<T> = Result<T, DBError>;

/// Trait representing a handler for the database
#[async_trait]
pub trait DBAccessor: Send + Sync {}

impl std::fmt::Display for DBError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::QuizNotFound(id) => write!(f, "Failed to find a quiz with id: {id}"),
Self::DBCorrupt(msg) => write!(f, "Database is corrupted: {msg}"),
Self::Other(error) => error.fmt(f),
}
}
}

impl std::error::Error for DBError {}
31 changes: 31 additions & 0 deletions rust-server/src/database/sql.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use super::*;

use sqlx::AnyPool;

/// Handles access to the postgres games database
pub struct SqlAccess {
pool: AnyPool,
}

impl SqlAccess {
/// Sets up access to the database and initializes all the neccessary tables
pub async fn new(db_uri: &str) -> DBResult<Self> {
let pool = DBPool::connect(db_uri).await?;
let sql = Self { pool }.init().await?;
Ok(sql)
}

/// Initialize all the neccessary database stuff
async fn init(self) -> DBResult<Self> {
Ok(self)
}
}

#[async_trait]
impl DBAccessor for SqlAccess {}

impl From<sqlx::Error> for DBError {
fn from(error: sqlx::Error) -> Self {
Self::Other(Box::new(error))
}
}
16 changes: 16 additions & 0 deletions rust-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
mod database;
mod server;

#[cfg(test)]
mod tests;

#[rocket::launch]
async fn launch() -> _ {
let config = rocket::Config {
log_level: rocket::log::LogLevel::Debug,
address: std::net::Ipv4Addr::new(0, 0, 0, 0).into(),
port: 8080,
..Default::default()
};
server::rocket(config, "postgresql://test:test@postgres_db:5432/test").await
}
89 changes: 89 additions & 0 deletions rust-server/src/server/games.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use rocket::serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Debug, Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct Games {
map: HashMap<GameCode, Game>,
}

/// Represents different states of a game
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
pub enum GameState {
/// Initial state of the game
Lobby,
InProgress,
Finished,
}

pub type GameCode = String;
pub type PlayerName = String;

#[derive(Debug, Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct Player {}

#[derive(Debug, Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct Game {
players: HashMap<PlayerName, Player>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct GameAnswer {}

impl Games {
pub fn new() -> Self {
Self {
map: HashMap::new(),
}
}

pub fn create_game(&mut self) -> GameCode {
let code = loop {
let code = game_code_generator();
if !self.map.contains_key(&code) {
break code;
}
};
let game = Game::new();
let res = self.map.insert(code.clone(), game);
assert!(res.is_none());
code
}

pub fn get_game(&self, code: &GameCode) -> Option<&Game> {
self.map.get(code)
}

pub fn get_game_mut(&mut self, code: &GameCode) -> Option<&mut Game> {
self.map.get_mut(code)
}
}

impl Game {
pub fn new() -> Self {
Self {
players: HashMap::new(),
}
}

pub fn player_join(&mut self, player_name: PlayerName) {
self.players.entry(player_name).or_insert_with(|| Player {});
}
}

pub fn game_code_generator() -> GameCode {
use rand::Rng;
const LEN: usize = 4;

let mut rng = rand::thread_rng();
let mut code = String::new();
for _ in 0..LEN {
let symbol = rng.gen_range('A'..='Z');
code.push(symbol);
}
code
}
Loading