Fix routing issue for the cook page

This commit is contained in:
Jeremy Wall 2022-08-25 20:47:25 -04:00
parent ba64e5a094
commit 82f6c337ee
3 changed files with 2 additions and 128 deletions

View File

@ -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<T>(pub T);
impl<T> IntoResponse for StaticFile<T>
where
T: Into<String>,
{
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<String>) -> 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<Arc<recipe_store::AsyncFileStore>>) -> Response {
let result: Result<axum::Json<Vec<RecipeEntry>>, 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::<RecipeEntry>::new())),
Err(e) => Err(e),
};
result.into_response()
}
#[instrument]
async fn api_categories(
Extension(store): Extension<Arc<recipe_store::AsyncFileStore>>,
) -> Response {
let recipe_result = store
.get_categories()
.await
.map_err(|e| format!("Error: {:?}", e));
let result: Result<axum::Json<String>, 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");
}

View File

@ -1,8 +1,6 @@
{pkgs? (import <nixpkgs>) {},
version ? "0.2.1",
cargoVendorDeps ? (import ./../cargoVendorDeps/default.nix {inherit pkgs version; }),
rust-wasm,
trunk ? (import ./../trunk/default.nix {inherit pkgs;}),
}:
with pkgs;
let

View File

@ -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