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:
Gunwant Jain
2021-01-06 06:13:03 +05:30
parent ae8bd544de
commit c2ad9ffd90

View File

@@ -7,9 +7,12 @@ use std::env;
use std::io::prelude::*; use std::io::prelude::*;
use std::collections::HashMap; use std::collections::HashMap;
use std::path::Path; use std::path::Path;
use std::fs;
use std::fs::File; use std::fs::File;
use rocket_contrib::templates::Template; use rocket_contrib::templates::Template;
use rocket::request::Form;
use rocket::response::Redirect;
use rocket::Data; use rocket::Data;
mod paste_id; mod paste_id;
@@ -57,16 +60,33 @@ fn upload(paste: Data) -> Result<String, std::io::Error> {
paste.stream_to_file(filepath)?; paste.stream_to_file(filepath)?;
let url = match tree_magic::from_filepath(filepath).as_str().contains("text") { 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), .unwrap_or("<no_host_provided>".to_string()), id = id),
false => format!("https://{host}/{id}\n", host = env::var("HOST") false => format!("https://{host}/{id}\n", host = env::var("HOST_URL")
.unwrap_or("<no_host_provided>".to_string()), id = id) .unwrap_or("http://localhost:8000".to_string()), id = id)
}; };
Ok(url) 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("/")] #[get("/")]
fn index() -> Option<Template> { fn index() -> Option<Template> {
let mut map = HashMap::new(); let mut map = HashMap::new();
@@ -76,7 +96,7 @@ fn index() -> Option<Template> {
fn main() { fn main() {
rocket::ignite() rocket::ignite()
.mount("/", routes![index, upload, retrieve, pretty_retrieve]) .mount("/", routes![index, upload, submit, retrieve, pretty_retrieve])
.attach(Template::fairing()) .attach(Template::fairing())
.launch(); .launch();
} }