JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Fixes

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨8a111d7

⁨src/main.rs⁩ - ⁨2370⁩ bytes
Raw
1 use std::{error::Error, net::SocketAddr, sync::Arc};
2
3 use connection::{connection_worker, Connections, RawConnection};
4 use giterated_daemon::{
5 backend::{git::GitBackend, RepositoryBackend},
6 connection, listener,
7 };
8 use listener::Listeners;
9 use tokio::{
10 io::{AsyncRead, AsyncWrite},
11 net::{TcpListener, TcpStream},
12 sync::Mutex,
13 };
14 use tokio_tungstenite::{accept_async, WebSocketStream};
15
16 #[macro_use]
17 extern crate tracing;
18
19 #[tokio::main]
20 async fn main() -> Result<(), Box<dyn Error>> {
21 tracing_subscriber::fmt::init();
22 let mut listener = TcpListener::bind("127.0.0.1:8080").await?;
23 let connections: Arc<Mutex<Connections>> = Arc::default();
24 let listeners: Arc<Mutex<Listeners>> = Arc::default();
25 let backend: Arc<Mutex<dyn RepositoryBackend + Send>> = Arc::new(Mutex::new(GitBackend::new()));
26
27 loop {
28 let stream = accept_stream(&mut listener).await;
29
30 let (stream, address) = match stream {
31 Ok(stream) => stream,
32 Err(err) => {
33 error!("Failed to accept connection. {:?}", err);
34 continue;
35 }
36 };
37
38 info!("Accepted connection from {}", address);
39
40 let connection = accept_websocket_connection(stream).await;
41
42 let connection = match connection {
43 Ok(connection) => connection,
44 Err(err) => {
45 error!(
46 "Failed to initiate Websocket connection from {}. {:?}",
47 address, err
48 );
49 continue;
50 }
51 };
52
53 info!("Websocket connection established with {}", address);
54
55 let connection = RawConnection {
56 task: tokio::spawn(connection_worker(
57 connection,
58 listeners.clone(),
59 connections.clone(),
60 backend.clone(),
61 address,
62 )),
63 };
64
65 connections.lock().await.connections.push(connection);
66 }
67 }
68
69 async fn accept_stream(
70 listener: &mut TcpListener,
71 ) -> Result<(TcpStream, SocketAddr), Box<dyn Error>> {
72 let stream = listener.accept().await?;
73
74 Ok(stream)
75 }
76
77 async fn accept_websocket_connection<S: AsyncRead + AsyncWrite + Unpin>(
78 stream: S,
79 ) -> Result<WebSocketStream<S>, Box<dyn Error>> {
80 let connection = accept_async(stream).await?;
81
82 Ok(connection)
83 }
84