Skip to content

Commit

Permalink
Merge pull request #39 from MyK00L/contest-service
Browse files Browse the repository at this point in the history
Contest service
  • Loading branch information
eutampieri authored Oct 13, 2021
2 parents 46f4b15 + 30ba04c commit dfef87e
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 110 deletions.
89 changes: 0 additions & 89 deletions contest_service/db/init.js

This file was deleted.

7 changes: 4 additions & 3 deletions contest_service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use tonic::{transport::*, Request, Response, Status};

mod mappings;

mod mongo_schema;
#[cfg(test)]
mod tests;

Expand All @@ -34,9 +35,9 @@ pub struct ContestService {

impl ContestService {
async fn new() -> Result<Self, Box<dyn std::error::Error>> {
Ok(Self {
db_client: Client::with_options(ClientOptions::parse(CONNECTION_STRING).await?)?,
})
let db_client = Client::with_options(ClientOptions::parse(CONNECTION_STRING).await?)?;
mongo_schema::init_contest_service_db(db_client.database("contestdb")).await?;
Ok(Self { db_client })
}

/// Do not call this function, call get_*_collection or get_contest_metadata instead
Expand Down
10 changes: 5 additions & 5 deletions contest_service/src/mappings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ pub mod user {
#[derive(Clone)]
pub struct User {
username: String,
name: String,
fullname: String,
password: Password,
}

Expand Down Expand Up @@ -318,7 +318,7 @@ pub mod user {
fn from(record: Document) -> Self {
Self {
username: record.get_str("_id").unwrap().to_owned(),
name: record.get_str("fullName").unwrap().to_owned(),
fullname: record.get_str("fullname").unwrap().to_owned(),
// Here I assume that the password stored in the DB are hashed, since I hash them before insertion
password: Password::Hashed(record.get_str("password").unwrap().to_owned()),
}
Expand All @@ -328,7 +328,7 @@ pub mod user {
fn from(pb: protos::service::contest::SetUserRequest) -> Self {
Self {
username: pb.username,
name: pb.fullname,
fullname: pb.fullname,
password: Password::Clear(pb.password),
}
}
Expand All @@ -339,7 +339,7 @@ pub mod user {
u.hash_password().expect("Could not hash password");
let mut result = Document::new();
result.insert("_id", u.username);
result.insert("longName", u.name);
result.insert("fullname", u.fullname);
result.insert(
"password",
{
Expand All @@ -357,7 +357,7 @@ pub mod user {
fn from(u: User) -> Self {
Self::Success(protos::service::contest::auth_user_response::Success {
username: u.username,
fullname: u.name,
fullname: u.fullname,
})
}
}
Expand Down
124 changes: 124 additions & 0 deletions contest_service/src/mongo_schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
use mongodb::{
bson::doc,
options::{CreateCollectionOptions, ValidationAction, ValidationLevel},
Database,
};

pub async fn init_contest_service_db(db: Database) -> Result<(), Box<dyn std::error::Error>> {
// TODO: consider using this validator syntax (might be slightly nicer):
// https://docs.mongodb.com/v5.0/core/schema-validation/#other-query-expressions
db.create_collection(
"contest_metadata",
CreateCollectionOptions::builder()
.validator(doc! {
"$jsonSchema": {
"bsonType": "object",
"required": ["name", "description"],
"properties": {
"name": { "bsonType": "string" },
"description": { "bsonType": "string" },
"startTime": { "bsonType": "timestamp" }, // missing means there is no start time
"endTime": { "bsonType": "timestamp" } // missing means there is no end time
}
}
})
.validation_action(ValidationAction::Error)
.validation_level(ValidationLevel::Strict)
.capped(true)
.max(1)
// Size must be set if we want to used capped. SWe set it to an unreachable value (1 MB) so
// it will never be reached by our singleton document (enforced by max(1)).
.size(2_u64.pow(20))
.build(),
)
.await?;

db.create_collection(
"problems",
CreateCollectionOptions::builder()
.validator(doc! {
"$jsonSchema": {
"bsonType": "object",
"required": ["_id", "name", "longName", "statement"],
"properties": {
"_id": { "bsonType": "int" }, // problem id
"name": { "bsonType": "string" },
"longName": { "bsonType": "string" },
"statement": { "bsonType": "binData" }
}
}
})
.validation_action(ValidationAction::Error)
.validation_level(ValidationLevel::Strict)
.build(),
)
.await?;

db.create_collection(
"users",
CreateCollectionOptions::builder()
.validator(doc! {
"$jsonSchema": {
"bsonType": "object",
"required": ["_id", "fullname", "password"],
"properties": {
"_id": { "bsonType": "string" }, // username
"fullname": { "bsonType": "string" },
"password": { "bsonType": "string" }
}
}
})
.validation_action(ValidationAction::Error)
.validation_level(ValidationLevel::Strict)
.build(),
)
.await?;

db.create_collection(
"announcements",
CreateCollectionOptions::builder()
.validator(doc! {
"$jsonSchema": {
"bsonType": "object",
"required": ["_id", "subject", "text", "created"],
"properties": {
"_id": { "bsonType": "int" }, // announcement id
"subject": { "bsonType": "string" },
"problemId": { "bsonType": "int" },
"text": { "bsonType": "string" },
"to": { "bsonType": "string" },
"created": { "bsonType": "timestamp" }
}
}
})
.validation_action(ValidationAction::Error)
.validation_level(ValidationLevel::Strict)
.build(),
)
.await?;

db.create_collection(
"questions",
CreateCollectionOptions::builder()
.validator(doc! {
"$jsonSchema": {
"bsonType": "object",
"required": ["_id", "subject", "text", "created"],
"properties": {
"_id": { "bsonType": "int" }, // question id
"subject": { "bsonType": "string" },
"problemId": { "bsonType": "int" },
"text": { "bsonType": "string" },
"from": { "bsonType": "string" },
"created": { "bsonType": "timestamp" }
}
}
})
.validation_action(ValidationAction::Error)
.validation_level(ValidationLevel::Strict)
.build(),
)
.await?;

Ok(())
}
25 changes: 12 additions & 13 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,30 @@ version: "3.3"

services:
contest_service_db:
image: mongo:4.4.8
image: mongo:4.4.8 #mongo:5.0.0
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
MONGO_INITDB_DATABASE: contestdb
volumes:
- ./contest_service/db/init.js:/docker-entrypoint-initdb.d/init.js
# - contest_service_db_volume:/data/db
ports:
# volumes:
# - contest_service_db_volume:/data/db
ports:
- 27017:27017
contest_service:
build:
context: .
dockerfile: ./contest_service/Dockerfile
ports:
ports:
- 50051:50051
depends_on:
depends_on:
- contest_service_db

evaluation_service:
build:
context: .
dockerfile: ./evaluation_service/Dockerfile
ports:
ports:
- 50052:50051

submission_service_db:
Expand All @@ -39,18 +38,18 @@ services:
# volumes:
# - ./submission_service/db/init.js:/docker-entrypoint-initdb.d/init.js
# - submission_service_db_volume:/data/db
ports:
ports:
- 27020:27017
submission_service:
build:
context: .
dockerfile: ./submission_service/Dockerfile
ports:
ports:
- 50055:50051
depends_on:
depends_on:
- submission_service_db


volumes:
contest_service_db_volume:
submission_service_db_volume: # what for?
submission_service_db_volume: # what for?

0 comments on commit dfef87e

Please sign in to comment.