feat: create date api and dynamo context
The DynamoClient context should be used across the entire application - there are some significant performance improvements when under real load.
This commit is contained in:
1 parent
5ea7461a4d
commit
f15d151694
3 files changed
+95
-5
No files matched your search
+1
-1
@@ -2,7 +2,7 @@ pub mod app;
|
|||||||
mod common;
|
mod common;
|
||||||
mod components;
|
mod components;
|
||||||
mod pages;
|
mod pages;
|
||||||
mod utils;
|
pub mod utils;
|
||||||
|
|
||||||
#[cfg(feature = "hydrate")]
|
#[cfg(feature = "hydrate")]
|
||||||
#[wasm_bindgen::prelude::wasm_bindgen]
|
#[wasm_bindgen::prelude::wasm_bindgen]
|
||||||
|
|||||||
+19
-4
@@ -6,7 +6,12 @@ async fn main() {
|
|||||||
use leptos::prelude::*;
|
use leptos::prelude::*;
|
||||||
use leptos_axum::{LeptosRoutes, generate_route_list};
|
use leptos_axum::{LeptosRoutes, generate_route_list};
|
||||||
use scholarships_rs::app::*;
|
use scholarships_rs::app::*;
|
||||||
|
use scholarships_rs::utils::server::create_dynamo_client;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
let dynamo_client = create_dynamo_client().await;
|
||||||
|
let shared_client = Arc::new(dynamo_client);
|
||||||
|
|
||||||
let conf = get_configuration(None).unwrap();
|
let conf = get_configuration(None).unwrap();
|
||||||
let addr = conf.leptos_options.site_addr;
|
let addr = conf.leptos_options.site_addr;
|
||||||
let leptos_options = conf.leptos_options;
|
let leptos_options = conf.leptos_options;
|
||||||
@@ -14,10 +19,20 @@ async fn main() {
|
|||||||
let routes = generate_route_list(App);
|
let routes = generate_route_list(App);
|
||||||
|
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.leptos_routes(&leptos_options, routes, {
|
.leptos_routes_with_context(
|
||||||
let leptos_options = leptos_options.clone();
|
&leptos_options,
|
||||||
move || shell(leptos_options.clone())
|
routes,
|
||||||
})
|
{
|
||||||
|
let shared_client = Arc::clone(&shared_client);
|
||||||
|
move || {
|
||||||
|
provide_context(Arc::clone(&shared_client));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
let leptos_options = leptos_options.clone();
|
||||||
|
move || shell(leptos_options.clone())
|
||||||
|
}
|
||||||
|
)
|
||||||
.fallback(leptos_axum::file_and_error_handler(shell))
|
.fallback(leptos_axum::file_and_error_handler(shell))
|
||||||
.with_state(leptos_options);
|
.with_state(leptos_options);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,75 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
use leptos::logging::log;
|
||||||
|
use leptos::prelude::*;
|
||||||
|
use crate::common::DateInfo;
|
||||||
|
|
||||||
|
#[cfg(feature = "ssr")]
|
||||||
|
static DATES_TABLE: &str = "leptos-dates";
|
||||||
|
|
||||||
|
#[cfg(feature = "ssr")]
|
||||||
|
use aws_sdk_dynamodb::{
|
||||||
|
types::{WriteRequest, PutRequest},
|
||||||
|
Client as DynamoClient
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(feature = "ssr")]
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
#[server]
|
||||||
|
pub async fn create_dates(dates: Vec<DateInfo>) -> Result<(), ServerFnError> {
|
||||||
|
// Serialize and put dates into the database.
|
||||||
|
let client = expect_context::<Arc<DynamoClient>>();
|
||||||
|
|
||||||
|
log!("Creating dates: {:?}", dates);
|
||||||
|
|
||||||
|
let requests = dates
|
||||||
|
.into_iter()
|
||||||
|
.map(|date| {
|
||||||
|
let item = serde_dynamo::to_item(&date)
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
let put_request = PutRequest::builder()
|
||||||
|
.set_item(Some(item))
|
||||||
|
.build()
|
||||||
|
.expect("Couldn't build request, item is not present");
|
||||||
|
|
||||||
|
WriteRequest::builder().put_request(put_request).build()
|
||||||
|
})
|
||||||
|
.collect::<Vec<WriteRequest>>();
|
||||||
|
|
||||||
|
let mut request_map = HashMap::new();
|
||||||
|
request_map.insert(DATES_TABLE.to_string(), requests);
|
||||||
|
|
||||||
|
client
|
||||||
|
.batch_write_item()
|
||||||
|
.set_request_items(Some(request_map))
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map(|_| ())
|
||||||
|
.map_err(ServerFnError::from)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[server]
|
||||||
|
pub async fn get_important_dates() -> Result<Vec<DateInfo>, ServerFnError> {
|
||||||
|
let client = expect_context::<Arc<DynamoClient>>();
|
||||||
|
|
||||||
|
client
|
||||||
|
.scan()
|
||||||
|
.table_name(DATES_TABLE)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map(|output| {
|
||||||
|
if let Some(items) = output.items {
|
||||||
|
items
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|item| {
|
||||||
|
serde_dynamo::from_item::<_, DateInfo>(item)
|
||||||
|
.ok()
|
||||||
|
})
|
||||||
|
.collect::<Vec<DateInfo>>()
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.map_err(ServerFnError::from)
|
||||||
|
}
|
||||||
Reference in new issue
Block a user