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: ⁨74f247e

⁨src/main.rs⁩ - ⁨3615⁩ 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 info!("Connected");
44
45 let repository_backend: Arc<Mutex<dyn RepositoryBackend + Send>> = Arc::new(Mutex::new({
46 let foo: GitBackend = GitBackend {
47 pg_pool: db_pool,
48 repository_folder: String::from("/tmp/foo"),
49 };
50 foo
51 }));
52
53 let token_granter = Arc::new(Mutex::new(AuthenticationTokenGranter {
54 config: config.clone(),
55 }));
56
57 info!("Connected");
58
59 loop {
60 let stream = accept_stream(&mut listener).await;
61 info!("Connected");
62
63 let (stream, address) = match stream {
64 Ok(stream) => stream,
65 Err(err) => {
66 error!("Failed to accept connection. {:?}", err);
67 continue;
68 }
69 };
70
71 info!("Accepted connection from {}", address);
72
73 let connection = accept_websocket_connection(stream).await;
74
75 let connection = match connection {
76 Ok(connection) => connection,
77 Err(err) => {
78 error!(
79 "Failed to initiate Websocket connection from {}. {:?}",
80 address, err
81 );
82 continue;
83 }
84 };
85
86 info!("Websocket connection established with {}", address);
87
88 let connection = RawConnection {
89 task: tokio::spawn(connection_worker(
90 connection,
91 listeners.clone(),
92 connections.clone(),
93 repository_backend.clone(),
94 token_granter.clone(),
95 address,
96 )),
97 };
98
99 connections.lock().await.connections.push(connection);
100 }
101 }
102
103 async fn accept_stream(
104 listener: &mut TcpListener,
105 ) -> Result<(TcpStream, SocketAddr), Box<dyn Error>> {
106 let stream = listener.accept().await?;
107
108 Ok(stream)
109 }
110
111 async fn accept_websocket_connection<S: AsyncRead + AsyncWrite + Unpin>(
112 stream: S,
113 ) -> Result<WebSocketStream<S>, Box<dyn Error>> {
114 let connection = accept_async(stream).await?;
115
116 Ok(connection)
117 }
118