From 3094454ad9b3931f296ba4aa2cdaf0339721b219 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Sun, 15 Dec 2019 13:54:09 -0500 Subject: [PATCH] use lto for release, better list --- Cargo.toml | 3 +++ bunbun.toml | 1 - src/main.rs | 52 +++++++++++++++++++++++++++--------------- src/templates/list.hbs | 14 ++++++++++++ 4 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 src/templates/list.hbs diff --git a/Cargo.toml b/Cargo.toml index 5d09974..7d10351 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,6 @@ actix-web = "1.0" serde = "1.0" serde_yaml = "0.8" handlebars = "2.0" + +[profile.release] +lto = true diff --git a/bunbun.toml b/bunbun.toml index 779c717..ea7cf8e 100644 --- a/bunbun.toml +++ b/bunbun.toml @@ -13,4 +13,3 @@ routes: yt: "https://www.youtube.com/results?search_query={{query}}" # Searches na: "https://nyaa.si/?f=0&c=1_2&q={{query}}" - # diff --git a/src/main.rs b/src/main.rs index 4639162..ffedd1d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,26 +20,33 @@ routes: g: "https://google.com/search?q={{query}}" "#; +static CONFIG_FILE: &str = "bunbun.toml"; + +#[get("/ls")] +fn list(data: Data>) -> impl Responder { + HttpResponse::Ok().body( + data.renderer + .read() + .unwrap() + .render("list", &data.routes) + .unwrap(), + ) +} + #[derive(Deserialize)] struct SearchQuery { to: String, } -#[get("/ls")] -fn list(data: Data>) -> impl Responder { - let mut resp = String::new(); - for (k, v) in data.routes.iter() { - resp.push_str(&format!("{}: {}\n", k, v)); - } - HttpResponse::Ok().body(resp) -} - #[get("/hop")] fn hop(data: Data>, query: Query) -> impl Responder { - let reg = Handlebars::new(); let mut raw_args = query.to.split_ascii_whitespace(); let command = raw_args.next(); + if command.is_none() { + return HttpResponse::NotFound().body("not found"); + } + // Reform args into url-safe string (probably want to go thru an actual parser) let mut args = String::new(); if let Some(first_arg) = raw_args.next() { @@ -50,10 +57,6 @@ fn hop(data: Data>, query: Query) -> impl Responder { } } - if command.is_none() { - return HttpResponse::NotFound().body("not found"); - } - let mut template_args = HashMap::new(); template_args.insert("query", args); @@ -61,7 +64,11 @@ fn hop(data: Data>, query: Query) -> impl Responder { Some(template) => HttpResponse::Found() .header( header::LOCATION, - reg.render_template(template, &template_args).unwrap(), + data.renderer + .read() + .unwrap() + .render_template(template, &template_args) + .unwrap(), ) .finish(), None => match &data.default_route { @@ -77,7 +84,10 @@ fn hop(data: Data>, query: Query) -> impl Responder { HttpResponse::Found() .header( header::LOCATION, - reg.render_template(data.routes.get(route).unwrap(), &template_args) + data.renderer + .read() + .unwrap() + .render_template(data.routes.get(route).unwrap(), &template_args) .unwrap(), ) .finish() @@ -126,7 +136,7 @@ struct State { } fn main() -> Result<(), Error> { - let config_file = match File::open("bunbun.toml") { + let config_file = match File::open(CONFIG_FILE) { Ok(file) => file, Err(_) => { eprintln!("Unable to find a bunbun.toml file. Creating default!"); @@ -136,7 +146,7 @@ fn main() -> Result<(), Error> { .open("bunbun.toml") .expect("Unable to write to directory!"); fd.write_all(DEFAULT_CONFIG)?; - File::open("bunbun.toml")? + File::open(CONFIG_FILE)? } }; @@ -176,4 +186,10 @@ fn compile_templates() -> Handlebars { ) .unwrap(); handlebars + .register_template_string( + "list", + String::from_utf8_lossy(include_bytes!("templates/list.hbs")), + ) + .unwrap(); + handlebars } diff --git a/src/templates/list.hbs b/src/templates/list.hbs new file mode 100644 index 0000000..7ae871f --- /dev/null +++ b/src/templates/list.hbs @@ -0,0 +1,14 @@ + + + + + + + + + {{#each this}} + + {{/each}} +
ShortcutTarget
{{@key}}{{this}}
+ +