From c375e0ccd75dd32b73d7060bf9e784438010f8bb Mon Sep 17 00:00:00 2001 From: Maurice <mauricegolverdingen@gmail.com> Date: Thu, 3 Nov 2022 18:34:59 +0100 Subject: [PATCH] Added prefix url env variable option --- README.md | 1 + config/openrc.sh | 1 + src/main.rs | 31 ++++++++++++++++++++++--------- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 387fcfc..ca42686 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ For usage, see `about.html` | -------- | ------- | | PORT | 8080 | | DATA_DIR | ./data | +| URL_PREFIX | "" | ## Setup Place the about.html file from this folder in the same folder as the binary diff --git a/config/openrc.sh b/config/openrc.sh index 1cb557d..cad7ce1 100644 --- a/config/openrc.sh +++ b/config/openrc.sh @@ -11,6 +11,7 @@ start() { --security-opt=no-new-privileges \ -p 127.0.0.1:8080:8080 \ -v pastabble-v:/app/data \ + -e PREFIX "https://paste.plabble.org" \ pastabble' } diff --git a/src/main.rs b/src/main.rs index 581b101..1905d8f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,6 +34,7 @@ fn main() { // Get settings let port: u16 = env::var("PORT").unwrap_or(String::from("8080")).parse().expect("Failed to parse PORT variable"); let data_dir: String = env::var("DATA_DIR").unwrap_or(String::from("./data")); + let prefix: Option<String> = env::var("PREFIX").ok(); let config = Config::new(data_dir); let store = Store::new(config).expect("Failed to initialize database store"); @@ -64,8 +65,8 @@ fn main() { } } }, - (POST) (/to/{id: String}) => { register_link(&links, req, Some(id)) }, - (POST) (/to) => { register_link(&links, req, None) }, + (POST) (/to/{id: String}) => { register_link(&links, req, Some(id), prefix.clone()) }, + (POST) (/to) => { register_link(&links, req, None, prefix.clone()) }, (GET) (/{id: String}) => { // Get note from database, if exists match pastes.get(&id).expect("Failed to access pastes DB") { @@ -80,7 +81,7 @@ fn main() { }, (POST) (/{id: String}) => { let id = if id.is_empty() { None } else { Some(id) }; - register_paste(&pastes, req, id) + register_paste(&pastes, req, id, prefix.clone()) }, _ => Response::empty_404() ) @@ -108,7 +109,7 @@ fn main() { } // Register paste handler -fn register_paste(pastes: &Bucket<String, Json<Paste>>, req: &Request, id: Option<String>) -> Response { +fn register_paste(pastes: &Bucket<String, Json<Paste>>, req: &Request, id: Option<String>, prefix: Option<String>) -> Response { // Try read body from form data, and if not present from request body let body = match post_input!(req, { @@ -134,12 +135,18 @@ fn register_paste(pastes: &Bucket<String, Json<Paste>>, req: &Request, id: Optio pastes.set(&key, &paste).expect("Failed to save note"); pastes.flush().expect("Failed to save paste to database"); - // Return key - Response::text(key) + // Return url with key + let url = if prefix.is_some() { + format!("{}/{}", prefix.unwrap(), key) + } else { + key + }; + + Response::text(url) } // Register link route handler -fn register_link(links: &Bucket<String,String>, req: &Request, id: Option<String>) -> Response { +fn register_link(links: &Bucket<String,String>, req: &Request, id: Option<String>, prefix: Option<String>) -> Response { // Try read body from form data, and if not present from request body let link = match post_input!(req, { @@ -157,6 +164,12 @@ fn register_link(links: &Bucket<String,String>, req: &Request, id: Option<String links.set(&key, &link).expect("Failed to save link"); links.flush().expect("Failed to save link to database"); - // Return key - Response::text(key) + // Return url with key + let url = if prefix.is_some() { + format!("{}/{}", prefix.unwrap(), key) + } else { + key + }; + + Response::text(url) } \ No newline at end of file