From bdf4bdd8fcf364cee197720a4c00ff4f044bba34 Mon Sep 17 00:00:00 2001 From: holly sparkles Date: Tue, 10 Oct 2023 20:28:39 +0200 Subject: [PATCH] feat: add ability to customise welcome response by passing a filename --- README.md | 3 ++- src/config.rs | 1 + src/main.rs | 32 ++++++++++++++++++++------------ 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index e1a1e98..07ed65a 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,10 @@ export LICENSE_API_BASE_URL="/" export LICENSE_API_DATABASE="/path_to/your/license_database.yml" export LICENSE_API_SERVER_IP="0.0.0.0" export LICENSE_API_SERVER_PORT="8080" +export LICENSE_API_WELCOME_MESSAGE="/path/to/welcome.txt" ``` -If `LICENSE_API_DATABASE` is not set, the server will refuse to start. +All environment variables except for `LICENSE_API_DATABASE` are optional. If `LICENSE_API_DATABASE` is not set, the server will refuse to start. ## License diff --git a/src/config.rs b/src/config.rs index b2f0909..cf17cc7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -14,6 +14,7 @@ pub struct ServerConfig { pub server_ip: Ipv4Addr, // TODO: IPv6 support #[serde(default = "ServerConfig::default_server_port")] pub server_port: u16, + pub welcome_message: Option } impl ServerConfig { diff --git a/src/main.rs b/src/main.rs index 8f99c2a..a22da75 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,22 +80,30 @@ fn build_response(status: StatusCode, data: String) -> Response { } async fn get_welcome_message(req: tide::Request) -> tide::Result { - // TODO: make this better - let base_url = req.state().config.base_url.as_str(); + match &req.state().config.welcome_message { + Some(file) => match async_std::fs::read_to_string(file).await { + Ok(data) => return Ok(build_response(StatusCode::Ok, data)), + Err(_) => { + log::error!("Unable to read welcome message file."); + return Ok(build_response(StatusCode::Ok, build_welcome_message(req))); + } + }, + None => return Ok(build_response(StatusCode::Ok, build_welcome_message(req))), + } +} + +fn build_welcome_message(req: tide::Request) -> String { + // TODO: make this better + let base_url = req.state().config.base_url.clone(); let header = String::from("Available endpoints:"); let base = format!("{}", base_url); let list = format!("{}list", base_url); let all = format!("{}list/full", base_url); let license = format!("{}", base_url); - - Ok(build_response( - StatusCode::Ok, - format!( - "{}\n {}\n {}\n {}\n {}", - header, base, list, all, license - ) - .to_string(), - )) + format!( + "{}\n {}\n {}\n {}\n {}", + header, base, list, all, license + ) } async fn get_fallback_response(req: tide::Request) -> tide::Result { @@ -128,7 +136,7 @@ async fn get_requested_license(req: tide::Request) -> tide::Resul // TODO: make this better let request = req.url().path().strip_prefix("/").unwrap_or(""); - // TODO: break this up + // TODO: break this up let response = match req.state().database.get_license(request) { Some(license) => { // is there a better way to do this? -- libgit2 1.7.2