Add logging to crate + Formatting changes
All checks were successful
Run Tests on Code / run-tests (push) Successful in 10s

This commit is contained in:
Neshura 2024-04-08 17:52:45 +02:00
parent 273df8a793
commit 8f910d666a
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C

View file

@ -1,19 +1,48 @@
use std::fmt::{Display, format, Formatter}; use std::fmt::{Display, Formatter};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use actix_web::{web, App, HttpResponse, HttpServer, get, Responder, HttpRequest}; 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)] #[derive(Clone)]
enum Protocol { enum Protocol {
HTTP, Http,
HTTPS Https
} }
impl Display for Protocol { impl Display for Protocol {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self { match self {
Protocol::HTTP => write!(f, "http://"), Protocol::Http => write!(f, "http://"),
Protocol::HTTPS => write!(f, "https://") Protocol::Https => write!(f, "https://")
} }
} }
} }
@ -34,21 +63,24 @@ struct Config {
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { 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 { let config = Config {
redirects: vec![ redirects: vec![
DomainLinkConfig { DomainLinkConfig {
domain: "neshura.me".to_owned(), domain: "neshura.me".to_owned(),
protocol: Protocol::HTTPS, protocol: Protocol::Https,
target: "neshweb.net".to_owned(), target: "neshweb.net".to_owned(),
}, },
DomainLinkConfig { DomainLinkConfig {
domain: "lemmy.neshura.me".to_owned(), domain: "lemmy.neshura.me".to_owned(),
protocol: Protocol::HTTPS, protocol: Protocol::Https,
target: "bookwormstory.social/u/neshura".to_owned() target: "bookwormstory.social/u/neshura".to_owned()
}, },
DomainLinkConfig { DomainLinkConfig {
domain: "test2.neshura.me".to_owned(), domain: "test2.neshura.me".to_owned(),
protocol: Protocol::HTTPS, protocol: Protocol::Https,
target: "neshweb.net".to_owned() target: "neshweb.net".to_owned()
} }
], ],
@ -58,6 +90,12 @@ async fn main() -> std::io::Result<()> {
IpAddr::V4(Ipv4Addr::new(192, 168, 178, 11)) 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 || { let mut server = HttpServer::new(move || {
App::new() App::new()
.app_data(web::Data::new(config.redirects.clone())) .app_data(web::Data::new(config.redirects.clone()))
@ -65,9 +103,9 @@ async fn main() -> std::io::Result<()> {
.service(dry_handle) .service(dry_handle)
}); });
for address in &config.addresses { for address in config.addresses.iter() {
for port in &config.ports { for port in config.ports.iter() {
server = server.bind((address.clone(), port.clone()))? server = server.bind((*address, *port))?
} }
} }
server.run().await server.run().await