Skip to content

Commit

Permalink
Display nix version (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
srid authored Aug 2, 2023
1 parent 9362e92 commit 395714f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 100 deletions.
59 changes: 11 additions & 48 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
<div class="flex flex-col items-center justify-center min-h-screen bg-blue-300">
<div class="flex flex-col items-center justify-start px-4 py-8 mx-auto bg-white border-4 rounded-lg">
<Header1 text="Welcome to nix-browser placeholder" />
<Header1 text="Welcome to nix-browser" />
<div class="items-left">
<Header2 text="Frontend" />
<p class="my-1">"This value ⤵️ is generated in-browser:"</p>
<pre>{thing.browser_view()}</pre>
<Header2 text="Backend" />
<pre>fn_url: {ReadThings::url()}</pre>
{move || {
things.read(cx)
.map(move |things| {
log!("things: {:?}", things);
match things {
Err(e) => {
view! { cx, <pre class="p-2 my-2 font-bold bg-red-200 shadow-lg">"Server Error: " {e.to_string()}</pre>}.into_view(cx)
}
Ok(things) => {
things.into_iter().map(move |thing| {
view! {
cx,
<li>{thing.browser_view()}</li>
}
}).collect_view(cx)
}
}
})
}}
<Link link="/hello" text="request backend /hello API" rel="external" />
<div><Link link="/sdf" text="broken link" /></div>
<Counter />
<Header2 text="Nix Info" />
<p class="my-1"><pre>
<Await
future=|_| nix::nix_info()
bind:data
>
{format!("{data:?}")}
</Await>
</pre></p>
</div>
</div>
</div>
Expand Down Expand Up @@ -105,20 +85,3 @@ fn NotFound(cx: Scope) -> impl IntoView {
</div>
}
}

/// 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,
<div class="mx-auto my-8 text-center md:container">
<Header2 text="Leptops Counter" />
<button
class="p-4 border-2 rounded-full shadow-lg active:shadow-none bg-blue-50 hover:bg-blue-200 active:bg-blue-500"
on:click=on_click>"Click Me: " {count}</button>
</div>
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod app;
mod thing;
mod nix;
#[cfg(feature = "hydrate")]
use wasm_bindgen::prelude::wasm_bindgen;

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod app;
mod nix;
#[cfg(feature = "ssr")]
mod server;
mod thing;

#[cfg(feature = "ssr")]
#[tokio::main]
Expand Down
8 changes: 8 additions & 0 deletions src/nix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use leptos::*;

#[server(NixInfo, "/api")]
pub async fn nix_info() -> Result<String, ServerFnError> {
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())
}
16 changes: 1 addition & 15 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -49,9 +41,3 @@ pub async fn not_found_handler(
leptos_axum::render_app_to_stream(options.to_owned(), move |cx| view! {cx, <App/>});
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()
}
35 changes: 0 additions & 35 deletions src/thing.rs

This file was deleted.

0 comments on commit 395714f

Please sign in to comment.