use anyhow::Error; use giterated_daemon::{authentication::AuthenticationTokenGranter, client::client_wrapper}; use giterated_models::instance::Instance; use giterated_plugin::new_stack::Runtime; use giterated_protocol::NetworkedSubstack; use sqlx::{postgres::PgConnectOptions, ConnectOptions, PgPool}; use std::{net::SocketAddr, str::FromStr, sync::Arc}; use tokio::{ fs::File, io::{AsyncRead, AsyncReadExt, AsyncWrite}, net::{TcpListener, TcpStream}, sync::{Mutex, OnceCell}, }; use tokio_tungstenite::{accept_async, WebSocketStream}; use tokio_util::task::LocalPoolHandle; use toml::Table; #[macro_use] extern crate tracing; #[tokio::main] async fn main() -> Result<(), Error> { tracing_subscriber::fmt::init(); let config: Table = { let mut file = File::open("Giterated.toml").await?; let mut text = String::new(); file.read_to_string(&mut text).await?; text.parse()? }; let mut listener = TcpListener::bind(config["giterated"]["bind"].as_str().unwrap()).await?; let db_conn_options = PgConnectOptions::new() .host(config["postgres"]["host"].as_str().unwrap()) .port(config["postgres"]["port"].as_integer().unwrap() as u16) .database(config["postgres"]["database"].as_str().unwrap()) .username(config["postgres"]["user"].as_str().unwrap()) .password(config["postgres"]["password"].as_str().unwrap()) .log_statements(log::LevelFilter::Off); let db_pool = PgPool::connect_with(db_conn_options).await?; debug!("Running database migrations..."); sqlx::migrate!().run(&db_pool).await?; info!("Connected"); let token_granter = Arc::new(Mutex::new(AuthenticationTokenGranter { config: config.clone(), instance: Instance::from_str(config["giterated"]["instance"].as_str().unwrap()).unwrap(), })); info!("Connected"); let mut runtime = Runtime::default(); let networked_stack = NetworkedSubstack { home_uri: Some( Instance::from_str(config["giterated"]["instance"].as_str().unwrap()) .unwrap() .0, ), }; let runtime = Arc::new(runtime); let pool = LocalPoolHandle::new(5); loop { let stream = accept_stream(&mut listener).await; info!("Connected"); let (stream, address) = match stream { Ok(stream) => stream, Err(err) => { error!("Failed to accept connection. {:?}", err); continue; } }; info!("Accepted connection from {}", address); let connection = accept_websocket_connection(stream).await; let connection = match connection { Ok(connection) => connection, Err(err) => { error!( "Failed to initiate Websocket connection from {}. {:?}", address, err ); continue; } }; info!("Websocket connection established with {}", address); let our_instance = Instance::from_str(config["giterated"]["instance"].as_str().unwrap()).unwrap(); let runtime = runtime.clone(); pool.spawn_pinned(move || client_wrapper(our_instance, connection, runtime)); } } async fn accept_stream(listener: &mut TcpListener) -> Result<(TcpStream, SocketAddr), Error> { let stream = listener.accept().await?; Ok(stream) } async fn accept_websocket_connection( stream: S, ) -> Result, Error> { let connection = accept_async(stream).await?; Ok(connection) }