diff --git a/src/backend/git.rs b/src/backend/git.rs new file mode 100644 index 0000000..e9e6658 --- /dev/null +++ b/src/backend/git.rs @@ -0,0 +1,65 @@ +use std::error::Error; + +use crate::{ + command::repository::{ + CreateRepositoryCommand, CreateRepositoryResponse, RepositoryFileInspectionCommand, + RepositoryFileInspectionResponse, RepositoryInfoRequest, RepositoryIssueLabelsRequest, + RepositoryIssueLabelsResponse, RepositoryIssuesCountRequest, RepositoryIssuesCountResponse, + RepositoryIssuesRequest, RepositoryIssuesResponse, + }, + model::repository::RepositoryView, +}; + +use super::RepositoryBackend; + +pub struct GitBackend; + +impl GitBackend { + pub fn new() -> Self { + Self + } +} + +impl RepositoryBackend for GitBackend { + fn create_repository( + &mut self, + request: &CreateRepositoryCommand, + ) -> Result> { + todo!() + } + + fn repository_info( + &mut self, + request: &RepositoryInfoRequest, + ) -> Result> { + todo!() + } + + fn repository_file_inspect( + &mut self, + request: &RepositoryFileInspectionCommand, + ) -> Result> { + todo!() + } + + fn repository_issues_count( + &mut self, + request: &RepositoryIssuesCountRequest, + ) -> Result> { + todo!() + } + + fn repository_issue_labels( + &mut self, + request: &RepositoryIssueLabelsRequest, + ) -> Result> { + todo!() + } + + fn repository_issues( + &mut self, + request: &RepositoryIssuesRequest, + ) -> Result> { + todo!() + } +} diff --git a/src/backend/mod.rs b/src/backend/mod.rs new file mode 100644 index 0000000..b1de6d6 --- /dev/null +++ b/src/backend/mod.rs @@ -0,0 +1,41 @@ +pub mod git; + +use std::error::Error; + +use crate::{ + command::repository::{ + CreateRepositoryCommand, CreateRepositoryResponse, RepositoryFileInspectionCommand, + RepositoryFileInspectionResponse, RepositoryInfoRequest, RepositoryIssueLabelsRequest, + RepositoryIssueLabelsResponse, RepositoryIssuesCountRequest, RepositoryIssuesCountResponse, + RepositoryIssuesRequest, RepositoryIssuesResponse, + }, + model::repository::RepositoryView, +}; + +pub trait RepositoryBackend { + fn create_repository( + &mut self, + request: &CreateRepositoryCommand, + ) -> Result>; + fn repository_info( + &mut self, + request: &RepositoryInfoRequest, + ) -> Result>; + fn repository_file_inspect( + &mut self, + request: &RepositoryFileInspectionCommand, + ) -> Result>; + + fn repository_issues_count( + &mut self, + request: &RepositoryIssuesCountRequest, + ) -> Result>; + fn repository_issue_labels( + &mut self, + request: &RepositoryIssueLabelsRequest, + ) -> Result>; + fn repository_issues( + &mut self, + request: &RepositoryIssuesRequest, + ) -> Result>; +} diff --git a/src/command/repository.rs b/src/command/repository.rs index e1f5423..496a870 100644 --- a/src/command/repository.rs +++ b/src/command/repository.rs @@ -34,7 +34,7 @@ pub enum RepositoryResponse { CreateRepository(CreateRepositoryResponse), RepositoryFileInspection(RepositoryFileInspectionResponse), RepositoryInfo(RepositoryView), - IssuesCount(IssuesCountResponse), + IssuesCount(RepositoryIssuesCountResponse), IssueLabels(RepositoryIssueLabelsResponse), Issues(RepositoryIssuesResponse), } diff --git a/src/connection.rs b/src/connection.rs index 63808bb..d4e4111 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -13,6 +13,7 @@ use tokio::{ use tokio_tungstenite::{tungstenite::Message, WebSocketStream}; use crate::{ + backend::RepositoryBackend, command::{ issues::IssuesCountResponse, repository::{ @@ -56,6 +57,7 @@ pub async fn connection_worker( mut socket: WebSocketStream, listeners: Arc>, mut connections: Arc>, + backend: Arc>, addr: SocketAddr, ) { let mut handshaked = false; @@ -183,20 +185,76 @@ pub async fn connection_worker( // This message is targeting this instance match &repository.command { RepositoryMessageKind::Request(request) => match request { - RepositoryRequest::CreateRepository(_) => todo!(), - RepositoryRequest::RepositoryFileInspection(_) => { - let response = RepositoryFileInspectionResponse::File { - commit_metadata: CommitMetadata::default(), + RepositoryRequest::CreateRepository(request) => { + let mut backend = backend.lock().await; + let response = backend.create_repository(request); + + let response = match response { + Ok(response) => response, + Err(err) => { + error!("Error handling request: {:?}", err); + continue; + } }; + drop(backend); + + socket + .send(Message::Binary( + serde_json::to_vec(&MessageKind::Repository( + RepositoryMessage { + target: repository.target.clone(), + command: RepositoryMessageKind::Response( + RepositoryResponse::CreateRepository(response), + ), + }, + )) + .unwrap(), + )) + .await + .unwrap(); + } + RepositoryRequest::RepositoryFileInspection(request) => { + let mut backend = backend.lock().await; + let response = backend.repository_file_inspect(request); + + let response = match response { + Ok(response) => response, + Err(err) => { + error!("Error handling request: {:?}", err); + continue; + } + }; + drop(backend); + + socket + .send(Message::Binary( + serde_json::to_vec(&MessageKind::Repository( + RepositoryMessage { + target: repository.target.clone(), + command: RepositoryMessageKind::Response( + RepositoryResponse::RepositoryFileInspection( + response, + ), + ), + }, + )) + .unwrap(), + )) + .await + .unwrap(); } - RepositoryRequest::RepositoryInfo(_) => { - let response = RepositoryView { - name: String::from("Nederland"), - description: String::from("ik hou van het nederland"), - default_branch: String::from("nederland"), - latest_commit: CommitMetadata::default(), - files: vec![], + RepositoryRequest::RepositoryInfo(request) => { + let mut backend = backend.lock().await; + let response = backend.repository_info(request); + + let response = match response { + Ok(response) => response, + Err(err) => { + error!("Error handling request: {:?}", err); + continue; + } }; + drop(backend); socket .send(Message::Binary( @@ -213,9 +271,18 @@ pub async fn connection_worker( .await .unwrap(); } - RepositoryRequest::IssuesCount(_) => { - let response: IssuesCountResponse = - IssuesCountResponse { count: 727420 }; + RepositoryRequest::IssuesCount(request) => { + let mut backend = backend.lock().await; + let response = backend.repository_issues_count(request); + + let response = match response { + Ok(response) => response, + Err(err) => { + error!("Error handling request: {:?}", err); + continue; + } + }; + drop(backend); socket .send(Message::Binary( @@ -232,9 +299,18 @@ pub async fn connection_worker( .await .unwrap(); } - RepositoryRequest::IssueLabels(_) => { - let response = RepositoryIssueLabelsResponse { labels: vec![] }; - + RepositoryRequest::IssueLabels(request) => { + let mut backend = backend.lock().await; + let response = backend.repository_issue_labels(request); + + let response = match response { + Ok(response) => response, + Err(err) => { + error!("Error handling request: {:?}", err); + continue; + } + }; + drop(backend); socket .send(Message::Binary( serde_json::to_vec(&MessageKind::Repository( @@ -250,8 +326,18 @@ pub async fn connection_worker( .await .unwrap(); } - RepositoryRequest::Issues(_) => { - let response = RepositoryIssuesResponse { issues: vec![] }; + RepositoryRequest::Issues(request) => { + let mut backend = backend.lock().await; + let response = backend.repository_issues(request); + + let response = match response { + Ok(response) => response, + Err(err) => { + error!("Error handling request: {:?}", err); + continue; + } + }; + drop(backend); socket .send(Message::Binary( diff --git a/src/lib.rs b/src/lib.rs index 8b29ebe..f1b7287 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod backend; pub mod command; pub mod connection; pub mod handshake; diff --git a/src/main.rs b/src/main.rs index 68e9be0..29eeeb5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,10 @@ use std::{error::Error, net::SocketAddr, sync::Arc}; use connection::{connection_worker, Connections, RawConnection, UnestablishedConnection}; -use giterated_daemon::{connection, listener}; +use giterated_daemon::{ + backend::{git::GitBackend, RepositoryBackend}, + connection, listener, +}; use listener::Listeners; use tokio::{ io::{AsyncRead, AsyncWrite}, @@ -19,6 +22,8 @@ async fn main() -> Result<(), Box> { let mut listener = TcpListener::bind("127.0.0.1:8080").await?; let mut connections: Arc> = Arc::default(); let mut listeners: Arc> = Arc::default(); + let mut backend: Arc> = + Arc::new(Mutex::new(GitBackend::new())); loop { let stream = accept_stream(&mut listener).await; @@ -53,6 +58,7 @@ async fn main() -> Result<(), Box> { connection, listeners.clone(), connections.clone(), + backend.clone(), address, )), };