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

ambee/giterated

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

Changes

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨0b2a26d

⁨src/main.rs⁩ - ⁨3558⁩ 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 .to_owned();
43 let db_pool = PgPool::connect_with(db_conn_options).await?;
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 loop {
58 let stream = accept_stream(&mut listener).await;
59
60 let (stream, address) = match stream {
61 Ok(stream) => stream,
62 Err(err) => {
63 error!("Failed to accept connection. {:?}", err);
64 continue;
65 }
66 };
67
68 info!("Accepted connection from {}", address);
69
70 let connection = accept_websocket_connection(stream).await;
71
72 let connection = match connection {
73 Ok(connection) => connection,
74 Err(err) => {
75 error!(
76 "Failed to initiate Websocket connection from {}. {:?}",
77 address, err
78 );
79 continue;
80 }
81 };
82
83 info!("Websocket connection established with {}", address);
84
85 let connection = RawConnection {
86 task: tokio::spawn(connection_worker(
87 connection,
88 listeners.clone(),
89 connections.clone(),
90 repository_backend.clone(),
91 token_granter.clone(),
92 address,
93 )),
94 };
95
96 connections.lock().await.connections.push(connection);
97 }
98 }
99
100 async fn accept_stream(
101 listener: &mut TcpListener,
102 ) -> Result<(TcpStream, SocketAddr), Box<dyn Error>> {
103 let stream = listener.accept().await?;
104
105 Ok(stream)
106 }
107
108 async fn accept_websocket_connection<S: AsyncRead + AsyncWrite + Unpin>(
109 stream: S,
110 ) -> Result<WebSocketStream<S>, Box<dyn Error>> {
111 let connection = accept_async(stream).await?;
112
113 Ok(connection)
114 }
115