From 4091d212774af42a58670620264f5bb348693deb Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Sun, 9 May 2021 11:06:23 +0200 Subject: [PATCH] Setup the routes for the static files --- Cargo.toml | 6 ++++++ src/infra/tcp_server.rs | 25 ++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1ec9b30..ee13d82 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,8 +4,14 @@ edition = "2018" name = "lldap" version = "0.1.0" +[patch.crates-io] +actix-files = { git = "https://github.com/actix/actix-web", rev = "a9dc1586a0935c48c3f841761bf81c43ca9e2651" } +actix-http = { git = "https://github.com/actix/actix-web", rev = "a9dc1586a0935c48c3f841761bf81c43ca9e2651" } +actix-web = { git = "https://github.com/actix/actix-web", rev = "a9dc1586a0935c48c3f841761bf81c43ca9e2651" } + [dependencies] actix = "0.11.1" +actix-files = "0.6.0-beta.4" actix-http = "3.0.0-beta.6" actix-rt = "2.2" actix-server = "2.0.0-beta.5" diff --git a/src/infra/tcp_server.rs b/src/infra/tcp_server.rs index ba124da..7d5c022 100644 --- a/src/infra/tcp_server.rs +++ b/src/infra/tcp_server.rs @@ -1,11 +1,20 @@ use crate::domain::handler::*; use crate::infra::configuration::Configuration; +use actix_files::{Files, NamedFile}; use actix_http::HttpServiceBuilder; use actix_server::ServerBuilder; use actix_service::map_config; -use actix_web::dev::AppConfig; -use actix_web::App; +use actix_web::{dev::AppConfig, web, App, HttpRequest}; use anyhow::{Context, Result}; +use std::path::PathBuf; + +async fn index(req: HttpRequest) -> actix_web::Result { + let mut path = PathBuf::new(); + path.push("app"); + let file = req.match_info().query("filename"); + path.push(if file.is_empty() { "index.html" } else { file }); + Ok(NamedFile::open(path)?) +} pub fn build_tcp_server( config: &Configuration, @@ -18,7 +27,17 @@ where server_builder .bind("http", ("0.0.0.0", config.http_port), move || { HttpServiceBuilder::new() - .finish(map_config(App::new(), |_| AppConfig::default())) + .finish(map_config( + App::new() + // Serve index.html and main.js, and default to index.html. + .route( + "/{filename:(index\\.html|main\\.js)?}", + web::get().to(index), + ) + // Serve the /pkg path with the compiled WASM app. + .service(Files::new("/pkg", "./app/pkg")), + |_| AppConfig::default(), + )) .tcp() }) .with_context(|| {