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

ambee/giterated

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

Add docs

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨51aad53

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