main.rs: add /submit route
Getting into frontend biz. Also s/HOST/HOST_URL/ because I didn't realise that HOST is already a taken variable. Signed-off-by: Gunwant Jain <mail@wantguns.dev>
This commit is contained in:
28
src/main.rs
28
src/main.rs
@@ -7,9 +7,12 @@ use std::env;
|
||||
use std::io::prelude::*;
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
|
||||
use rocket_contrib::templates::Template;
|
||||
use rocket::request::Form;
|
||||
use rocket::response::Redirect;
|
||||
use rocket::Data;
|
||||
|
||||
mod paste_id;
|
||||
@@ -57,16 +60,33 @@ fn upload(paste: Data) -> Result<String, std::io::Error> {
|
||||
paste.stream_to_file(filepath)?;
|
||||
|
||||
let url = match tree_magic::from_filepath(filepath).as_str().contains("text") {
|
||||
true => format!("https://{host}/p/{id}\n", host = env::var("HOST")
|
||||
true => format!("https://{host}/p/{id}\n", host = env::var("HOST_URL")
|
||||
.unwrap_or("<no_host_provided>".to_string()), id = id),
|
||||
|
||||
false => format!("https://{host}/{id}\n", host = env::var("HOST")
|
||||
.unwrap_or("<no_host_provided>".to_string()), id = id)
|
||||
false => format!("https://{host}/{id}\n", host = env::var("HOST_URL")
|
||||
.unwrap_or("http://localhost:8000".to_string()), id = id)
|
||||
};
|
||||
|
||||
Ok(url)
|
||||
}
|
||||
|
||||
#[derive(FromForm)]
|
||||
struct PasteIdForm {
|
||||
val: String,
|
||||
}
|
||||
|
||||
#[post("/submit", data = "<paste>")]
|
||||
fn submit(paste: Form<PasteIdForm>) -> Redirect {
|
||||
let id = PasteId::new(4);
|
||||
|
||||
let filename = format!("upload/{id}", id = id);
|
||||
let content = paste.into_inner().val;
|
||||
|
||||
fs::write(&filename, content).expect("Unable to write to the file");
|
||||
|
||||
Redirect::to(format!("/p/{id}", id = id))
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
fn index() -> Option<Template> {
|
||||
let mut map = HashMap::new();
|
||||
@@ -76,7 +96,7 @@ fn index() -> Option<Template> {
|
||||
|
||||
fn main() {
|
||||
rocket::ignite()
|
||||
.mount("/", routes![index, upload, retrieve, pretty_retrieve])
|
||||
.mount("/", routes![index, upload, submit, retrieve, pretty_retrieve])
|
||||
.attach(Template::fairing())
|
||||
.launch();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user