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

ambee/giterated

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

Fixed imports!

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨ef0e853

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