mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 01:12:24 +00:00
frontend
This commit is contained in:
@@ -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>)),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user