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

ambee/giterated

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

Change forwarding

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨2556801

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