Pastabble frontend
This commit is contained in:
44
src/main.rs
44
src/main.rs
@@ -1,4 +1,4 @@
|
||||
use std::{env, fs, thread};
|
||||
use std::{env, fs::{self, File}, thread};
|
||||
|
||||
use chrono::Utc;
|
||||
use kv::{Config, Store, Json, Bucket, Value};
|
||||
@@ -30,10 +30,20 @@ fn generate_key<T : Value>(key: Option<String>, store: &Bucket<String, T>) -> St
|
||||
}
|
||||
}
|
||||
|
||||
fn get_query(req: &Request, name: &str) -> Option<String> {
|
||||
let params: Vec<&str> = req.raw_query_string().split('&').collect();
|
||||
let param = params.iter().find(|p|p.starts_with(&format!("{}=", name)));
|
||||
match param {
|
||||
Some(q) => q.split('=').last(),
|
||||
None => None,
|
||||
}.map(|d|d.to_string())
|
||||
}
|
||||
|
||||
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 source_dir: String = env::var("WWW_DIR").unwrap_or(String::from("./wwwroot"));
|
||||
let prefix: Option<String> = env::var("PREFIX").ok();
|
||||
|
||||
let config = Config::new(data_dir);
|
||||
@@ -47,12 +57,27 @@ fn main() {
|
||||
// Get links bucket
|
||||
let links = store.bucket::<String, String>(Some("links"))
|
||||
.expect("Failed to open links bucket");
|
||||
|
||||
|
||||
// Create server
|
||||
let server = Server::new(format!("0.0.0.0:{}", port), move |req| {
|
||||
if req.url().ends_with(".html") || req.url().ends_with(".css") || req.url().ends_with(".js") {
|
||||
let asset_response = rouille::match_assets(req, &source_dir);
|
||||
if asset_response.is_success() {
|
||||
return asset_response;
|
||||
}
|
||||
}
|
||||
|
||||
router!(req,
|
||||
(GET) (/) => {
|
||||
Response::html(&about)
|
||||
match File::open(format!("{}/index.html", &source_dir)) {
|
||||
Ok(file) => {
|
||||
Response::from_file("text/html", file)
|
||||
},
|
||||
Err(_) => {
|
||||
Response::html(&about)
|
||||
}
|
||||
}
|
||||
},
|
||||
(GET) (/to/{id: String}) => {
|
||||
match links.get(&id).expect("Failed to access links DB") {
|
||||
@@ -71,7 +96,13 @@ fn main() {
|
||||
// Get note from database, if exists
|
||||
match pastes.get(&id).expect("Failed to access pastes DB") {
|
||||
Some(paste) => {
|
||||
Response::text(paste.0.content)
|
||||
// If details, return JSON. Else, return text
|
||||
if get_query(req, "details").is_some() {
|
||||
Response::json(&paste.0)
|
||||
} else {
|
||||
Response::text(paste.0.content)
|
||||
.with_additional_header("Access-Control-Allow-Origin", "*")
|
||||
}
|
||||
},
|
||||
None => {
|
||||
Response::text(format!("Note with ID '{}' not found", &id))
|
||||
@@ -110,7 +141,7 @@ fn main() {
|
||||
|
||||
// Register paste handler
|
||||
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, {
|
||||
content: String
|
||||
@@ -123,11 +154,14 @@ fn register_paste(pastes: &Bucket<String, Json<Paste>>, req: &Request, id: Optio
|
||||
}
|
||||
};
|
||||
|
||||
// If lang param is present in query, extract it
|
||||
let language = get_query(req, "lang");
|
||||
|
||||
// Create and save new note
|
||||
let paste = Json(Paste {
|
||||
content: body,
|
||||
expires: None,
|
||||
language: None,
|
||||
language,
|
||||
created: Utc::now()
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user