Setup the routes for the static files

This commit is contained in:
Valentin Tolmer 2021-05-09 11:06:23 +02:00
parent 9dd94f12a6
commit 4091d21277
2 changed files with 28 additions and 3 deletions

View File

@ -4,8 +4,14 @@ edition = "2018"
name = "lldap" name = "lldap"
version = "0.1.0" 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] [dependencies]
actix = "0.11.1" actix = "0.11.1"
actix-files = "0.6.0-beta.4"
actix-http = "3.0.0-beta.6" actix-http = "3.0.0-beta.6"
actix-rt = "2.2" actix-rt = "2.2"
actix-server = "2.0.0-beta.5" actix-server = "2.0.0-beta.5"

View File

@ -1,11 +1,20 @@
use crate::domain::handler::*; use crate::domain::handler::*;
use crate::infra::configuration::Configuration; use crate::infra::configuration::Configuration;
use actix_files::{Files, NamedFile};
use actix_http::HttpServiceBuilder; use actix_http::HttpServiceBuilder;
use actix_server::ServerBuilder; use actix_server::ServerBuilder;
use actix_service::map_config; use actix_service::map_config;
use actix_web::dev::AppConfig; use actix_web::{dev::AppConfig, web, App, HttpRequest};
use actix_web::App;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use std::path::PathBuf;
async fn index(req: HttpRequest) -> actix_web::Result<NamedFile> {
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<Backend>( pub fn build_tcp_server<Backend>(
config: &Configuration, config: &Configuration,
@ -18,7 +27,17 @@ where
server_builder server_builder
.bind("http", ("0.0.0.0", config.http_port), move || { .bind("http", ("0.0.0.0", config.http_port), move || {
HttpServiceBuilder::new() 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() .tcp()
}) })
.with_context(|| { .with_context(|| {