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

ambee/giterated

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

no clue what this is

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨7889bf6

⁨giterated-daemon/src/main.rs⁩ - ⁨3378⁩ bytes
Raw
1 use anyhow::Error;
2 use giterated_daemon::{authentication::AuthenticationTokenGranter, client::client_wrapper};
3
4 use giterated_models::instance::Instance;
5
6 use giterated_runtime::{Runtime, RuntimeHandle, StaticRuntimeExt};
7 use sqlx::{postgres::PgConnectOptions, ConnectOptions, PgPool};
8 use std::{net::SocketAddr, str::FromStr, sync::Arc};
9 use tokio::{
10 fs::File,
11 io::{AsyncRead, AsyncReadExt, AsyncWrite},
12 net::{TcpListener, TcpStream},
13 sync::Mutex,
14 };
15 use tokio_tungstenite::{accept_async, WebSocketStream};
16 use tokio_util::task::LocalPoolHandle;
17 use toml::Table;
18
19 #[macro_use]
20 extern crate tracing;
21
22 #[tokio::main]
23 async fn main() -> Result<(), Error> {
24 tracing_subscriber::fmt::init();
25 let config: Table = {
26 let mut file = File::open("Giterated.toml").await?;
27 let mut text = String::new();
28 file.read_to_string(&mut text).await?;
29 text.parse()?
30 };
31 let mut listener = TcpListener::bind(config["giterated"]["bind"].as_str().unwrap()).await?;
32 let db_conn_options = PgConnectOptions::new()
33 .host(config["postgres"]["host"].as_str().unwrap())
34 .port(config["postgres"]["port"].as_integer().unwrap() as u16)
35 .database(config["postgres"]["database"].as_str().unwrap())
36 .username(config["postgres"]["user"].as_str().unwrap())
37 .password(config["postgres"]["password"].as_str().unwrap())
38 .log_statements(log::LevelFilter::Off);
39 let db_pool = PgPool::connect_with(db_conn_options).await?;
40
41 debug!("Running database migrations...");
42 sqlx::migrate!().run(&db_pool).await?;
43 info!("Connected");
44
45 let _token_granter = Arc::new(Mutex::new(AuthenticationTokenGranter {
46 config: config.clone(),
47 instance: Instance::from_str(config["giterated"]["instance"].as_str().unwrap()).unwrap(),
48 }));
49
50 info!("Connected");
51
52 let runtime = Runtime::new();
53
54 let pool = LocalPoolHandle::new(5);
55
56 loop {
57 let stream = accept_stream(&mut listener).await;
58 info!("Connected");
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 our_instance =
86 Instance::from_str(config["giterated"]["instance"].as_str().unwrap()).unwrap();
87
88 pool.spawn_pinned(move || {
89 client_wrapper(our_instance, connection, RuntimeHandle::from_static())
90 });
91 }
92 }
93
94 async fn accept_stream(listener: &mut TcpListener) -> Result<(TcpStream, SocketAddr), Error> {
95 let stream = listener.accept().await?;
96
97 Ok(stream)
98 }
99
100 async fn accept_websocket_connection<S: AsyncRead + AsyncWrite + Unpin>(
101 stream: S,
102 ) -> Result<WebSocketStream<S>, Error> {
103 let connection = accept_async(stream).await?;
104
105 Ok(connection)
106 }
107