Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lldap_config.docker_template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
## The public URL of the server, for password reset links.
#http_url = "http://localhost"

## The path to the front-end assets (relative to the working directory).
#assets_path = "./app"

## Random secret for JWT signature.
## This secret should be random, and should be shared with application
## servers that need to consume the JWTs.
Expand Down
3 changes: 3 additions & 0 deletions server/src/infra/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use figment_file_provider_adapter::FileAdapter;
use lldap_auth::opaque::{server::ServerSetup, KeyPair};
use secstr::SecUtf8;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use url::Url;

#[derive(
Expand Down Expand Up @@ -125,6 +126,8 @@ pub struct Configuration {
// "***SECRET***".
#[builder(default)]
pub key_seed: Option<SecUtf8>,
#[builder(default = r#"PathBuf::from("./app")"#)]
pub assets_path: PathBuf,
#[builder(default)]
pub smtp_options: MailOptions,
#[builder(default)]
Expand Down
47 changes: 35 additions & 12 deletions server/src/infra/tcp_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
use hmac::Hmac;
use sha2::Sha512;
use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::RwLock;
use tracing::info;
use tracing::{info, warn};

async fn index<Backend>(data: web::Data<AppState<Backend>>) -> actix_web::Result<impl Responder> {
let mut file = std::fs::read_to_string(r"./app/index.html")?;
let mut file = std::fs::read_to_string(data.assets_path.join("index.html"))?;

Check warning on line 29 in server/src/infra/tcp_server.rs

View check run for this annotation

Codecov / codecov/patch

server/src/infra/tcp_server.rs#L29

Added line #L29 was not covered by tests

if data.server_url.path() != "/" {
file = file.replace(
Expand Down Expand Up @@ -80,7 +81,7 @@
async fn main_js_handler<Backend>(
data: web::Data<AppState<Backend>>,
) -> actix_web::Result<impl Responder> {
let mut file = std::fs::read_to_string(r"./app/static/main.js")?;
let mut file = std::fs::read_to_string(data.assets_path.join("static/main.js"))?;

Check warning on line 84 in server/src/infra/tcp_server.rs

View check run for this annotation

Codecov / codecov/patch

server/src/infra/tcp_server.rs#L84

Added line #L84 was not covered by tests

if data.server_url.path() != "/" {
file = file.replace("/pkg/", format!("{}/pkg/", data.server_url.path()).as_str());
Expand All @@ -91,13 +92,20 @@
.insert_header((header::CONTENT_TYPE, "text/javascript")))
}

async fn wasm_handler() -> actix_web::Result<impl Responder> {
Ok(actix_files::NamedFile::open_async("./app/pkg/lldap_app_bg.wasm").await?)
async fn wasm_handler<Backend>(
data: web::Data<AppState<Backend>>,
) -> actix_web::Result<impl Responder> {
Ok(
actix_files::NamedFile::open_async(data.assets_path.join("pkg/lldap_app_bg.wasm.gz"))
.await?,

Check warning on line 100 in server/src/infra/tcp_server.rs

View check run for this annotation

Codecov / codecov/patch

server/src/infra/tcp_server.rs#L95-L100

Added lines #L95 - L100 were not covered by tests
)
}

async fn wasm_handler_compressed() -> actix_web::Result<impl Responder> {
async fn wasm_handler_compressed<Backend>(
data: web::Data<AppState<Backend>>,
) -> actix_web::Result<impl Responder> {

Check warning on line 106 in server/src/infra/tcp_server.rs

View check run for this annotation

Codecov / codecov/patch

server/src/infra/tcp_server.rs#L104-L106

Added lines #L104 - L106 were not covered by tests
Ok(
actix_files::NamedFile::open_async("./app/pkg/lldap_app_bg.wasm.gz")
actix_files::NamedFile::open_async(data.assets_path.join("pkg/lldap_app_bg.wasm.gz"))

Check warning on line 108 in server/src/infra/tcp_server.rs

View check run for this annotation

Codecov / codecov/patch

server/src/infra/tcp_server.rs#L108

Added line #L108 was not covered by tests
.await?
.customize()
.insert_header(header::ContentEncoding::Gzip)
Expand All @@ -111,6 +119,7 @@
jwt_secret: secstr::SecUtf8,
jwt_blacklist: HashSet<u64>,
server_url: url::Url,
assets_path: PathBuf,
mail_options: MailOptions,
) where
Backend: TcpBackendHandler + BackendHandler + LoginHandler + OpaqueHandler + Clone + 'static,
Expand All @@ -121,6 +130,7 @@
jwt_key: hmac::Mac::new_from_slice(jwt_secret.unsecure().as_bytes()).unwrap(),
jwt_blacklist: RwLock::new(jwt_blacklist),
server_url,
assets_path: assets_path.clone(),
mail_options,
}))
.route(
Expand All @@ -138,16 +148,22 @@
.configure(super::graphql::api::configure_endpoint::<Backend>),
)
.service(
web::resource("/pkg/lldap_app_bg.wasm.gz").route(web::route().to(wasm_handler_compressed)),
web::resource("/pkg/lldap_app_bg.wasm.gz")
.route(web::route().to(wasm_handler_compressed::<Backend>)),
)
.service(
web::resource("/pkg/lldap_app_bg.wasm").route(web::route().to(wasm_handler::<Backend>)),
)
.service(web::resource("/pkg/lldap_app_bg.wasm").route(web::route().to(wasm_handler)))
.service(web::resource("/static/main.js").route(web::route().to(main_js_handler::<Backend>)))
// Serve the /pkg path with the compiled WASM app.
.service(Files::new("/pkg", "./app/pkg"))
.service(Files::new("/pkg", assets_path.join("pkg")))
// Serve static files
.service(Files::new("/static", "./app/static"))
.service(Files::new("/static", assets_path.join("static")))
// Serve static fonts
.service(Files::new("/static/fonts", "./app/static/fonts"))
.service(Files::new(
"/static/fonts",
assets_path.join("static/fonts"),
))
// Default to serve index.html for unknown routes, to support routing.
.default_service(web::route().guard(guard::Get()).to(index::<Backend>));
}
Expand All @@ -157,6 +173,7 @@
pub jwt_key: Hmac<Sha512>,
pub jwt_blacklist: RwLock<HashSet<u64>>,
pub server_url: url::Url,
pub assets_path: PathBuf,
pub mail_options: MailOptions,
}

Expand Down Expand Up @@ -195,8 +212,12 @@
.await
.context("while getting the jwt blacklist")?;
let server_url = config.http_url.0.clone();
let assets_path = config.assets_path.clone();
let mail_options = config.smtp_options.clone();
let verbose = config.verbose;
if !assets_path.join("index.html").exists() {
warn!("Cannot find {}, please ensure that assets_path is set correctly and that the front-end files exist.", assets_path.to_string_lossy())

Check warning on line 219 in server/src/infra/tcp_server.rs

View check run for this annotation

Codecov / codecov/patch

server/src/infra/tcp_server.rs#L219

Added line #L219 was not covered by tests
}
info!("Starting the API/web server on port {}", config.http_port);
server_builder
.bind(
Expand All @@ -207,6 +228,7 @@
let jwt_secret = jwt_secret.clone();
let jwt_blacklist = jwt_blacklist.clone();
let server_url = server_url.clone();
let assets_path = assets_path.clone();
let mail_options = mail_options.clone();
HttpServiceBuilder::default()
.finish(map_config(
Expand All @@ -222,6 +244,7 @@
jwt_secret,
jwt_blacklist,
server_url,
assets_path,
mail_options,
)
}),
Expand Down
Loading