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

ambee/giterated

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

More progress :)

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨92c3f32

⁨giterated-daemon/src/main.rs⁩ - ⁨3385⁩ 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_plugin::new_stack::Runtime;
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 runtime = Arc::new(runtime);
55
56 let pool = LocalPoolHandle::new(5);
57
58 loop {
59 let stream = accept_stream(&mut listener).await;
60 info!("Connected");
61
62 let (stream, address) = match stream {
63 Ok(stream) => stream,
64 Err(err) => {
65 error!("Failed to accept connection. {:?}", err);
66 continue;
67 }
68 };
69
70 info!("Accepted connection from {}", address);
71
72 let connection = accept_websocket_connection(stream).await;
73
74 let connection = match connection {
75 Ok(connection) => connection,
76 Err(err) => {
77 error!(
78 "Failed to initiate Websocket connection from {}. {:?}",
79 address, err
80 );
81 continue;
82 }
83 };
84
85 info!("Websocket connection established with {}", address);
86
87 let our_instance =
88 Instance::from_str(config["giterated"]["instance"].as_str().unwrap()).unwrap();
89 let runtime = runtime.clone();
90
91 pool.spawn_pinned(move || client_wrapper(our_instance, connection, runtime));
92 }
93 }
94
95 async fn accept_stream(listener: &mut TcpListener) -> Result<(TcpStream, SocketAddr), Error> {
96 let stream = listener.accept().await?;
97
98 Ok(stream)
99 }
100
101 async fn accept_websocket_connection<S: AsyncRead + AsyncWrite + Unpin>(
102 stream: S,
103 ) -> Result<WebSocketStream<S>, Error> {
104 let connection = accept_async(stream).await?;
105
106 Ok(connection)
107 }
108