Added prefix url env variable option

This commit is contained in:
Maurice 2022-11-03 18:34:59 +01:00
parent b46c107a85
commit c375e0ccd7
3 changed files with 24 additions and 9 deletions

@ -12,6 +12,7 @@ For usage, see `about.html`
| -------- | ------- | | -------- | ------- |
| PORT | 8080 | | PORT | 8080 |
| DATA_DIR | ./data | | DATA_DIR | ./data |
| URL_PREFIX | "" |
## Setup ## Setup
Place the about.html file from this folder in the same folder as the binary Place the about.html file from this folder in the same folder as the binary

@ -11,6 +11,7 @@ start() {
--security-opt=no-new-privileges \ --security-opt=no-new-privileges \
-p 127.0.0.1:8080:8080 \ -p 127.0.0.1:8080:8080 \
-v pastabble-v:/app/data \ -v pastabble-v:/app/data \
-e PREFIX "https://paste.plabble.org" \
pastabble' pastabble'
} }

@ -34,6 +34,7 @@ fn main() {
// Get settings // Get settings
let port: u16 = env::var("PORT").unwrap_or(String::from("8080")).parse().expect("Failed to parse PORT variable"); 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 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 config = Config::new(data_dir);
let store = Store::new(config).expect("Failed to initialize database store"); 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/{id: String}) => { register_link(&links, req, Some(id), prefix.clone()) },
(POST) (/to) => { register_link(&links, req, None) }, (POST) (/to) => { register_link(&links, req, None, prefix.clone()) },
(GET) (/{id: String}) => { (GET) (/{id: String}) => {
// Get note from database, if exists // Get note from database, if exists
match pastes.get(&id).expect("Failed to access pastes DB") { match pastes.get(&id).expect("Failed to access pastes DB") {
@ -80,7 +81,7 @@ fn main() {
}, },
(POST) (/{id: String}) => { (POST) (/{id: String}) => {
let id = if id.is_empty() { None } else { Some(id) }; 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() _ => Response::empty_404()
) )
@ -108,7 +109,7 @@ fn main() {
} }
// Register paste handler // 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 // Try read body from form data, and if not present from request body
let body = match post_input!(req, { 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.set(&key, &paste).expect("Failed to save note");
pastes.flush().expect("Failed to save paste to database"); pastes.flush().expect("Failed to save paste to database");
// Return key // Return url with key
Response::text(key) let url = if prefix.is_some() {
format!("{}/{}", prefix.unwrap(), key)
} else {
key
};
Response::text(url)
} }
// Register link route handler // 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 // Try read body from form data, and if not present from request body
let link = match post_input!(req, { 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.set(&key, &link).expect("Failed to save link");
links.flush().expect("Failed to save link to database"); links.flush().expect("Failed to save link to database");
// Return key // Return url with key
Response::text(key) let url = if prefix.is_some() {
format!("{}/{}", prefix.unwrap(), key)
} else {
key
};
Response::text(url)
} }