forked from Maurice/pastabble
Added prefix url env variable option
This commit is contained in:
parent
b46c107a85
commit
c375e0ccd7
@ -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
|
||||
|
@ -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'
|
||||
}
|
||||
|
||||
|
31
src/main.rs
31
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)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user