Functionaltiy
parent: tbd commit: 5646846
Showing 6 changed files with 152 insertions and 17 deletions
Cargo.lock
@@ -211,6 +211,7 @@ dependencies = [ | ||
211 | 211 | "tokio", |
212 | 212 | "tokio-tungstenite", |
213 | 213 | "tracing", |
214 | "tracing-subscriber", | |
214 | 215 | ] |
215 | 216 | |
216 | 217 | [[package]] |
@@ -253,6 +254,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | ||
253 | 254 | checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" |
254 | 255 | |
255 | 256 | [[package]] |
257 | name = "lazy_static" | |
258 | version = "1.4.0" | |
259 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
260 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" | |
261 | ||
262 | [[package]] | |
256 | 263 | name = "libc" |
257 | 264 | version = "0.2.147" |
258 | 265 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -301,6 +308,16 @@ dependencies = [ | ||
301 | 308 | ] |
302 | 309 | |
303 | 310 | [[package]] |
311 | name = "nu-ansi-term" | |
312 | version = "0.46.0" | |
313 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
314 | checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" | |
315 | dependencies = [ | |
316 | "overload", | |
317 | "winapi", | |
318 | ] | |
319 | ||
320 | [[package]] | |
304 | 321 | name = "num_cpus" |
305 | 322 | version = "1.16.0" |
306 | 323 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -326,6 +343,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | ||
326 | 343 | checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" |
327 | 344 | |
328 | 345 | [[package]] |
346 | name = "overload" | |
347 | version = "0.1.1" | |
348 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
349 | checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" | |
350 | ||
351 | [[package]] | |
329 | 352 | name = "parking_lot" |
330 | 353 | version = "0.12.1" |
331 | 354 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -490,6 +513,15 @@ dependencies = [ | ||
490 | 513 | ] |
491 | 514 | |
492 | 515 | [[package]] |
516 | name = "sharded-slab" | |
517 | version = "0.1.4" | |
518 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
519 | checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31" | |
520 | dependencies = [ | |
521 | "lazy_static", | |
522 | ] | |
523 | ||
524 | [[package]] | |
493 | 525 | name = "signal-hook-registry" |
494 | 526 | version = "1.4.1" |
495 | 527 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -555,6 +587,16 @@ dependencies = [ | ||
555 | 587 | ] |
556 | 588 | |
557 | 589 | [[package]] |
590 | name = "thread_local" | |
591 | version = "1.1.7" | |
592 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
593 | checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" | |
594 | dependencies = [ | |
595 | "cfg-if", | |
596 | "once_cell", | |
597 | ] | |
598 | ||
599 | [[package]] | |
558 | 600 | name = "tinyvec" |
559 | 601 | version = "1.6.0" |
560 | 602 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -641,6 +683,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | ||
641 | 683 | checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a" |
642 | 684 | dependencies = [ |
643 | 685 | "once_cell", |
686 | "valuable", | |
687 | ] | |
688 | ||
689 | [[package]] | |
690 | name = "tracing-log" | |
691 | version = "0.1.3" | |
692 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
693 | checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" | |
694 | dependencies = [ | |
695 | "lazy_static", | |
696 | "log", | |
697 | "tracing-core", | |
698 | ] | |
699 | ||
700 | [[package]] | |
701 | name = "tracing-subscriber" | |
702 | version = "0.3.17" | |
703 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
704 | checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77" | |
705 | dependencies = [ | |
706 | "nu-ansi-term", | |
707 | "sharded-slab", | |
708 | "smallvec", | |
709 | "thread_local", | |
710 | "tracing-core", | |
711 | "tracing-log", | |
644 | 712 | ] |
645 | 713 | |
646 | 714 | [[package]] |
@@ -707,6 +775,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | ||
707 | 775 | checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" |
708 | 776 | |
709 | 777 | [[package]] |
778 | name = "valuable" | |
779 | version = "0.1.0" | |
780 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
781 | checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" | |
782 | ||
783 | [[package]] | |
710 | 784 | name = "version_check" |
711 | 785 | version = "0.9.4" |
712 | 786 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -719,6 +793,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | ||
719 | 793 | checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" |
720 | 794 | |
721 | 795 | [[package]] |
796 | name = "winapi" | |
797 | version = "0.3.9" | |
798 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
799 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" | |
800 | dependencies = [ | |
801 | "winapi-i686-pc-windows-gnu", | |
802 | "winapi-x86_64-pc-windows-gnu", | |
803 | ] | |
804 | ||
805 | [[package]] | |
806 | name = "winapi-i686-pc-windows-gnu" | |
807 | version = "0.4.0" | |
808 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
809 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" | |
810 | ||
811 | [[package]] | |
812 | name = "winapi-x86_64-pc-windows-gnu" | |
813 | version = "0.4.0" | |
814 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
815 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" | |
816 | ||
817 | [[package]] | |
722 | 818 | name = "windows-sys" |
723 | 819 | version = "0.48.0" |
724 | 820 | source = "registry+https://github.com/rust-lang/crates.io-index" |
Cargo.toml
@@ -11,4 +11,5 @@ tokio = { version = "1.32.0", features = ["full"] } | ||
11 | 11 | tracing = "*" |
12 | 12 | futures-util = "*" |
13 | 13 | serde = { version = "1", features = ["derive"]} |
14 | serde_json = "1.0" | |
14 | \ No newline at end of file | |
14 | serde_json = "1.0" | |
15 | tracing-subscriber = "0.3" | |
15 | \ No newline at end of file |
src/connection.rs
@@ -26,7 +26,8 @@ use crate::{ | ||
26 | 26 | listener::Listeners, |
27 | 27 | model::{ |
28 | 28 | instance::{Instance, InstanceMeta}, |
29 | repository::{CommitMetadata, RepositoryView}, | |
29 | repository::{CommitMetadata, Repository, RepositoryView}, | |
30 | user::User, | |
30 | 31 | }, |
31 | 32 | }; |
32 | 33 | |
@@ -59,7 +60,7 @@ pub async fn connection_worker( | ||
59 | 60 | ) { |
60 | 61 | let mut handshaked = false; |
61 | 62 | let this_instance = Instance { |
62 | url: String::from("FOO"), | |
63 | url: String::from("127.0.0.1:8080"), | |
63 | 64 | }; |
64 | 65 | |
65 | 66 | while let Some(message) = socket.next().await { |
@@ -92,6 +93,8 @@ pub async fn connection_worker( | ||
92 | 93 | } |
93 | 94 | }; |
94 | 95 | |
96 | info!("Read payload: {}", std::str::from_utf8(&payload).unwrap()); | |
97 | ||
95 | 98 | if let MessageKind::Handshake(handshake) = message { |
96 | 99 | match handshake { |
97 | 100 | HandshakeMessage::Initiate(_) => { |
@@ -105,7 +108,10 @@ pub async fn connection_worker( | ||
105 | 108 | |
106 | 109 | socket |
107 | 110 | .send(Message::Binary( |
108 | serde_json::to_vec(&HandshakeMessage::Response(message)).unwrap(), | |
111 | serde_json::to_vec(&MessageKind::Handshake( | |
112 | HandshakeMessage::Response(message), | |
113 | )) | |
114 | .unwrap(), | |
109 | 115 | )) |
110 | 116 | .await |
111 | 117 | .unwrap(); |
@@ -118,7 +124,10 @@ pub async fn connection_worker( | ||
118 | 124 | |
119 | 125 | socket |
120 | 126 | .send(Message::Binary( |
121 | serde_json::to_vec(&HandshakeMessage::Finalize(message)).unwrap(), | |
127 | serde_json::to_vec(&MessageKind::Handshake( | |
128 | HandshakeMessage::Finalize(message), | |
129 | )) | |
130 | .unwrap(), | |
122 | 131 | )) |
123 | 132 | .await |
124 | 133 | .unwrap(); |
@@ -128,6 +137,19 @@ pub async fn connection_worker( | ||
128 | 137 | HandshakeMessage::Finalize(_) => { |
129 | 138 | handshaked = true; |
130 | 139 | |
140 | // Send HandshakeMessage::Finalize | |
141 | let message = HandshakeFinalize { success: true }; | |
142 | ||
143 | socket | |
144 | .send(Message::Binary( | |
145 | serde_json::to_vec(&MessageKind::Handshake( | |
146 | HandshakeMessage::Finalize(message), | |
147 | )) | |
148 | .unwrap(), | |
149 | )) | |
150 | .await | |
151 | .unwrap(); | |
152 | ||
131 | 153 | continue; |
132 | 154 | } |
133 | 155 | } |
@@ -139,6 +161,7 @@ pub async fn connection_worker( | ||
139 | 161 | |
140 | 162 | if let MessageKind::Repository(repository) = &message { |
141 | 163 | if repository.target.instance != this_instance { |
164 | info!("Forwarding command to {}", repository.target.instance.url); | |
142 | 165 | // We need to send this command to a different instance |
143 | 166 | |
144 | 167 | let mut listener = send_and_get_listener(message, &listeners, &connections).await; |
@@ -253,6 +276,8 @@ pub async fn connection_worker( | ||
253 | 276 | } |
254 | 277 | } |
255 | 278 | } |
279 | ||
280 | info!("Connection closed"); | |
256 | 281 | } |
257 | 282 | |
258 | 283 | async fn send_and_get_listener( |
@@ -260,14 +285,20 @@ async fn send_and_get_listener( | ||
260 | 285 | listeners: &Arc<Mutex<Listeners>>, |
261 | 286 | mut connections: &Arc<Mutex<Connections>>, |
262 | 287 | ) -> Receiver<MessageKind> { |
263 | let (instance, user, repository) = match &message { | |
264 | MessageKind::Handshake(_) => { | |
265 | todo!() | |
266 | } | |
267 | MessageKind::Repository(repository) => (None, None, Some(repository.target.clone())), | |
268 | }; | |
288 | let (instance, user, repository): (Option<Instance>, Option<User>, Option<Repository>) = | |
289 | match &message { | |
290 | MessageKind::Handshake(_) => { | |
291 | todo!() | |
292 | } | |
293 | MessageKind::Repository(repository) => (None, None, Some(repository.target.clone())), | |
294 | }; | |
269 | 295 | |
270 | let target = todo!(); | |
296 | let target = match (&instance, &user, &repository) { | |
297 | (Some(instance), _, _) => instance.clone(), | |
298 | (_, Some(user), _) => user.instance.clone(), | |
299 | (_, _, Some(repository)) => repository.instance.clone(), | |
300 | _ => unreachable!(), | |
301 | }; | |
271 | 302 | |
272 | 303 | let mut listeners = listeners.lock().await; |
273 | 304 | let mut listener = listeners.add(instance, user, repository); |
src/main.rs
@@ -15,6 +15,7 @@ extern crate tracing; | ||
15 | 15 | |
16 | 16 | #[tokio::main] |
17 | 17 | async fn main() -> Result<(), Box<dyn Error>> { |
18 | tracing_subscriber::fmt::init(); | |
18 | 19 | let mut listener = TcpListener::bind("127.0.0.1:8080").await?; |
19 | 20 | let mut connections: Arc<Mutex<Connections>> = Arc::default(); |
20 | 21 | let mut listeners: Arc<Mutex<Listeners>> = Arc::default(); |
@@ -30,6 +31,8 @@ async fn main() -> Result<(), Box<dyn Error>> { | ||
30 | 31 | } |
31 | 32 | }; |
32 | 33 | |
34 | info!("Accepted connection from {}", address); | |
35 | ||
33 | 36 | let connection = accept_websocket_connection(stream).await; |
34 | 37 | |
35 | 38 | let connection = match connection { |
@@ -43,6 +46,8 @@ async fn main() -> Result<(), Box<dyn Error>> { | ||
43 | 46 | } |
44 | 47 | }; |
45 | 48 | |
49 | info!("Websocket connection established with {}", address); | |
50 | ||
46 | 51 | let connection = RawConnection { |
47 | 52 | task: tokio::spawn(connection_worker( |
48 | 53 | connection, |
src/model/repository.rs
@@ -10,7 +10,7 @@ pub struct Repository { | ||
10 | 10 | pub instance: Instance, |
11 | 11 | } |
12 | 12 | |
13 | #[derive(Clone, Serialize, Deserialize)] | |
13 | #[derive(Clone, Debug, Serialize, Deserialize)] | |
14 | 14 | pub struct RepositoryView { |
15 | 15 | pub name: String, |
16 | 16 | pub description: String, |
@@ -19,19 +19,19 @@ pub struct RepositoryView { | ||
19 | 19 | pub files: Vec<RepositoryFile>, |
20 | 20 | } |
21 | 21 | |
22 | #[derive(Clone, Serialize, Deserialize)] | |
22 | #[derive(Debug, Clone, Serialize, Deserialize)] | |
23 | 23 | pub enum RepositoryFile { |
24 | 24 | Directory(String), |
25 | 25 | File(String), |
26 | 26 | } |
27 | 27 | |
28 | #[derive(Clone, Serialize, Deserialize)] | |
28 | #[derive(Debug, Clone, Serialize, Deserialize)] | |
29 | 29 | pub struct RepositoryFileWithCommitMetadata { |
30 | 30 | pub file: RepositoryFile, |
31 | 31 | pub commit_metadata: CommitMetadata, |
32 | 32 | } |
33 | 33 | |
34 | #[derive(Clone, Serialize, Deserialize, Default)] | |
34 | #[derive(Debug, Clone, Serialize, Deserialize, Default)] | |
35 | 35 | pub struct CommitMetadata { |
36 | 36 | pub author: String, |
37 | 37 | pub message: String, |