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(|| {