Handlebars: init at app startup and save handlebars to app state.
This commit is contained in:
parent
02d641f711
commit
f54b4e591d
|
@ -4,19 +4,19 @@ use crate::handlers;
|
|||
use crate::models;
|
||||
|
||||
|
||||
pub fn all(state: models::AppState) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
|
||||
pub fn all(state: models::AppState) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone + '_{
|
||||
landing(state.clone())
|
||||
.or(start_webauthn_registration(state.clone()))
|
||||
.or(start_webauthn_registration(state.clone()))
|
||||
}
|
||||
|
||||
pub fn landing(state: models::AppState) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
|
||||
pub fn landing(state: models::AppState) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone + '_ {
|
||||
warp::path::end()
|
||||
.and(warp::any().map(move || state.clone()))
|
||||
.and_then(handlers::landing_page)
|
||||
}
|
||||
|
||||
pub fn start_webauthn_registration(state: models::AppState) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
|
||||
pub fn start_webauthn_registration(state: models::AppState) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone + '_ {
|
||||
warp::path!("webauthn" / "start-registration")
|
||||
.and(warp::post())
|
||||
.and(json_body())
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
use std::convert::Infallible;
|
||||
use warp::Reply;
|
||||
|
||||
use crate::{models::{AppState, Db, User}, templates};
|
||||
use crate::{models::{AppState, User}, templates};
|
||||
|
||||
pub async fn landing_page (app_state: AppState) -> Result<impl Reply, Infallible> {
|
||||
let content: String = templates::landing_page().unwrap();
|
||||
pub async fn landing_page (app_state: AppState<'_>) -> Result<impl Reply, Infallible> {
|
||||
let content: String = templates::landing_page(app_state).unwrap();
|
||||
Ok(warp::reply::html(content))
|
||||
}
|
||||
|
||||
pub async fn start_webauthn_registration(user: User, app_state: AppState) -> Result<impl warp::Reply, Infallible> {
|
||||
pub async fn start_webauthn_registration(user: User, app_state: AppState<'_>) -> Result<impl warp::Reply, Infallible> {
|
||||
let mut db = app_state.db.lock().await;
|
||||
// TODO: query the user
|
||||
let mut user = db.users.first_mut().unwrap();
|
||||
|
|
|
@ -4,6 +4,7 @@ use std::sync::{Arc};
|
|||
|
||||
use tokio::sync::{Mutex};
|
||||
|
||||
use handlebars::Handlebars;
|
||||
use webauthn_rs::prelude::Uuid;
|
||||
use webauthn_rs::{Webauthn, WebauthnBuilder};
|
||||
|
||||
|
@ -22,12 +23,13 @@ pub struct User {
|
|||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AppState {
|
||||
pub struct AppState<'a>{
|
||||
pub webauthn: Arc<Webauthn>,
|
||||
pub db: Db
|
||||
pub db: Db,
|
||||
pub hbs: Arc<Handlebars<'a>>
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
impl AppState<'_> {
|
||||
pub fn new() -> Self {
|
||||
let rp = "localhost";
|
||||
let rp_origin = Url::parse("http://localhost:8000").expect("Invalid URL");
|
||||
|
@ -43,10 +45,12 @@ impl AppState {
|
|||
let db: Db = Arc::new(Mutex::new(Database {
|
||||
users: Vec::from([user])
|
||||
}));
|
||||
let hbs = Arc::new(crate::templates::new().unwrap());
|
||||
|
||||
AppState {
|
||||
webauthn,
|
||||
db,
|
||||
hbs
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,23 @@
|
|||
use handlebars::RenderError;
|
||||
use serde_json::json;
|
||||
use handlebars::Handlebars;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub fn landing_page() -> Result<String, RenderError> {
|
||||
let mut hb = handlebars::Handlebars::new();
|
||||
use crate::models::AppState;
|
||||
|
||||
pub fn new<'a>() -> Result<Handlebars<'a>, RenderError> {
|
||||
let rootpath = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
let landing_path = rootpath.join("src/templates/landing.hbs");
|
||||
let mut hbs = handlebars::Handlebars::new();
|
||||
let css = rootpath.join("src/templates/main.css");
|
||||
hb.register_template_file("landing", landing_path.to_str().unwrap())?;
|
||||
hb.register_template_file("css", css.to_str().unwrap())?;
|
||||
hbs.register_template_file("landing", landing_path.to_str().unwrap())?;
|
||||
hbs.register_template_file("css", css.to_str().unwrap())?;
|
||||
return Ok(hbs)
|
||||
}
|
||||
|
||||
pub fn landing_page(app_state: AppState<'_>) -> Result<String, RenderError> {
|
||||
let hb = app_state.hbs;
|
||||
let data = json!({
|
||||
});
|
||||
hb.render("landing", &data)
|
||||
|
|
Loading…
Reference in a new issue