mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 14:42:30 +00:00
73 lines
2.2 KiB
Rust
73 lines
2.2 KiB
Rust
use actix_session::{storage::CookieSessionStore, SessionMiddleware};
|
|
use actix_web::{
|
|
cookie::Key,
|
|
http::Method,
|
|
web::{self, Data, Path},
|
|
Responder,
|
|
};
|
|
use askama::Template;
|
|
use routes::login::{route_get_login, route_post_login};
|
|
use rustical_store::{
|
|
auth::{AuthenticationMiddleware, AuthenticationProvider},
|
|
model::Calendar,
|
|
CalendarStore,
|
|
};
|
|
use std::sync::Arc;
|
|
use tokio::sync::RwLock;
|
|
|
|
mod config;
|
|
mod routes;
|
|
|
|
pub use config::FrontendConfig;
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "pages/user.html")]
|
|
struct UserPage {
|
|
pub owner: String,
|
|
pub calendars: Vec<Calendar>,
|
|
}
|
|
|
|
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();
|
|
UserPage {
|
|
owner: owner.to_owned(),
|
|
calendars: store.get_calendars(&owner).await.unwrap(),
|
|
}
|
|
}
|
|
|
|
pub fn configure_frontend<AP: AuthenticationProvider, C: CalendarStore + ?Sized>(
|
|
cfg: &mut web::ServiceConfig,
|
|
auth_provider: Arc<AP>,
|
|
store: Arc<RwLock<C>>,
|
|
) {
|
|
cfg.service(
|
|
web::scope("")
|
|
.wrap(AuthenticationMiddleware::new(auth_provider.clone()))
|
|
.wrap(
|
|
SessionMiddleware::builder(CookieSessionStore::default(), Key::from(&[0; 64]))
|
|
.cookie_secure(true)
|
|
.cookie_content_security(actix_session::config::CookieContentSecurity::Private)
|
|
.build(),
|
|
)
|
|
.app_data(Data::from(auth_provider))
|
|
.app_data(Data::from(store.clone()))
|
|
.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>)),
|
|
)
|
|
.service(
|
|
web::resource("/login")
|
|
.route(web::method(Method::GET).to(route_get_login))
|
|
.route(web::method(Method::POST).to(route_post_login::<AP>)),
|
|
),
|
|
);
|
|
}
|