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

ambee/giterated

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

Add backend trait and git backend stub

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨b9203a9

⁨src/main.rs⁩ - ⁨2415⁩ bytes
Raw
1 use std::{error::Error, net::SocketAddr, sync::Arc};
2
3 use connection::{connection_worker, Connections, RawConnection, UnestablishedConnection};
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 mut connections: Arc<Mutex<Connections>> = Arc::default();
24 let mut listeners: Arc<Mutex<Listeners>> = Arc::default();
25 let mut backend: Arc<Mutex<dyn RepositoryBackend + Send>> =
26 Arc::new(Mutex::new(GitBackend::new()));
27
28 loop {
29 let stream = accept_stream(&mut listener).await;
30
31 let (stream, address) = match stream {
32 Ok(stream) => stream,
33 Err(err) => {
34 error!("Failed to accept connection. {:?}", err);
35 continue;
36 }
37 };
38
39 info!("Accepted connection from {}", address);
40
41 let connection = accept_websocket_connection(stream).await;
42
43 let connection = match connection {
44 Ok(connection) => connection,
45 Err(err) => {
46 error!(
47 "Failed to initiate Websocket connection from {}. {:?}",
48 address, err
49 );
50 continue;
51 }
52 };
53
54 info!("Websocket connection established with {}", address);
55
56 let connection = RawConnection {
57 task: tokio::spawn(connection_worker(
58 connection,
59 listeners.clone(),
60 connections.clone(),
61 backend.clone(),
62 address,
63 )),
64 };
65
66 connections.lock().await.connections.push(connection);
67 }
68 }
69
70 async fn accept_stream(
71 listener: &mut TcpListener,
72 ) -> Result<(TcpStream, SocketAddr), Box<dyn Error>> {
73 let stream = listener.accept().await?;
74
75 Ok(stream)
76 }
77
78 async fn accept_websocket_connection<S: AsyncRead + AsyncWrite + Unpin>(
79 stream: S,
80 ) -> Result<WebSocketStream<S>, Box<dyn Error>> {
81 let connection = accept_async(stream).await?;
82
83 Ok(connection)
84 }
85