From 395714f7e0d1a79b2ac88844f9a94df5e63b2dc6 Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar <3998+srid@users.noreply.github.com> Date: Wed, 2 Aug 2023 18:47:01 -0400 Subject: [PATCH] Display nix version (#12) --- src/app.rs | 59 ++++++++++----------------------------------------- src/lib.rs | 2 +- src/main.rs | 2 +- src/nix.rs | 8 +++++++ src/server.rs | 16 +------------- src/thing.rs | 35 ------------------------------ 6 files changed, 22 insertions(+), 100 deletions(-) create mode 100644 src/nix.rs delete mode 100644 src/thing.rs diff --git a/src/app.rs b/src/app.rs index e9dc2784..93d3102d 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,4 +1,4 @@ -use crate::thing::{read_things, ReadThings, Thing}; +use crate::nix; use cfg_if::cfg_if; #[cfg(feature = "ssr")] use http::status::StatusCode; @@ -32,40 +32,20 @@ pub fn App(cx: Scope) -> impl IntoView { #[component] fn Home(cx: Scope) -> impl IntoView { - let thing = Thing::new("Hello from frontend".to_string()); - let things = create_local_resource(cx, move || (), move |_| read_things()); view! { cx,
- +
- -

"This value ⤵️ is generated in-browser:"

-
{thing.browser_view()}
- -
fn_url: {ReadThings::url()}
- {move || { - things.read(cx) - .map(move |things| { - log!("things: {:?}", things); - match things { - Err(e) => { - view! { cx,
"Server Error: " {e.to_string()}
}.into_view(cx) - } - Ok(things) => { - things.into_iter().map(move |thing| { - view! { - cx, -
  • {thing.browser_view()}
  • - } - }).collect_view(cx) - } - } - }) - }} - -
    - + +

    +                        
    +                            {format!("{data:?}")}
    +                        
    +                    

    @@ -105,20 +85,3 @@ fn NotFound(cx: Scope) -> impl IntoView { } } - -/// Renders the home page of your application. -#[component] -fn Counter(cx: Scope) -> impl IntoView { - // Creates a reactive value to update the button - let (count, set_count) = create_signal(cx, 0); - let on_click = move |_| set_count.update(|count| *count += 1); - - view! { cx, -
    - - -
    - } -} diff --git a/src/lib.rs b/src/lib.rs index 527fbbe4..f1313caf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ mod app; -mod thing; +mod nix; #[cfg(feature = "hydrate")] use wasm_bindgen::prelude::wasm_bindgen; diff --git a/src/main.rs b/src/main.rs index e8a494a9..44539612 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ mod app; +mod nix; #[cfg(feature = "ssr")] mod server; -mod thing; #[cfg(feature = "ssr")] #[tokio::main] diff --git a/src/nix.rs b/src/nix.rs new file mode 100644 index 00000000..e46be9ae --- /dev/null +++ b/src/nix.rs @@ -0,0 +1,8 @@ +use leptos::*; + +#[server(NixInfo, "/api")] +pub async fn nix_info() -> Result { + use tokio::process::Command; + let nix_version = Command::new("nix").arg("--version").output().await?.stdout; + String::from_utf8(nix_version).map_err(|e| e.into()) +} diff --git a/src/server.rs b/src/server.rs index 5d3c9043..c3246714 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,14 +1,9 @@ use std::convert::Infallible; use crate::app::App; -use crate::thing::{ReadThings, Thing}; use axum::response::Response as AxumResponse; use axum::{body::Body, http::Request, response::IntoResponse}; -use axum::{ - routing::{get, post}, - Router, -}; -use axum_macros::debug_handler; +use axum::{routing::post, Router}; use leptos::*; use leptos_axum::{generate_route_list, LeptosRoutes}; use tower_http::services::ServeDir; @@ -21,8 +16,6 @@ pub async fn main() { let not_found_service = tower::service_fn(move |req| not_found_handler(leptos_options.to_owned(), req)); let app = Router::new() - // custom routes - .route("/hello", get(root)) // server functions API routes .route("/api/*fn_name", post(leptos_axum::handle_server_fns)) // application routes @@ -32,7 +25,6 @@ pub async fn main() { .fallback_service(client_dist.clone().not_found_service(not_found_service)) .with_state(conf.leptos_options.clone()); println!("Launching http://{}", &conf.leptos_options.site_addr); - println!("fn_url: {}", ReadThings::url()); axum::Server::bind(&conf.leptos_options.site_addr) .serve(app.into_make_service()) .await @@ -49,9 +41,3 @@ pub async fn not_found_handler( leptos_axum::render_app_to_stream(options.to_owned(), move |cx| view! {cx, }); Ok(handler(req).await.into_response()) } - -#[debug_handler] -async fn root() -> String { - let thing = Thing::new("Hello from backend".to_string()); - serde_json::to_string(&thing).unwrap() -} diff --git a/src/thing.rs b/src/thing.rs deleted file mode 100644 index 83a0ba52..00000000 --- a/src/thing.rs +++ /dev/null @@ -1,35 +0,0 @@ -use leptos::*; -use serde::{Deserialize, Serialize}; -use std::env; - -#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone)] -pub struct Thing { - pub id: uuid::Uuid, - pub text: String, -} - -impl Thing { - pub fn new(text: String) -> Self { - Self { - id: uuid::Uuid::new_v4(), - text, - } - } - /// How this thing is displayed in the browser - pub fn browser_view(&self) -> String { - format!("Thing({}): {}", self.id, self.text) - } -} - -#[server(ReadThings, "/api")] -pub async fn read_things() -> Result, leptos::ServerFnError> { - Ok(vec![ - Thing::new("Hello 1 from backend".to_string()), - Thing::new("Hello 2 from backend".to_string()), - Thing::new("Hello 3 from backend".to_string()), - // NOTE: env::current_dir() will not work on wasm backend - // Thus, acting as proof that the server macro discards body when - // compiling on frontend. - Thing::new(format!("CWD: {}", env::current_dir().unwrap().display())), - ]) -}