Skip to content

Commit

Permalink
Added collection struct for contexts to reduce number of arguments (c…
Browse files Browse the repository at this point in the history
…lippy complained)
  • Loading branch information
williamwoldum committed Nov 27, 2023
1 parent b8d59d9 commit e509573
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 107 deletions.
21 changes: 21 additions & 0 deletions src/api/context_collection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::api::hashing_context::HashingContextTrait;
use crate::api::server::server::ecdar_backend_server::EcdarBackend;
use crate::database::access_context::AccessContextTrait;
use crate::database::in_use_context::InUseContextTrait;
use crate::database::model_context::ModelContextTrait;
use crate::database::query_context::QueryContextTrait;
use crate::database::session_context::SessionContextTrait;
use crate::database::user_context::UserContextTrait;
use std::sync::Arc;

#[derive(Clone)]
pub struct ContextCollection {
pub(crate) access_context: Arc<dyn AccessContextTrait>,
pub(crate) in_use_context: Arc<dyn InUseContextTrait>,

Check warning on line 14 in src/api/context_collection.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

fields `in_use_context` and `model_context` are never read

warning: fields `in_use_context` and `model_context` are never read --> src/api/context_collection.rs:14:16 | 12 | pub struct ContextCollection { | ----------------- fields in this struct 13 | pub(crate) access_context: Arc<dyn AccessContextTrait>, 14 | pub(crate) in_use_context: Arc<dyn InUseContextTrait>, | ^^^^^^^^^^^^^^ 15 | pub(crate) model_context: Arc<dyn ModelContextTrait>, | ^^^^^^^^^^^^^ | = note: `ContextCollection` has a derived impl for the trait `Clone`, but this is intentionally ignored during dead code analysis = note: `#[warn(dead_code)]` on by default

Check warning on line 14 in src/api/context_collection.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

fields `in_use_context` and `model_context` are never read

warning: fields `in_use_context` and `model_context` are never read --> src/api/context_collection.rs:14:16 | 12 | pub struct ContextCollection { | ----------------- fields in this struct 13 | pub(crate) access_context: Arc<dyn AccessContextTrait>, 14 | pub(crate) in_use_context: Arc<dyn InUseContextTrait>, | ^^^^^^^^^^^^^^ 15 | pub(crate) model_context: Arc<dyn ModelContextTrait>, | ^^^^^^^^^^^^^ | = note: `ContextCollection` has a derived impl for the trait `Clone`, but this is intentionally ignored during dead code analysis = note: `#[warn(dead_code)]` on by default
pub(crate) model_context: Arc<dyn ModelContextTrait>,
pub(crate) query_context: Arc<dyn QueryContextTrait>,
pub(crate) session_context: Arc<dyn SessionContextTrait>,
pub(crate) user_context: Arc<dyn UserContextTrait>,
pub(crate) reveaal_context: Arc<dyn EcdarBackend>,
pub(crate) hashing_context: Arc<dyn HashingContextTrait>,
}
92 changes: 42 additions & 50 deletions src/api/ecdar_api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::api::hashing_context::HashingContextTrait;
use crate::api::context_collection::ContextCollection;
use crate::api::server::server::get_auth_token_request::user_credentials;
use crate::entities::access;
use crate::entities::session;
Expand All @@ -8,10 +8,6 @@ use std::sync::Arc;
use tonic::{Code, Request, Response, Status};

use crate::api::server::server::{ecdar_api_auth_server::EcdarApiAuth, ecdar_api_server::EcdarApi};
use crate::database::access_context::AccessContextTrait;
use crate::database::in_use_context::InUseContextTrait;
use crate::database::model_context::ModelContextTrait;
use crate::database::query_context::QueryContextTrait;
use crate::database::session_context::SessionContextTrait;
use crate::database::user_context::UserContextTrait;
use crate::entities::query;
Expand All @@ -31,14 +27,7 @@ use super::{

#[derive(Clone)]
pub struct ConcreteEcdarApi {
access_context: Arc<dyn AccessContextTrait>,
in_use_context: Arc<dyn InUseContextTrait>,
model_context: Arc<dyn ModelContextTrait>,
query_context: Arc<dyn QueryContextTrait>,
session_context: Arc<dyn SessionContextTrait>,
user_context: Arc<dyn UserContextTrait>,
reveaal_context: Arc<dyn EcdarBackend>,
hashing_context: Arc<dyn HashingContextTrait>,
contexts: ContextCollection,
}

/// Updates or creates a session in the database for a given user.
Expand Down Expand Up @@ -122,26 +111,8 @@ fn is_valid_username(username: &str) -> bool {
}

impl ConcreteEcdarApi {
pub fn new(
access_context: Arc<dyn AccessContextTrait>,
in_use_context: Arc<dyn InUseContextTrait>,
model_context: Arc<dyn ModelContextTrait>,
query_context: Arc<dyn QueryContextTrait>,
session_context: Arc<dyn SessionContextTrait>,
user_context: Arc<dyn UserContextTrait>,
reveaal_context: Arc<dyn EcdarBackend>,
hashing_context: Arc<dyn HashingContextTrait>,
) -> Self {
ConcreteEcdarApi {
access_context,
in_use_context,
model_context,
query_context,
session_context,
user_context,
reveaal_context,
hashing_context,
}
pub fn new(contexts: ContextCollection) -> Self {
ConcreteEcdarApi { contexts }
}
}

Expand Down Expand Up @@ -183,7 +154,7 @@ impl EcdarApi for ConcreteEcdarApi {
user_id: access.user_id,
};

match self.access_context.create(access).await {
match self.contexts.access_context.create(access).await {
Ok(_) => Ok(Response::new(())),
Err(error) => Err(Status::new(Code::Internal, error.to_string())),
}
Expand All @@ -209,7 +180,7 @@ impl EcdarApi for ConcreteEcdarApi {
user_id: Default::default(),
};

match self.access_context.update(access).await {
match self.contexts.access_context.update(access).await {
Ok(_) => Ok(Response::new(())),
Err(error) => Err(Status::new(Code::Internal, error.to_string())),
}
Expand All @@ -223,7 +194,12 @@ impl EcdarApi for ConcreteEcdarApi {
&self,
request: Request<DeleteAccessRequest>,
) -> Result<Response<()>, Status> {
match self.access_context.delete(request.get_ref().id).await {
match self
.contexts
.access_context
.delete(request.get_ref().id)
.await
{
Ok(_) => Ok(Response::new(())),
Err(error) => match error {
sea_orm::DbErr::RecordNotFound(message) => {
Expand All @@ -249,6 +225,7 @@ impl EcdarApi for ConcreteEcdarApi {

// Get user from database
let user = self
.contexts
.user_context
.get_by_id(uid)
.await
Expand Down Expand Up @@ -279,13 +256,13 @@ impl EcdarApi for ConcreteEcdarApi {
None => user.email,
},
password: match message.clone().password {
Some(password) => self.hashing_context.hash_password(password),
Some(password) => self.contexts.hashing_context.hash_password(password),
None => user.password,
},
};

// Update user in database
match self.user_context.update(new_user).await {
match self.contexts.user_context.update(new_user).await {
Ok(_) => Ok(Response::new(())),
Err(error) => Err(Status::new(Code::Internal, error.to_string())),
}
Expand All @@ -300,7 +277,7 @@ impl EcdarApi for ConcreteEcdarApi {
let uid = get_uid_from_request(&request)?;

// Delete user from database
match self.user_context.delete(uid).await {
match self.contexts.user_context.delete(uid).await {
Ok(_) => Ok(Response::new(())),
Err(error) => Err(Status::new(Code::Internal, error.to_string())),
}
Expand All @@ -322,7 +299,7 @@ impl EcdarApi for ConcreteEcdarApi {
model_id: query_request.model_id,
};

match self.query_context.create(query).await {
match self.contexts.query_context.create(query).await {
Ok(_) => Ok(Response::new(())),
Err(error) => Err(Status::new(Code::Internal, error.to_string())),
}
Expand All @@ -340,6 +317,7 @@ impl EcdarApi for ConcreteEcdarApi {
let message = request.get_ref().clone();

let old_query_res = self
.contexts
.query_context
.get_by_id(message.id)
.await
Expand All @@ -358,7 +336,7 @@ impl EcdarApi for ConcreteEcdarApi {
outdated: old_query.outdated,
};

match self.query_context.update(query).await {
match self.contexts.query_context.update(query).await {
Ok(_) => Ok(Response::new(())),
Err(error) => Err(Status::new(Code::Internal, error.to_string())),
}
Expand All @@ -371,7 +349,12 @@ impl EcdarApi for ConcreteEcdarApi {
&self,
request: Request<DeleteQueryRequest>,
) -> Result<Response<()>, Status> {
match self.query_context.delete(request.get_ref().id).await {
match self
.contexts
.query_context
.delete(request.get_ref().id)
.await
{
Ok(_) => Ok(Response::new(())),
Err(error) => match error {
sea_orm::DbErr::RecordNotFound(message) => {
Expand Down Expand Up @@ -426,8 +409,11 @@ impl EcdarApiAuth for ConcreteEcdarApi {
// Get user from credentials
if let Some(user_credentials) = message.user_credentials {
let input_password = user_credentials.password.clone();
user_from_db =
get_auth_find_user_helper(Arc::clone(&self.user_context), user_credentials).await?;
user_from_db = get_auth_find_user_helper(
Arc::clone(&self.contexts.user_context),
user_credentials,
)
.await?;

// Check if password in request matches users password
if input_password != user_from_db.password {
Expand Down Expand Up @@ -457,7 +443,7 @@ impl EcdarApiAuth for ConcreteEcdarApi {

// Update or create session in database
handle_session(
self.session_context.clone(),
self.contexts.session_context.clone(),
&request,
is_new_session,
access_token.clone(),
Expand Down Expand Up @@ -493,7 +479,7 @@ impl EcdarApiAuth for ConcreteEcdarApi {
email: message.clone().email,
};

match self.user_context.create(user).await {
match self.contexts.user_context.create(user).await {
Ok(_) => Ok(Response::new(())),
Err(e) => match e.sql_err() {
Some(SqlErr::UniqueConstraintViolation(e)) => {
Expand All @@ -517,28 +503,34 @@ impl EcdarBackend for ConcreteEcdarApi {
&self,
_request: Request<()>,
) -> Result<Response<UserTokenResponse>, Status> {
self.reveaal_context.get_user_token(_request).await
self.contexts.reveaal_context.get_user_token(_request).await
}

async fn send_query(
&self,
request: Request<QueryRequest>,
) -> Result<Response<QueryResponse>, Status> {
self.reveaal_context.send_query(request).await
self.contexts.reveaal_context.send_query(request).await
}

async fn start_simulation(
&self,
request: Request<SimulationStartRequest>,
) -> Result<Response<SimulationStepResponse>, Status> {
self.reveaal_context.start_simulation(request).await
self.contexts
.reveaal_context
.start_simulation(request)
.await
}

async fn take_simulation_step(
&self,
request: Request<SimulationStepRequest>,
) -> Result<Response<SimulationStepResponse>, Status> {
self.reveaal_context.take_simulation_step(request).await
self.contexts
.reveaal_context
.take_simulation_step(request)
.await
}
}

Expand Down
1 change: 1 addition & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod auth;
pub mod context_collection;
pub mod ecdar_api;
pub mod hashing_context;
pub mod reveaal_context;
Expand Down
31 changes: 4 additions & 27 deletions src/api/server.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
use std::env;
use std::sync::Arc;

use tonic::transport::Server;

use crate::api::auth;
use crate::api::context_collection::ContextCollection;
use crate::api::ecdar_api::ConcreteEcdarApi;
use crate::api::hashing_context::HashingContextTrait;
use crate::api::server::server::ecdar_api_auth_server::EcdarApiAuthServer;
use crate::api::server::server::ecdar_api_server::EcdarApiServer;
use crate::api::server::server::ecdar_backend_server::{EcdarBackend, EcdarBackendServer};
use crate::database::access_context::AccessContextTrait;
use crate::database::in_use_context::InUseContextTrait;
use crate::database::model_context::ModelContextTrait;
use crate::database::query_context::QueryContextTrait;
use crate::database::session_context::SessionContextTrait;
use crate::database::user_context::UserContextTrait;
use crate::api::server::server::ecdar_backend_server::EcdarBackendServer;

pub mod server {
tonic::include_proto!("ecdar_proto_buf");
}

Check warning on line 14 in src/api/server.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

module has the same name as its containing module

warning: module has the same name as its containing module --> src/api/server.rs:12:1 | 12 | / pub mod server { 13 | | tonic::include_proto!("ecdar_proto_buf"); 14 | | } | |_^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#module_inception = note: `#[warn(clippy::module_inception)]` on by default

Check warning on line 14 in src/api/server.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

module has the same name as its containing module

warning: module has the same name as its containing module --> src/api/server.rs:12:1 | 12 | / pub mod server { 13 | | tonic::include_proto!("ecdar_proto_buf"); 14 | | } | |_^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#module_inception = note: `#[warn(clippy::module_inception)]` on by default

pub async fn start_grpc_server(
access_context: Arc<dyn AccessContextTrait>,
in_use_context: Arc<dyn InUseContextTrait>,
model_context: Arc<dyn ModelContextTrait>,
query_context: Arc<dyn QueryContextTrait>,
session_context: Arc<dyn SessionContextTrait>,
user_context: Arc<dyn UserContextTrait>,
reveaal_context: Arc<dyn EcdarBackend>,
hashing_context: Arc<dyn HashingContextTrait>,
contexts: ContextCollection,
) -> Result<(), Box<dyn std::error::Error>> {
// defining address for our service
let addr = env::var("API_ADDRESS")
Expand All @@ -38,16 +24,7 @@ pub async fn start_grpc_server(

println!("Starting grpc server on '{}'", addr);

let svc = ConcreteEcdarApi::new(
access_context,
in_use_context,
model_context,
query_context,
session_context,
user_context,
reveaal_context,
hashing_context,
);
let svc = ConcreteEcdarApi::new(contexts);

// adding services to our server.
Server::builder()
Expand Down
32 changes: 12 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod database;
mod entities;
mod tests;

use crate::api::context_collection::ContextCollection;
use crate::api::hashing_context::HashingContext;
use crate::api::reveaal_context::ReveaalContext;
use crate::database::access_context::AccessContext;
Expand Down Expand Up @@ -33,27 +34,18 @@ async fn main() -> Result<(), Box<dyn Error>> {
_ => panic!("Database protocol not supported"),
};

let model_context = Arc::new(ModelContext::new(db_context.clone()));
let user_context = Arc::new(UserContext::new(db_context.clone()));
let access_context = Arc::new(AccessContext::new(db_context.clone()));
let query_context = Arc::new(QueryContext::new(db_context.clone()));
let session_context = Arc::new(SessionContext::new(db_context.clone()));
let in_use_context = Arc::new(InUseContext::new(db_context.clone()));
let reveaal_context = Arc::new(ReveaalContext);
let hashing_context = Arc::new(HashingContext);
let contexts = ContextCollection {
access_context: Arc::new(AccessContext::new(db_context.clone())),
in_use_context: Arc::new(InUseContext::new(db_context.clone())),
model_context: Arc::new(ModelContext::new(db_context.clone())),
query_context: Arc::new(QueryContext::new(db_context.clone())),
session_context: Arc::new(SessionContext::new(db_context.clone())),
user_context: Arc::new(UserContext::new(db_context.clone())),
reveaal_context: Arc::new(ReveaalContext),
hashing_context: Arc::new(HashingContext),
};

start_grpc_server(
access_context,
in_use_context,
model_context,
query_context,
session_context,
user_context,
reveaal_context,
hashing_context,
)
.await
.unwrap();
start_grpc_server(contexts).await.unwrap();

Ok(())
}
Loading

0 comments on commit e509573

Please sign in to comment.