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

ambee/giterated

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

Add message forwarding

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨e4fa992

⁨giterated-daemon/src/main.rs⁩ - ⁨4371⁩ bytes
Raw
1 use anyhow::Error;
2 use connection::{Connections, RawConnection};
3 use giterated_daemon::{
4 authentication::AuthenticationTokenGranter,
5 backend::{git::GitBackend, user::UserAuth, RepositoryBackend, UserBackend},
6 connection::{self, wrapper::connection_wrapper},
7 federation::connections::InstanceConnections,
8 };
9 use giterated_models::model::instance::Instance;
10 use sqlx::{postgres::PgConnectOptions, ConnectOptions, PgPool};
11 use std::{net::SocketAddr, str::FromStr, sync::Arc};
12 use tokio::{
13 fs::File,
14 io::{AsyncRead, AsyncReadExt, AsyncWrite},
15 net::{TcpListener, TcpStream},
16 sync::Mutex,
17 };
18 use tokio_tungstenite::{accept_async, WebSocketStream};
19 use toml::Table;
20
21 #[macro_use]
22 extern crate tracing;
23
24 #[tokio::main]
25 async fn main() -> Result<(), Error> {
26 tracing_subscriber::fmt::init();
27 let mut listener = TcpListener::bind("0.0.0.0:7270").await?;
28 let connections: Arc<Mutex<Connections>> = Arc::default();
29 let instance_connections: Arc<Mutex<InstanceConnections>> = Arc::default();
30 let config: Table = {
31 let mut file = File::open("Giterated.toml").await?;
32 let mut text = String::new();
33 file.read_to_string(&mut text).await?;
34 text.parse()?
35 };
36 let db_conn_options = PgConnectOptions::new()
37 .host(config["postgres"]["host"].as_str().unwrap())
38 .port(config["postgres"]["port"].as_integer().unwrap() as u16)
39 .database(config["postgres"]["database"].as_str().unwrap())
40 .username(config["postgres"]["user"].as_str().unwrap())
41 .password(config["postgres"]["password"].as_str().unwrap())
42 .log_statements(log::LevelFilter::Off);
43 let db_pool = PgPool::connect_with(db_conn_options).await?;
44
45 debug!("Running database migrations...");
46 sqlx::migrate!().run(&db_pool).await?;
47 info!("Connected");
48
49 let repository_backend: Arc<Mutex<dyn RepositoryBackend + Send>> =
50 Arc::new(Mutex::new(GitBackend {
51 pg_pool: db_pool.clone(),
52 repository_folder: String::from(
53 config["giterated"]["backend"]["git"]["root"]
54 .as_str()
55 .unwrap(),
56 ),
57 instance: Instance::from_str("giterated.dev").unwrap(),
58 }));
59
60 let token_granter = Arc::new(Mutex::new(AuthenticationTokenGranter {
61 config: config.clone(),
62 instance: Instance::from_str("giterated.dev").unwrap(),
63 }));
64
65 let user_backend: Arc<Mutex<dyn UserBackend + Send>> = Arc::new(Mutex::new(UserAuth::new(
66 db_pool.clone(),
67 &Instance::from_str("giterated.dev").unwrap(),
68 token_granter.clone(),
69 )));
70
71 info!("Connected");
72
73 loop {
74 let stream = accept_stream(&mut listener).await;
75 info!("Connected");
76
77 let (stream, address) = match stream {
78 Ok(stream) => stream,
79 Err(err) => {
80 error!("Failed to accept connection. {:?}", err);
81 continue;
82 }
83 };
84
85 info!("Accepted connection from {}", address);
86
87 let connection = accept_websocket_connection(stream).await;
88
89 let connection = match connection {
90 Ok(connection) => connection,
91 Err(err) => {
92 error!(
93 "Failed to initiate Websocket connection from {}. {:?}",
94 address, err
95 );
96 continue;
97 }
98 };
99
100 info!("Websocket connection established with {}", address);
101
102 let connection = RawConnection {
103 task: tokio::spawn(connection_wrapper(
104 connection,
105 connections.clone(),
106 repository_backend.clone(),
107 user_backend.clone(),
108 token_granter.clone(),
109 address,
110 Instance::from_str("giterated.dev").unwrap(),
111 instance_connections.clone(),
112 )),
113 };
114
115 connections.lock().await.connections.push(connection);
116 }
117 }
118
119 async fn accept_stream(listener: &mut TcpListener) -> Result<(TcpStream, SocketAddr), Error> {
120 let stream = listener.accept().await?;
121
122 Ok(stream)
123 }
124
125 async fn accept_websocket_connection<S: AsyncRead + AsyncWrite + Unpin>(
126 stream: S,
127 ) -> Result<WebSocketStream<S>, Error> {
128 let connection = accept_async(stream).await?;
129
130 Ok(connection)
131 }
132