mirror of
https://github.com/zaphar/kitchen.git
synced 2025-07-22 19:40:14 -04:00
Improve logging with axum
This commit is contained in:
parent
6f14dceab9
commit
585939b842
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -702,6 +702,7 @@ dependencies = [
|
|||||||
"recipe-store",
|
"recipe-store",
|
||||||
"recipes",
|
"recipes",
|
||||||
"rust-embed",
|
"rust-embed",
|
||||||
|
"tower-http",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
]
|
]
|
||||||
@ -1394,6 +1395,7 @@ dependencies = [
|
|||||||
"tower",
|
"tower",
|
||||||
"tower-layer",
|
"tower-layer",
|
||||||
"tower-service",
|
"tower-service",
|
||||||
|
"tracing",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -16,6 +16,7 @@ axum = "0.5.13"
|
|||||||
rust-embed="6.4.0"
|
rust-embed="6.4.0"
|
||||||
mime_guess = "2.0.4"
|
mime_guess = "2.0.4"
|
||||||
async-trait = "0.1.57"
|
async-trait = "0.1.57"
|
||||||
|
tower-http = { version = "0.3.0", features = ["trace"] }
|
||||||
|
|
||||||
[dependencies.clap]
|
[dependencies.clap]
|
||||||
version = "3.2.16"
|
version = "3.2.16"
|
||||||
|
@ -30,7 +30,7 @@ fn create_app<'a>() -> clap::App<'a> {
|
|||||||
(version: crate_version!())
|
(version: crate_version!())
|
||||||
(author: crate_authors!())
|
(author: crate_authors!())
|
||||||
(about: "Kitchen Management CLI")
|
(about: "Kitchen Management CLI")
|
||||||
(@arg verbose: --verbose -v "Verbosity level for logging (error, warn, info, debug, trace")
|
(@arg verbose: --verbose -v +takes_value "Verbosity level for logging (error, warn, info, debug, trace")
|
||||||
(@subcommand recipe =>
|
(@subcommand recipe =>
|
||||||
(about: "parse a recipe file and output info about it")
|
(about: "parse a recipe file and output info about it")
|
||||||
(@arg ingredients: -i --ingredients "Output the ingredients list.")
|
(@arg ingredients: -i --ingredients "Output the ingredients list.")
|
||||||
|
@ -20,13 +20,14 @@ use async_std::stream::StreamExt;
|
|||||||
use axum::{
|
use axum::{
|
||||||
body::{boxed, Full},
|
body::{boxed, Full},
|
||||||
extract::{Extension, Path},
|
extract::{Extension, Path},
|
||||||
http::{header, StatusCode, Uri},
|
http::{header, StatusCode},
|
||||||
response::{IntoResponse, Redirect, Response},
|
response::{IntoResponse, Redirect, Response},
|
||||||
routing::{get, Router},
|
routing::{get, Router},
|
||||||
};
|
};
|
||||||
use mime_guess;
|
use mime_guess;
|
||||||
use recipe_store::{self, RecipeStore};
|
use recipe_store::{self, RecipeStore};
|
||||||
use rust_embed::RustEmbed;
|
use rust_embed::RustEmbed;
|
||||||
|
use tower_http::trace::TraceLayer;
|
||||||
use tracing::{debug, info, instrument, warn};
|
use tracing::{debug, info, instrument, warn};
|
||||||
|
|
||||||
use crate::api::ParseError;
|
use crate::api::ParseError;
|
||||||
@ -91,15 +92,15 @@ where
|
|||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
async fn ui_static_assets(Path(path): Path<String>) -> impl IntoResponse {
|
async fn ui_static_assets(Path(path): Path<String>) -> impl IntoResponse {
|
||||||
info!(path = path, "Serving ui path");
|
info!("Serving ui path");
|
||||||
|
|
||||||
let mut path = path.trim_start_matches("/");
|
let mut path = path.trim_start_matches("/");
|
||||||
path = if path == "" { "index.html" } else { path };
|
path = if path == "" { "index.html" } else { path };
|
||||||
debug!(path = path, "Serving ui path");
|
debug!(path = path, "Serving transformed path");
|
||||||
StaticFile(path.to_owned())
|
StaticFile(path.to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(skip(store))]
|
#[instrument]
|
||||||
async fn api_recipes(Extension(store): Extension<Arc<recipe_store::AsyncFileStore>>) -> Response {
|
async fn api_recipes(Extension(store): Extension<Arc<recipe_store::AsyncFileStore>>) -> Response {
|
||||||
let result: Result<axum::Json<Vec<String>>, String> = match store
|
let result: Result<axum::Json<Vec<String>>, String> = match store
|
||||||
.get_recipes()
|
.get_recipes()
|
||||||
@ -113,7 +114,7 @@ async fn api_recipes(Extension(store): Extension<Arc<recipe_store::AsyncFileStor
|
|||||||
result.into_response()
|
result.into_response()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(skip(store))]
|
#[instrument]
|
||||||
async fn api_categories(
|
async fn api_categories(
|
||||||
Extension(store): Extension<Arc<recipe_store::AsyncFileStore>>,
|
Extension(store): Extension<Arc<recipe_store::AsyncFileStore>>,
|
||||||
) -> Response {
|
) -> Response {
|
||||||
@ -134,13 +135,16 @@ pub async fn ui_main(recipe_dir_path: PathBuf, listen_socket: SocketAddr) {
|
|||||||
let store = Arc::new(recipe_store::AsyncFileStore::new(recipe_dir_path.clone()));
|
let store = Arc::new(recipe_store::AsyncFileStore::new(recipe_dir_path.clone()));
|
||||||
//let dir_path = (&dir_path).clone();
|
//let dir_path = (&dir_path).clone();
|
||||||
let router = Router::new()
|
let router = Router::new()
|
||||||
.layer(Extension(store))
|
|
||||||
.route("/", get(|| async { Redirect::temporary("/ui/") }))
|
.route("/", get(|| async { Redirect::temporary("/ui/") }))
|
||||||
.route("/ui/*path", get(ui_static_assets))
|
.route("/ui/*path", get(ui_static_assets))
|
||||||
// recipes api path route
|
// recipes api path route
|
||||||
.route("/api/v1/recipes", get(api_recipes))
|
.route("/api/v1/recipes", get(api_recipes))
|
||||||
// categories api path route
|
// categories api path route
|
||||||
.route("/api/v1/categories", get(api_categories));
|
.route("/api/v1/categories", get(api_categories))
|
||||||
|
// NOTE(jwall): Note that the layers are applied to the preceding routes not
|
||||||
|
// the following routes.
|
||||||
|
.layer(TraceLayer::new_for_http())
|
||||||
|
.layer(Extension(store));
|
||||||
info!(
|
info!(
|
||||||
http = format!("http://{}", listen_socket),
|
http = format!("http://{}", listen_socket),
|
||||||
"Starting server"
|
"Starting server"
|
||||||
|
@ -21,11 +21,9 @@ use async_std::{
|
|||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
use reqwasm;
|
use reqwasm;
|
||||||
#[cfg(target_arch = "wasm32")]
|
|
||||||
use tracing::debug;
|
|
||||||
use tracing::instrument;
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
use tracing::{info, warn};
|
use tracing::warn;
|
||||||
|
use tracing::{debug, instrument};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Error(String);
|
pub struct Error(String);
|
||||||
@ -85,7 +83,7 @@ pub trait RecipeStore: Clone + Sized {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct AsyncFileStore {
|
pub struct AsyncFileStore {
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
}
|
}
|
||||||
@ -107,6 +105,7 @@ impl RecipeStore for AsyncFileStore {
|
|||||||
category_path.push(&self.path);
|
category_path.push(&self.path);
|
||||||
category_path.push("categories.txt");
|
category_path.push("categories.txt");
|
||||||
let category_file = File::open(&category_path).await?;
|
let category_file = File::open(&category_path).await?;
|
||||||
|
debug!(category_file = ?category_path, "Opened category file");
|
||||||
let mut buf_reader = io::BufReader::new(category_file);
|
let mut buf_reader = io::BufReader::new(category_file);
|
||||||
let mut contents = Vec::new();
|
let mut contents = Vec::new();
|
||||||
buf_reader.read_to_end(&mut contents).await?;
|
buf_reader.read_to_end(&mut contents).await?;
|
||||||
@ -130,7 +129,7 @@ impl RecipeStore for AsyncFileStore {
|
|||||||
.any(|&s| s == entry.file_name().to_string_lossy().to_string())
|
.any(|&s| s == entry.file_name().to_string_lossy().to_string())
|
||||||
{
|
{
|
||||||
// add it to the entry
|
// add it to the entry
|
||||||
info!("adding recipe file {}", entry.file_name().to_string_lossy());
|
debug!("adding recipe file {}", entry.file_name().to_string_lossy());
|
||||||
let recipe_contents = read_to_string(entry.path()).await?;
|
let recipe_contents = read_to_string(entry.path()).await?;
|
||||||
entry_vec.push(recipe_contents);
|
entry_vec.push(recipe_contents);
|
||||||
} else {
|
} else {
|
||||||
|
2
run.sh
2
run.sh
@ -15,6 +15,6 @@ EXAMPLES=${EXAMPLES:-../examples}
|
|||||||
make clean wasm kitchen
|
make clean wasm kitchen
|
||||||
pushd kitchen
|
pushd kitchen
|
||||||
echo Starting api server serving ${EXAMPLES}
|
echo Starting api server serving ${EXAMPLES}
|
||||||
cargo run -- serve --dir ${EXAMPLES}
|
cargo run -- --verbose debug serve --dir ${EXAMPLES}
|
||||||
popd
|
popd
|
||||||
# This is ghetto but I'm doing it anyway
|
# This is ghetto but I'm doing it anyway
|
Loading…
x
Reference in New Issue
Block a user