diff --git a/Cargo.lock b/Cargo.lock index 4e73c9b..cff25dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,6 +26,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "ansi_colours" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14eec43e0298190790f41679fe69ef7a829d2a2ddd78c8c00339e84710e435fe" +dependencies = [ + "rgb", +] + [[package]] name = "anyhow" version = "1.0.98" @@ -118,9 +127,11 @@ dependencies = [ name = "boxy" version = "0.1.0" dependencies = [ + "ansi_colours", "anyhow", "base64", "bcrypt", + "colour", "http-body-util", "hyper", "hyper-util", @@ -143,6 +154,12 @@ version = "3.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" +[[package]] +name = "bytemuck" +version = "1.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c76a5792e44e4abe34d3abf15636779261d45a7450612059293d1d2cfc63422" + [[package]] name = "byteorder" version = "1.5.0" @@ -180,6 +197,15 @@ dependencies = [ "inout", ] +[[package]] +name = "colour" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b536eebcabe54980476d120a182f7da2268fe02d22575cca99cee5fdda178280" +dependencies = [ + "winapi", +] + [[package]] name = "cpufeatures" version = "0.2.17" @@ -830,6 +856,15 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" +[[package]] +name = "rgb" +version = "0.8.52" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c6a884d2998352bb4daf0183589aec883f16a6da1f4dde84d8e2e9a5409a1ce" +dependencies = [ + "bytemuck", +] + [[package]] name = "ring" version = "0.17.14" @@ -1286,6 +1321,22 @@ dependencies = [ "web-sys", ] +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + [[package]] name = "winapi-util" version = "0.1.9" @@ -1295,6 +1346,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-sys" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index 0c511d3..e6bf1d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,6 @@ serde = "1.0.219" base64 = "0.22.1" string-builder = "0.2.0" json = "0.12.4" +ansi_colours = "1.2.3" +colour = "2.1.0" diff --git a/config.yaml b/config.yaml index 602b3e2..fa4c12b 100644 --- a/config.yaml +++ b/config.yaml @@ -1,7 +1,4 @@ -db: - host: '127.0.0.1' - user: 'postgres' - password: 'trust' +database: 'postgresql://postgres:trust@127.0.0.1' proxy: listen: 127.0.0.1 @@ -11,10 +8,6 @@ api: listen: 127.0.0.1 port: 8006 -hosts: # ignore this it doesn't function - - hostname: localhost:8005 - address: localhost:8000 - clients: - name: 'eu-central-1' # Example Client right here (the client in this case would be for example the stereo.cat backend) hashed_secret: '$2b$12$5wH/0p702PPqVp7fCpVS4.1GA2/wAbk89w2nMjwuS8439OhjCUGbK' # password123 diff --git a/src/config.rs b/src/config.rs index 719f3c9..a15575f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -45,45 +45,16 @@ impl Client { .nth(0) .unwrap(); - return bcrypt::verify(secret, client.hashed_secret.as_str()).unwrap(); + bcrypt::verify(secret, client.hashed_secret.as_str()).unwrap() } } -#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)] -pub struct Host { - pub hostname: String, - pub address: String, -} - #[derive(Debug, Serialize, Deserialize, PartialEq, Clone)] pub struct Config { - pub db: Db, + pub database: String, pub proxy: Proxy, pub api: Api, pub clients: Vec, - pub hosts: Vec, -} - -impl Db { - pub async fn to_string(&self) -> String { - let mut builder = String::new(); - - builder += format!( - "host={} port={} user={} dbname={}", - self.host, - self.port.unwrap_or(5432), - self.user, - self.database.clone().unwrap_or(self.user.clone()), - ) - .as_str(); - - match &self.password { - Some(x) => builder += format!(" password={}", x).as_str(), - None => {} - } - - builder - } } impl Config { diff --git a/src/main.rs b/src/main.rs index 8ded716..8fd2d2c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,12 +3,12 @@ mod db; mod server; mod services; -use std::{env, sync::Arc}; +use std::{env, process::exit, sync::Arc}; use bcrypt::DEFAULT_COST; use config::Config; use db::BoxyDatabase; -use log::{error, info}; +use log::{debug, error, info}; use server::Server; use services::{api::ApiService, controller::ControllerService}; use tokio::sync::Mutex; @@ -49,20 +49,21 @@ async fn main() -> Result<(), Box> { let config = Config::get().await.unwrap(); - let db_string = config.db.to_string().await; + debug!("Database URI: {}", config.database); - info!("Database string: {}", db_string); - - let (client, conn) = tokio_postgres::connect(db_string.as_str(), NoTls) + let (client, conn) = tokio_postgres::connect(config.database.as_str(), NoTls) .await .unwrap(); tokio::spawn(async move { if let Err(e) = conn.await { error!("Error while connecting to database: {}", e); + exit(1); } }); + info!("Connected to database."); + let database = Box::new(BoxyDatabase::new(client).await.unwrap()); let database_shared = Arc::new(Mutex::new(Box::leak(database))); @@ -86,10 +87,12 @@ async fn main() -> Result<(), Box> { .unwrap(); tokio::task::spawn(async move { + info!("Starting API server..."); api_server.handle().await; }); // We don't put this on a separate thread because we'd be wasting the main thread. + info!("Starting proxy server..."); proxy_server.handle().await; Ok(()) diff --git a/src/server.rs b/src/server.rs index f35f836..0eb7c63 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,4 +1,4 @@ -use std::error::Error; +use std::{any::type_name_of_val, error::Error}; use http_body_util::{Either, Full}; use hyper::{ @@ -8,7 +8,7 @@ use hyper::{ service::{HttpService, Service}, }; use hyper_util::rt::TokioIo; -use log::error; +use log::{error, info}; use tokio::net::{TcpListener, TcpStream}; pub type GeneralResponse = Response; @@ -39,6 +39,12 @@ where >::Future: Send, { pub async fn handle(&self) { + info!( + "Server started at http://{} for service: {}", + self.listener.local_addr().unwrap(), + type_name_of_val(&self.service) + ); + loop { let (stream, _) = self.listener.accept().await.unwrap(); @@ -46,6 +52,7 @@ where svc_clone.stream(&stream); let io = TokioIo::new(stream); + tokio::task::spawn(async move { if let Err(err) = http1::Builder::new() .writev(false)