From 8f910d666a3728f89cb9f05e5de294f807330066 Mon Sep 17 00:00:00 2001 From: Neshura Date: Mon, 8 Apr 2024 17:52:45 +0200 Subject: [PATCH] Add logging to crate + Formatting changes --- src/main.rs | 62 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index c187a47..395bf51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,48 @@ -use std::fmt::{Display, format, Formatter}; +use std::fmt::{Display, Formatter}; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use actix_web::{web, App, HttpResponse, HttpServer, get, Responder, HttpRequest}; -use actix_web::web::Redirect; +use log::{LevelFilter}; +use systemd_journal_logger::{connected_to_journal, JournalLog}; + + +macro_rules! info { + ($msg:tt) => { + match connected_to_journal() { + true => log::info!("[INFO] {}", $msg), + false => println!("[INFO] {}", $msg), + } + }; +} + +macro_rules! warn { + ($msg:tt) => { + match connected_to_journal() { + true => log::warn!("[WARN] {}", $msg), + false => println!("[WARN] {}", $msg), + } + }; +} + +macro_rules! error { + ($msg:tt) => { + match connected_to_journal() { + true => log::error!("[ERROR] {}", $msg), + false => eprintln!("[ERROR] {}", $msg), + } + }; +} #[derive(Clone)] enum Protocol { - HTTP, - HTTPS + Http, + Https } impl Display for Protocol { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { - Protocol::HTTP => write!(f, "http://"), - Protocol::HTTPS => write!(f, "https://") + Protocol::Http => write!(f, "http://"), + Protocol::Https => write!(f, "https://") } } } @@ -34,21 +63,24 @@ struct Config { #[actix_web::main] async fn main() -> std::io::Result<()> { + JournalLog::new().expect("Systemd-Logger crate error").install().expect("Systemd-Logger crate error"); + log::set_max_level(LevelFilter::Info); + let config = Config { redirects: vec![ DomainLinkConfig { domain: "neshura.me".to_owned(), - protocol: Protocol::HTTPS, + protocol: Protocol::Https, target: "neshweb.net".to_owned(), }, DomainLinkConfig { domain: "lemmy.neshura.me".to_owned(), - protocol: Protocol::HTTPS, + protocol: Protocol::Https, target: "bookwormstory.social/u/neshura".to_owned() }, DomainLinkConfig { domain: "test2.neshura.me".to_owned(), - protocol: Protocol::HTTPS, + protocol: Protocol::Https, target: "neshweb.net".to_owned() } ], @@ -58,6 +90,12 @@ async fn main() -> std::io::Result<()> { IpAddr::V4(Ipv4Addr::new(192, 168, 178, 11)) ], }; + + let msg = "This Build is not intended for production use!"; + info!("Test Info"); + warn!(msg); + error!(msg); + let mut server = HttpServer::new(move || { App::new() .app_data(web::Data::new(config.redirects.clone())) @@ -65,9 +103,9 @@ async fn main() -> std::io::Result<()> { .service(dry_handle) }); - for address in &config.addresses { - for port in &config.ports { - server = server.bind((address.clone(), port.clone()))? + for address in config.addresses.iter() { + for port in config.ports.iter() { + server = server.bind((*address, *port))? } } server.run().await