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: ⁨b91129f

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