mirror of
https://github.com/zaphar/kitchen.git
synced 2025-07-24 19:59:50 -04:00
Fix up axum routing
This commit is contained in:
parent
6a9e04ea74
commit
6f14dceab9
@ -19,16 +19,15 @@ use async_std::fs::{read_dir, read_to_string, DirEntry};
|
|||||||
use async_std::stream::StreamExt;
|
use async_std::stream::StreamExt;
|
||||||
use axum::{
|
use axum::{
|
||||||
body::{boxed, Full},
|
body::{boxed, Full},
|
||||||
extract::Extension,
|
extract::{Extension, Path},
|
||||||
handler::Handler,
|
|
||||||
http::{header, StatusCode, Uri},
|
http::{header, StatusCode, Uri},
|
||||||
response::{IntoResponse, 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 tracing::{info, instrument, warn};
|
use tracing::{debug, info, instrument, warn};
|
||||||
|
|
||||||
use crate::api::ParseError;
|
use crate::api::ParseError;
|
||||||
|
|
||||||
@ -90,16 +89,17 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn ui_static_assets(uri: Uri) -> impl IntoResponse {
|
#[instrument]
|
||||||
let path = uri
|
async fn ui_static_assets(Path(path): Path<String>) -> impl IntoResponse {
|
||||||
.path()
|
info!(path = path, "Serving ui path");
|
||||||
.trim_start_matches("/ui")
|
|
||||||
.trim_start_matches("/")
|
|
||||||
.to_string();
|
|
||||||
|
|
||||||
StaticFile(path)
|
let mut path = path.trim_start_matches("/");
|
||||||
|
path = if path == "" { "index.html" } else { path };
|
||||||
|
debug!(path = path, "Serving ui path");
|
||||||
|
StaticFile(path.to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(store))]
|
||||||
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,6 +113,7 @@ async fn api_recipes(Extension(store): Extension<Arc<recipe_store::AsyncFileStor
|
|||||||
result.into_response()
|
result.into_response()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(store))]
|
||||||
async fn api_categories(
|
async fn api_categories(
|
||||||
Extension(store): Extension<Arc<recipe_store::AsyncFileStore>>,
|
Extension(store): Extension<Arc<recipe_store::AsyncFileStore>>,
|
||||||
) -> Response {
|
) -> Response {
|
||||||
@ -130,16 +131,20 @@ async fn api_categories(
|
|||||||
|
|
||||||
#[instrument(fields(recipe_dir=?recipe_dir_path,listen=?listen_socket), skip_all)]
|
#[instrument(fields(recipe_dir=?recipe_dir_path,listen=?listen_socket), skip_all)]
|
||||||
pub async fn ui_main(recipe_dir_path: PathBuf, listen_socket: SocketAddr) {
|
pub async fn ui_main(recipe_dir_path: PathBuf, listen_socket: SocketAddr) {
|
||||||
let dir_path = recipe_dir_path.clone();
|
let store = Arc::new(recipe_store::AsyncFileStore::new(recipe_dir_path.clone()));
|
||||||
let store = Arc::new(recipe_store::AsyncFileStore::new(dir_path));
|
|
||||||
//let dir_path = (&dir_path).clone();
|
//let dir_path = (&dir_path).clone();
|
||||||
let router = Router::new()
|
let router = Router::new()
|
||||||
.layer(Extension(store))
|
.layer(Extension(store))
|
||||||
.route("/ui", ui_static_assets.into_service())
|
.route("/", get(|| async { Redirect::temporary("/ui/") }))
|
||||||
|
.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));
|
||||||
|
info!(
|
||||||
|
http = format!("http://{}", listen_socket),
|
||||||
|
"Starting server"
|
||||||
|
);
|
||||||
axum::Server::bind(&listen_socket)
|
axum::Server::bind(&listen_socket)
|
||||||
.serve(router.into_make_service())
|
.serve(router.into_make_service())
|
||||||
.await
|
.await
|
||||||
|
Loading…
x
Reference in New Issue
Block a user