use lto for release, better list

master
Edward Shen 2019-12-15 13:54:09 -05:00
parent 73712a1242
commit 3094454ad9
Signed by: edward
GPG Key ID: F350507060ED6C90
4 changed files with 51 additions and 19 deletions

View File

@ -11,3 +11,6 @@ actix-web = "1.0"
serde = "1.0"
serde_yaml = "0.8"
handlebars = "2.0"
[profile.release]
lto = true

View File

@ -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}}"
#

View File

@ -20,26 +20,33 @@ routes:
g: "https://google.com/search?q={{query}}"
"#;
static CONFIG_FILE: &str = "bunbun.toml";
#[get("/ls")]
fn list(data: Data<Arc<State>>) -> 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<Arc<State>>) -> 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<Arc<State>>, query: Query<SearchQuery>) -> 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<Arc<State>>, query: Query<SearchQuery>) -> 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<Arc<State>>, query: Query<SearchQuery>) -> 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<Arc<State>>, query: Query<SearchQuery>) -> 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
}

14
src/templates/list.hbs Normal file
View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<body>
<table>
<tr>
<th>Shortcut</th>
<th>Target</th>
</tr>
{{#each this}}
<tr><td>{{@key}}</td><td>{{this}}</td></tr>
{{/each}}
</table>
</body>
</html>