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

ambee/giterated

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

Add users table

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨9f36e3f

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