Serve static files

This commit is contained in:
2025-01-22 20:35:52 +01:00
parent b4a823a35f
commit f31e0ebbcc
6 changed files with 12320 additions and 5 deletions

View File

@ -155,3 +155,14 @@ pub struct OIDCProvider<'a> {
pub configuration_url: &'a str,
pub redirect_url: String,
}
#[cfg(test)]
mod test {
use crate::app_config::AppConfig;
#[test]
fn verify_cli() {
use clap::CommandFactory;
AppConfig::command().debug_assert()
}
}

View File

@ -1,3 +1,4 @@
use actix_session::config::SessionLifecycle;
use actix_session::{storage::RedisSessionStore, SessionMiddleware};
use actix_web::cookie::Key;
use actix_web::{web, App, HttpServer};
@ -23,11 +24,14 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
App::new()
// Add session management to your application using Redis for session state storage
.wrap(SessionMiddleware::new(
redis_store.clone(),
secret_key.clone(),
))
.wrap(
SessionMiddleware::builder(redis_store.clone(), secret_key.clone())
.cookie_name("matrixgw-session".to_string())
.session_lifecycle(SessionLifecycle::BrowserSession(Default::default()))
.build(),
)
// Web configuration routes
.route("/static/{tail:.*}", web::get().to(web_ui::static_file))
.route("/", web::get().to(web_ui::home))
.route("/oidc_cb", web::get().to(web_ui::oidc_cb))
.route("/sign_out", web::get().to(web_ui::sign_out))

View File

@ -7,6 +7,25 @@ use actix_session::Session;
use actix_web::{web, HttpResponse};
use light_openid::primitives::OpenIDConfig;
/// Static assets
#[derive(rust_embed::Embed)]
#[folder = "assets/"]
struct Assets;
/// Serve static file
pub async fn static_file(path: web::Path<String>) -> HttpResult {
match Assets::get(path.as_ref()) {
Some(content) => Ok(HttpResponse::Ok()
.content_type(
mime_guess::from_path(path.as_str())
.first_or_octet_stream()
.as_ref(),
)
.body(content.data.into_owned())),
None => Ok(HttpResponse::NotFound().body("404 Not Found")),
}
}
/// Main route
pub async fn home(session: Session) -> HttpResult {
// Get user information, requesting authentication if information is missing