Cleanup of testing files

This commit is contained in:
Ben Wishovich 2022-12-16 17:35:32 -08:00
parent 2d289dd2b6
commit 6fa15a5584
5 changed files with 46 additions and 48 deletions

View file

@ -1,5 +1,4 @@
use cfg_if::cfg_if;
use http::{header::SET_COOKIE, HeaderMap, HeaderValue, StatusCode};
use leptos::*;
use leptos_router::*;
use serde::{Deserialize, Serialize};
@ -7,6 +6,8 @@ use serde::{Deserialize, Serialize};
cfg_if! {
if #[cfg(feature = "ssr")] {
use sqlx::{Connection, SqliteConnection};
use http::{header::SET_COOKIE, HeaderMap, HeaderValue, StatusCode};
pub async fn db() -> Result<SqliteConnection, ServerFnError> {
Ok(SqliteConnection::connect("sqlite:Todos.db").await.map_err(|e| ServerFnError::ServerError(e.to_string()))?)
}

View file

@ -1,5 +1,4 @@
use cfg_if::cfg_if;
use http::{header::SET_COOKIE, HeaderMap, HeaderValue, StatusCode};
use leptos::*;
use leptos_meta::*;
use leptos_router::*;
@ -8,6 +7,8 @@ use serde::{Deserialize, Serialize};
cfg_if! {
if #[cfg(feature = "ssr")] {
use sqlx::{Connection, SqliteConnection};
use http::{header::SET_COOKIE, HeaderMap, HeaderValue, StatusCode};
pub async fn db() -> Result<SqliteConnection, ServerFnError> {
Ok(SqliteConnection::connect("sqlite:Todos.db").await.map_err(|e| ServerFnError::ServerError(e.to_string()))?)
@ -60,21 +61,6 @@ pub async fn get_todos(cx: Scope) -> Result<Vec<Todo>, ServerFnError> {
todos.push(row);
}
// Add a random header(because why not)
let mut res_headers = HeaderMap::new();
res_headers.insert(SET_COOKIE, HeaderValue::from_str("fizz=buzz").unwrap());
let res_parts = leptos_actix::ResponseParts {
headers: res_headers.into(),
status: Some(StatusCode::IM_A_TEAPOT),
};
let res_options_outer = use_context::<leptos_actix::ResponseOptions>(cx);
if let Some(res_options) = res_options_outer {
println!("Setting Options");
res_options.overwrite(res_parts).await;
}
Ok(todos)
}

View file

@ -276,23 +276,38 @@ pub fn render_app_to_stream(
.unwrap_or_default();
format!("{head}</head><body>{app}")
}))
.chain(futures::stream::once(async { tail.to_string() }))
.map(|html| Ok(web::Bytes::from(html)) as Result<web::Bytes>));
.chain(futures::stream::once(async { tail.to_string() }))
.map(|html| Ok(web::Bytes::from(html)) as Result<web::Bytes>));
// Get the first and second chunks in the stream, which renders the app shell, and thus allows Resources to run
let first_chunk = stream.next().await;
let second_chunk = stream.next().await;
// Get the first, second, and third chunks in the stream, which renders the app shell, and thus allows Resources to run
let first_chunk = stream.next().await;
let second_chunk = stream.next().await;
let third_chunk = stream.next().await;
let mut res_options = res_options.0.read().await;
println!("Reading Options");
println!("Response Options: {:#?}", res_options);
let complete_stream =
futures::stream::iter([first_chunk.unwrap(), second_chunk.unwrap()])
.chain(stream);
HttpResponse::Ok().content_type("text/html").streaming(
let res_options = res_options.0.read().await;
println!("Reading Options");
println!("Response Options: {:#?}", res_options);
let (status, mut headers) = (res_options.status.clone(), res_options.headers.clone());
let status = status.unwrap_or_default();
let complete_stream =
futures::stream::iter([first_chunk.unwrap(), second_chunk.unwrap(), third_chunk.unwrap()])
.chain(stream);
let mut res = HttpResponse::Ok().content_type("text/html").streaming(
complete_stream
)
);
// Add headers manipulated in the response
for (key, value) in headers.drain(){
if let Some(key) = key{
res.headers_mut().append(key, value);
}
};
// Set status to what is returned in the function
let res_status = res.status_mut();
*res_status = status;
// Return the response
res
}
})
}

View file

@ -38,7 +38,7 @@ pub struct ResponseParts {
pub struct ResponseOptions(pub Arc<RwLock<ResponseParts>>);
impl ResponseOptions {
/// A less boilerplatey way to overwrite the contents of `ResponseOptions` with a new `ResponseParts`
/// A less boilerplatey way to overwrite the default contents of `ResponseOptions` with a new `ResponseParts`
pub async fn overwrite(&self, parts: ResponseParts) {
let mut writable = self.0.write().await;
*writable = parts
@ -366,10 +366,6 @@ pub fn render_app_to_stream(
let res_options =
use_context::<ResponseOptions>(cx).unwrap();
println!(
"Inner Response Options: {:#?}",
res_options.0.read().await
);
let new_res_parts = res_options.0.read().await.clone();
let mut writable = res_options2.0.write().await;
@ -386,24 +382,27 @@ pub fn render_app_to_stream(
}
});
let stream = Box::pin(
let mut stream = Box::pin(
futures::stream::once(async move { head.clone() })
.chain(rx)
.chain(futures::stream::once(async { tail.to_string() }))
.map(|html| Ok(Bytes::from(html))),
);
// Get the first and second chunks in the stream, which renders the app shell, and thus allows Resources to run
// Get the first, second, and third chunks in the stream, which renders the app shell, and thus allows Resources to run
let first_chunk = stream.next().await;
let second_chunk = stream.next().await;
let third_chunk = stream.next().await;
// Extract the resources now that they've been rendered
let mut res_options = res_options3.0.read().await;
println!("Response Options: {:#?}", res_options);
let res_options = res_options3.0.read().await;
let complete_stream =
futures::stream::iter([first_chunk.unwrap(), second_chunk.unwrap()])
.chain(stream);
let complete_stream = futures::stream::iter([
first_chunk.unwrap(),
second_chunk.unwrap(),
third_chunk.unwrap(),
])
.chain(stream);
let mut res = Response::new(StreamBody::new(
Box::pin(complete_stream) as PinnedHtmlStream

View file

@ -266,11 +266,8 @@ where
let value = match Self::encoding() {
Encoding::Url => serde_urlencoded::from_bytes(data)
.map_err(|e| ServerFnError::Deserialization(e.to_string())),
Encoding::Cbor => {
println!("Deserialize Cbor!: {:x?}", &data);
ciborium::de::from_reader(data)
.map_err(|e| ServerFnError::Deserialization(e.to_string()))
}
Encoding::Cbor => ciborium::de::from_reader(data)
.map_err(|e| ServerFnError::Deserialization(e.to_string())),
};
Box::pin(async move {
let value: Self = match value {