@@ -16,3 +16,4 @@ tokio.workspace = true
|
||||
tower.workspace = true
|
||||
tower-http.workspace = true
|
||||
log.workspace = true
|
||||
dotenvy = "0.15.7"
|
||||
|
@@ -1,44 +0,0 @@
|
||||
use app::App;
|
||||
use axum::response::Response as AxumResponse;
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::State,
|
||||
http::{Request, Response, StatusCode, Uri},
|
||||
response::IntoResponse,
|
||||
};
|
||||
use leptos::*;
|
||||
use tower::ServiceExt;
|
||||
use tower_http::services::ServeDir;
|
||||
|
||||
pub async fn file_and_error_handler(
|
||||
uri: Uri,
|
||||
State(options): State<LeptosOptions>,
|
||||
req: Request<Body>,
|
||||
) -> AxumResponse {
|
||||
let root = options.site_root.clone();
|
||||
let res = get_static_file(uri.clone(), &root).await.unwrap();
|
||||
|
||||
if res.status() == StatusCode::OK {
|
||||
res.into_response()
|
||||
} else {
|
||||
let handler =
|
||||
leptos_axum::render_app_to_stream(options.to_owned(), move || view! { <App/> });
|
||||
handler(req).await.into_response()
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_static_file(uri: Uri, root: &str) -> Result<Response<Body>, (StatusCode, String)> {
|
||||
let req = Request::builder()
|
||||
.uri(uri.clone())
|
||||
.body(Body::empty())
|
||||
.unwrap();
|
||||
// `ServeDir` implements `tower::Service` so we can call it with `tower::ServiceExt::oneshot`
|
||||
// This path is relative to the cargo root
|
||||
match ServeDir::new(root).oneshot(req).await {
|
||||
Ok(res) => Ok(res.map(Body::new)),
|
||||
Err(err) => Err((
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Something went wrong: {err}"),
|
||||
)),
|
||||
}
|
||||
}
|
@@ -1,14 +1,14 @@
|
||||
use app::*;
|
||||
use axum::Router;
|
||||
use fileserv::file_and_error_handler;
|
||||
use leptos::*;
|
||||
use leptos::prelude::*;
|
||||
use leptos_axum::{generate_route_list, LeptosRoutes};
|
||||
use state::State;
|
||||
use tokio::signal;
|
||||
|
||||
pub mod fileserv;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let _ = dotenvy::dotenv();
|
||||
|
||||
simple_logger::init_with_level(log::Level::Debug).expect("couldn't initialize logging");
|
||||
|
||||
// Setting get_configuration(None) means we'll be using cargo-leptos's env values
|
||||
@@ -16,15 +16,31 @@ async fn main() {
|
||||
// <https://github.com/leptos-rs/start-axum#executing-a-server-on-a-remote-machine-without-the-toolchain>
|
||||
// Alternately a file can be specified such as Some("Cargo.toml")
|
||||
// The file would need to be included with the executable when moved to deployment
|
||||
let conf = get_configuration(None).await.unwrap();
|
||||
let conf = get_configuration(None).unwrap();
|
||||
|
||||
let leptos_options = conf.leptos_options;
|
||||
let state = State {};
|
||||
|
||||
let addr = leptos_options.site_addr;
|
||||
let routes = generate_route_list(App);
|
||||
|
||||
// build our application with a route
|
||||
let app = Router::new()
|
||||
.leptos_routes(&leptos_options, routes, App)
|
||||
.fallback(file_and_error_handler)
|
||||
.leptos_routes_with_context(
|
||||
&leptos_options,
|
||||
routes,
|
||||
{
|
||||
let state = state.clone();
|
||||
move || {
|
||||
provide_context(state.clone());
|
||||
}
|
||||
},
|
||||
{
|
||||
let leptos_options = leptos_options.clone();
|
||||
move || shell(leptos_options.clone())
|
||||
},
|
||||
)
|
||||
.fallback(leptos_axum::file_and_error_handler(shell))
|
||||
.with_state(leptos_options);
|
||||
|
||||
// run our app with hyper
|
||||
|
@@ -12,14 +12,35 @@ leptos_router.workspace = true
|
||||
server_fn.workspace = true
|
||||
leptos_axum = { workspace = true, optional = true }
|
||||
|
||||
tokio = {workspace = true, optional = true}
|
||||
tokio = { workspace = true, optional = true }
|
||||
serde.workspace = true
|
||||
http.workspace = true
|
||||
cfg-if.workspace = true
|
||||
thiserror.workspace = true
|
||||
cynic = { version = "3.9.1", optional = true, features = [
|
||||
"http-reqwest",
|
||||
"reqwest",
|
||||
"serde_json",
|
||||
] }
|
||||
reqwest = { version = "0.12.11", optional = true, features = ["json"] }
|
||||
axum = { workspace = true, optional = true, features = ["macros"] }
|
||||
serde_json = { version = "1.0.134", optional = true }
|
||||
|
||||
[features]
|
||||
default = []
|
||||
hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"]
|
||||
ssr = ["leptos/ssr", "leptos_meta/ssr", "leptos_router/ssr", "dep:leptos_axum", "dep:tokio"]
|
||||
|
||||
hydrate = ["leptos/hydrate"]
|
||||
ssr = [
|
||||
"leptos/ssr",
|
||||
"leptos_meta/ssr",
|
||||
"leptos_router/ssr",
|
||||
"dep:leptos_axum",
|
||||
"dep:tokio",
|
||||
"dep:cynic",
|
||||
"dep:reqwest",
|
||||
"dep:axum",
|
||||
"dep:serde_json",
|
||||
]
|
||||
cynic = ["dep:cynic"]
|
||||
reqwest = ["dep:reqwest"]
|
||||
axum = ["dep:axum"]
|
||||
serde_json = ["dep:serde_json"]
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use cfg_if::cfg_if;
|
||||
use http::status::StatusCode;
|
||||
use leptos::*;
|
||||
use leptos::prelude::*;
|
||||
use thiserror::Error;
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
@@ -28,7 +28,7 @@ pub fn ErrorTemplate(
|
||||
#[prop(optional)] errors: Option<RwSignal<Errors>>,
|
||||
) -> impl IntoView {
|
||||
let errors = match outside_errors {
|
||||
Some(e) => create_rw_signal(e),
|
||||
Some(e) => RwSignal::new(e),
|
||||
None => match errors {
|
||||
Some(e) => e,
|
||||
None => panic!("No Errors found and we expected errors!"),
|
||||
|
@@ -1,32 +1,52 @@
|
||||
use crate::error_template::{AppError, ErrorTemplate};
|
||||
|
||||
use leptos::*;
|
||||
use leptos::prelude::*;
|
||||
use leptos_meta::*;
|
||||
use leptos_router::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use leptos_router::{components::*, StaticSegment};
|
||||
|
||||
pub mod error_template;
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
pub mod state;
|
||||
|
||||
pub fn shell(options: LeptosOptions) -> impl IntoView {
|
||||
view! {
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<AutoReload options=options.clone() />
|
||||
<HydrationScripts options />
|
||||
<MetaTags />
|
||||
</head>
|
||||
<body>
|
||||
<App />
|
||||
</body>
|
||||
</html>
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn App() -> impl IntoView {
|
||||
// Provides context that manages stylesheets, titles, meta tags, etc.
|
||||
provide_meta_context();
|
||||
|
||||
view! {
|
||||
<Stylesheet id="leptos" href="/pkg/%%name%%.css"/>
|
||||
<Stylesheet id="leptos" href="/pkg/%%name%%.css" />
|
||||
|
||||
// sets the document title
|
||||
<Title text="%%name%%"/>
|
||||
<Title text="%%name%%" />
|
||||
|
||||
// content for this welcome page
|
||||
<Router fallback=|| {
|
||||
let mut outside_errors = Errors::default();
|
||||
outside_errors.insert_with_default_key(AppError::NotFound);
|
||||
view! { <ErrorTemplate outside_errors/> }.into_view()
|
||||
}>
|
||||
<main>
|
||||
<Routes>
|
||||
<Route ssr=SsrMode::Async path="" view=HomePage/>
|
||||
<Router>
|
||||
<main class="">
|
||||
<Routes fallback=|| {
|
||||
let mut outside_errors = Errors::default();
|
||||
outside_errors.insert_with_default_key(AppError::NotFound);
|
||||
view! { <ErrorTemplate outside_errors /> }.into_view()
|
||||
}>
|
||||
<Route path=StaticSegment("") view=pages::home::HomePage />
|
||||
</Routes>
|
||||
</main>
|
||||
</Router>
|
||||
@@ -34,6 +54,8 @@ pub fn App() -> impl IntoView {
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn HomePage() -> impl IntoView {
|
||||
view! {<h1> Hello %%name%% </h1>}
|
||||
pub fn HomePage() -> impl IntoView {
|
||||
view! {
|
||||
<h1> "%%name%%" </h1>
|
||||
}
|
||||
}
|
||||
|
14
cuddle-leptos/crates/app/src/state.rs
Normal file
14
cuddle-leptos/crates/app/src/state.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use axum::extract::FromRef;
|
||||
use leptos::prelude::expect_context;
|
||||
use server_fn::ServerFnError;
|
||||
|
||||
#[derive(FromRef, Clone)]
|
||||
pub struct State {}
|
||||
|
||||
pub async fn get_state() -> Result<State, ServerFnError> {
|
||||
let state = expect_context::<crate::state::State>();
|
||||
let axum::extract::State(state): axum::extract::State<crate::state::State> =
|
||||
leptos_axum::extract_with_state(&state).await?;
|
||||
|
||||
Ok(state)
|
||||
}
|
@@ -1,12 +1,10 @@
|
||||
use app::*;
|
||||
use leptos::*;
|
||||
use wasm_bindgen::prelude::wasm_bindgen;
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[wasm_bindgen::prelude::wasm_bindgen]
|
||||
pub fn hydrate() {
|
||||
use app::*;
|
||||
|
||||
// initializes logging using the `log` crate
|
||||
_ = console_log::init_with_level(log::Level::Debug);
|
||||
console_error_panic_hook::set_once();
|
||||
|
||||
leptos::mount_to_body(App);
|
||||
leptos::mount::hydrate_body(App);
|
||||
}
|
||||
|
Reference in New Issue
Block a user