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

ambee/giterated

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

Add token extension

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨86d028f

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