better logging & common crate

This commit is contained in:
OMGeeky
2024-05-06 18:28:46 +02:00
parent e979cc13f2
commit ce2b75e1b4
3 changed files with 1955 additions and 54 deletions

1988
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -6,10 +6,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
twba-backup-config = { version = "0.1.3", git = "https://github.com/OMGeeky/backup_config.git" }
twba-common = { version = "0.2", git = "https://github.com/OMGeeky/twba.common.git" }
hyper = { version = "0.14", features = ["full", "server"] }
tokio = { version = "1", features = ["full"] }
url = "2.2.2"
lazy_static = "1.4"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing = "0.1.40"

View File

@@ -5,16 +5,15 @@ use lazy_static::lazy_static;
use std::collections::HashMap;
use std::convert::Infallible;
use tokio::fs::write;
use tracing::{error, info, trace};
use tracing::instrument;
use twba_backup_config::Conf;
use twba_common::prelude::*;
use url::Url;
#[tokio::main]
#[instrument]
async fn main() {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.with_env_filter("warn,twba_code_receiver=trace,twba_backup_config=info")
.init();
let _guard = init_tracing("twba_code_receiver");
let make_svc =
make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(handle_request)) });
@@ -22,14 +21,17 @@ async fn main() {
let server = Server::bind(&addr).serve(make_svc);
info!("Starting code receiver");
if let Err(e) = server.await {
error!("server error: {}", e);
}
}
#[instrument]
async fn handle_request(req: Request<Body>) -> Result<Response<Body>, Infallible> {
match (req.method(), req.uri().path()) {
(&Method::GET, "/googleapi/auth") => auth_get(req).await,
(&Method::GET, "/") => Ok(Response::new(Body::from("Hello, World!"))),
(&Method::GET, "/favicon.ico") => Ok(Response::default()),
other => {
error!("404: {:?} {:?}", other.0, other.1);
let mut not_found = Response::default();
@@ -43,7 +45,7 @@ async fn auth_get(req: Request<Body>) -> Result<Response<Body>, Infallible> {
let url = format!("http://localhost{}", req.uri());
trace!("auth get request with url: '{}'", url);
let url = Url::parse(&url).unwrap();
let params: HashMap<_, _> = url.query_pairs().into_owned().collect();
let params: HashMap<_, _> = url.query_pairs().collect();
if let Some(code) = params.get("code") {
info!("Code received: '{}'", code);
let write_res = write_to_file(code.to_string()).await;