This commit is contained in:
Lennart
2024-10-05 19:07:50 +02:00
parent a974ce677e
commit a119e7f099
17 changed files with 975 additions and 97 deletions

View File

@@ -1,15 +1,14 @@
use actix_session::{storage::CookieSessionStore, Session, SessionMiddleware};
use actix_session::{storage::CookieSessionStore, SessionMiddleware};
use actix_web::{
cookie::Key,
get,
http::Method,
web::{self, Data, Path},
Responder,
};
use askama::Template;
use login::{route_get_login, route_post_login};
use routes::login::{route_get_login, route_post_login};
use rustical_store::{
auth::{AuthenticationMiddleware, AuthenticationProvider, User},
auth::{AuthenticationMiddleware, AuthenticationProvider},
model::Calendar,
CalendarStore,
};
@@ -17,49 +16,26 @@ use std::sync::Arc;
use tokio::sync::RwLock;
mod config;
mod login;
mod routes;
pub use config::FrontendConfig;
#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate {}
#[get("")]
async fn route_index(session: Session) -> IndexTemplate {
if let Some(user) = session.get::<User>("user").unwrap() {
dbg!(user);
} else {
session.insert("user", "lennart").unwrap();
}
dbg!(session.status());
IndexTemplate {}
}
#[derive(Template)]
#[template(path = "components/calendar_list.html")]
struct CalendarList {
#[template(path = "pages/user.html")]
struct UserPage {
pub owner: String,
pub calendars: Vec<Calendar>,
}
#[derive(Template)]
#[template(path = "layouts/default.html")]
struct DefaultTemplate<Body: Template> {
pub body: Body,
}
async fn route_user<C: CalendarStore + ?Sized>(
path: Path<String>,
store: Data<RwLock<C>>,
) -> impl Responder {
let store = store.read().await;
let owner = path.into_inner();
DefaultTemplate {
body: CalendarList {
owner: owner.to_owned(),
calendars: store.get_calendars(&owner).await.unwrap(),
},
UserPage {
owner: owner.to_owned(),
calendars: store.get_calendars(&owner).await.unwrap(),
}
}
@@ -79,8 +55,11 @@ pub fn configure_frontend<AP: AuthenticationProvider, C: CalendarStore + ?Sized>
)
.app_data(Data::from(auth_provider))
.app_data(Data::from(store.clone()))
.service(actix_files::Files::new("/public", "crates/frontend/public").prefer_utf8(true))
.service(route_index)
.service(
// TODO: Bundle assets in a neat way
actix_files::Files::new("/assets", "crates/frontend/frontend/dist/assets")
.prefer_utf8(true),
)
.service(
web::resource("/user/{user}").route(web::method(Method::GET).to(route_user::<C>)),
)

View File

@@ -8,14 +8,12 @@ use askama::Template;
use rustical_store::auth::AuthenticationProvider;
use serde::Deserialize;
use crate::DefaultTemplate;
#[derive(Template)]
#[template(path = "components/login.html")]
struct LoginForm;
#[template(path = "pages/login.html")]
struct LoginPage;
pub async fn route_get_login() -> impl Responder {
DefaultTemplate { body: LoginForm }
LoginPage
}
#[derive(Deserialize)]

View File

@@ -0,0 +1 @@
pub mod login;