From 82f6c337ee0d264d298cb4b4062cfe0ab6899175 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Thu, 25 Aug 2022 20:47:25 -0400 Subject: [PATCH] Fix routing issue for the cook page --- kitchen/src/web.rs | 124 ---------------------------------- nix/kitchenWasm/default.nix | 2 - web/src/router_integration.rs | 4 +- 3 files changed, 2 insertions(+), 128 deletions(-) delete mode 100644 kitchen/src/web.rs diff --git a/kitchen/src/web.rs b/kitchen/src/web.rs deleted file mode 100644 index cb9fe45..0000000 --- a/kitchen/src/web.rs +++ /dev/null @@ -1,124 +0,0 @@ -// Copyright 2022 Jeremy Wall -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -use std::net::SocketAddr; -use std::path::PathBuf; -use std::sync::Arc; - -use axum::{ - body::{boxed, Full}, - extract::{Extension, Path}, - http::{header, StatusCode}, - response::{IntoResponse, Redirect, Response}, - routing::{get, Router}, -}; -use mime_guess; -use recipe_store::{self, RecipeEntry, RecipeStore}; -use rust_embed::RustEmbed; -use tower_http::trace::TraceLayer; -use tracing::{debug, info, instrument}; - -#[derive(RustEmbed)] -#[folder = "../web/dist"] -struct UiAssets; - -pub struct StaticFile(pub T); - -impl IntoResponse for StaticFile -where - T: Into, -{ - fn into_response(self) -> Response { - let path = self.0.into(); - - match UiAssets::get(path.as_str()) { - Some(content) => { - let body = boxed(Full::from(content.data)); - let mime = mime_guess::from_path(path).first_or_octet_stream(); - Response::builder() - .header(header::CONTENT_TYPE, mime.as_ref()) - .body(body) - .unwrap() - } - None => Response::builder() - .status(StatusCode::NOT_FOUND) - .body(boxed(Full::from("404"))) - .unwrap(), - } - } -} - -#[instrument] -async fn ui_static_assets(Path(path): Path) -> impl IntoResponse { - info!("Serving ui path"); - - let mut path = path.trim_start_matches("/"); - path = if path == "" { "index.html" } else { path }; - debug!(path = path, "Serving transformed path"); - StaticFile(path.to_owned()) -} - -#[instrument] -async fn api_recipes(Extension(store): Extension>) -> Response { - let result: Result>, String> = match store - .get_recipes() - .await - .map_err(|e| format!("Error: {:?}", e)) - { - Ok(Some(recipes)) => Ok(axum::Json::from(recipes)), - Ok(None) => Ok(axum::Json::from(Vec::::new())), - Err(e) => Err(e), - }; - result.into_response() -} - -#[instrument] -async fn api_categories( - Extension(store): Extension>, -) -> Response { - let recipe_result = store - .get_categories() - .await - .map_err(|e| format!("Error: {:?}", e)); - let result: Result, String> = match recipe_result { - Ok(Some(categories)) => Ok(axum::Json::from(categories)), - Ok(None) => Ok(axum::Json::from(String::new())), - Err(e) => Err(e), - }; - result.into_response() -} - -#[instrument(fields(recipe_dir=?recipe_dir_path,listen=?listen_socket), skip_all)] -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 dir_path = (&dir_path).clone(); - let router = Router::new() - .route("/", get(|| async { Redirect::temporary("/ui/") })) - .route("/ui/*path", get(ui_static_assets)) - // recipes api path route - .route("/api/v1/recipes", get(api_recipes)) - // categories api path route - .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!( - http = format!("http://{}", listen_socket), - "Starting server" - ); - axum::Server::bind(&listen_socket) - .serve(router.into_make_service()) - .await - .expect("Failed to start service"); -} diff --git a/nix/kitchenWasm/default.nix b/nix/kitchenWasm/default.nix index fb1edf3..4a8e8e7 100644 --- a/nix/kitchenWasm/default.nix +++ b/nix/kitchenWasm/default.nix @@ -1,8 +1,6 @@ {pkgs? (import ) {}, version ? "0.2.1", - cargoVendorDeps ? (import ./../cargoVendorDeps/default.nix {inherit pkgs version; }), rust-wasm, - trunk ? (import ./../trunk/default.nix {inherit pkgs;}), }: with pkgs; let diff --git a/web/src/router_integration.rs b/web/src/router_integration.rs index 7063d4f..540c6aa 100644 --- a/web/src/router_integration.rs +++ b/web/src/router_integration.rs @@ -192,9 +192,9 @@ impl DeriveRoute for AppRoutes { debug!(origin=%input.0, path=%input.1, hash=%input.2, "routing"); let (_origin, path, _hash) = input; let route = match path.as_str() { - "" => AppRoutes::default(), + "" | "/" | "/ui/" => AppRoutes::default(), "/ui/plan" | "/" => AppRoutes::Plan, - "/ui/ook" => AppRoutes::Cook, + "/ui/cook" => AppRoutes::Cook, "/ui/inventory" => AppRoutes::Inventory, h => { // TODO(jwall): Parse the recipe hash