2019-12-15 08:07:36 -08:00
|
|
|
use actix_web::{
|
|
|
|
get,
|
|
|
|
http::header,
|
|
|
|
web::{Data, Query},
|
|
|
|
App, HttpResponse, HttpServer, Responder,
|
|
|
|
};
|
|
|
|
use handlebars::Handlebars;
|
2019-12-15 09:49:16 -08:00
|
|
|
use serde::Deserialize;
|
2019-12-15 08:07:36 -08:00
|
|
|
use std::collections::BTreeMap;
|
2019-12-15 09:49:16 -08:00
|
|
|
use std::collections::HashMap;
|
2019-12-15 08:07:36 -08:00
|
|
|
use std::fs::{File, OpenOptions};
|
|
|
|
use std::io::{Error, Write};
|
2019-12-15 09:49:16 -08:00
|
|
|
use std::sync::{Arc, RwLock};
|
2019-12-15 08:07:36 -08:00
|
|
|
|
2019-12-15 09:49:16 -08:00
|
|
|
static DEFAULT_CONFIG: &[u8] = br#"
|
2019-12-15 08:07:36 -08:00
|
|
|
bind_address: "127.0.0.1:8080"
|
2019-12-15 09:49:16 -08:00
|
|
|
public_address: "localhost"
|
|
|
|
|
2019-12-15 08:07:36 -08:00
|
|
|
routes:
|
|
|
|
g: "https://google.com/search?q={{query}}"
|
|
|
|
"#;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct SearchQuery {
|
|
|
|
to: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/ls")]
|
2019-12-15 09:49:16 -08:00
|
|
|
fn list(data: Data<Arc<State>>) -> impl Responder {
|
2019-12-15 08:07:36 -08:00
|
|
|
let mut resp = String::new();
|
2019-12-15 09:49:16 -08:00
|
|
|
for (k, v) in data.routes.iter() {
|
2019-12-15 08:07:36 -08:00
|
|
|
resp.push_str(&format!("{}: {}\n", k, v));
|
|
|
|
}
|
|
|
|
HttpResponse::Ok().body(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/hop")]
|
2019-12-15 09:49:16 -08:00
|
|
|
fn hop(data: Data<Arc<State>>, query: Query<SearchQuery>) -> impl Responder {
|
2019-12-15 08:07:36 -08:00
|
|
|
let reg = Handlebars::new();
|
|
|
|
let mut raw_args = query.to.split_ascii_whitespace();
|
|
|
|
let command = raw_args.next();
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
args.push_str(first_arg);
|
|
|
|
for arg in raw_args {
|
|
|
|
args.push_str("+");
|
|
|
|
args.push_str(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if command.is_none() {
|
|
|
|
return HttpResponse::NotFound().body("not found");
|
|
|
|
}
|
|
|
|
|
2019-12-15 09:49:16 -08:00
|
|
|
let mut template_args = HashMap::new();
|
|
|
|
template_args.insert("query", args);
|
2019-12-15 08:07:36 -08:00
|
|
|
|
2019-12-15 09:49:16 -08:00
|
|
|
match data.routes.get(command.unwrap()) {
|
2019-12-15 08:07:36 -08:00
|
|
|
Some(template) => HttpResponse::Found()
|
|
|
|
.header(
|
|
|
|
header::LOCATION,
|
2019-12-15 09:49:16 -08:00
|
|
|
reg.render_template(template, &template_args).unwrap(),
|
2019-12-15 08:07:36 -08:00
|
|
|
)
|
|
|
|
.finish(),
|
2019-12-15 09:49:16 -08:00
|
|
|
None => match &data.default_route {
|
|
|
|
Some(route) => {
|
|
|
|
template_args.insert(
|
|
|
|
"query",
|
|
|
|
format!(
|
|
|
|
"{}+{}",
|
|
|
|
command.unwrap(),
|
|
|
|
template_args.get("query").unwrap()
|
|
|
|
),
|
|
|
|
);
|
|
|
|
HttpResponse::Found()
|
|
|
|
.header(
|
|
|
|
header::LOCATION,
|
|
|
|
reg.render_template(data.routes.get(route).unwrap(), &template_args)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
None => HttpResponse::NotFound().body("not found"),
|
|
|
|
},
|
2019-12-15 08:07:36 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-15 09:49:16 -08:00
|
|
|
#[get("/")]
|
|
|
|
fn index(data: Data<Arc<State>>) -> impl Responder {
|
|
|
|
HttpResponse::Ok().body(data.renderer.read().unwrap().render("index", &()).unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/bunbunsearch.xml")]
|
|
|
|
fn opensearch(data: Data<Arc<State>>) -> impl Responder {
|
|
|
|
let mut template_args = HashMap::new();
|
|
|
|
template_args.insert("hostname", &data.public_address);
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.header(
|
|
|
|
header::CONTENT_TYPE,
|
|
|
|
"application/opensearchdescription+xml",
|
|
|
|
)
|
|
|
|
.body(
|
|
|
|
data.renderer
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.render("opensearch", &template_args)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-12-15 08:07:36 -08:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Config {
|
|
|
|
bind_address: String,
|
2019-12-15 09:49:16 -08:00
|
|
|
public_address: String,
|
|
|
|
default_route: Option<String>,
|
2019-12-15 08:07:36 -08:00
|
|
|
routes: BTreeMap<String, String>,
|
|
|
|
}
|
|
|
|
|
2019-12-15 09:49:16 -08:00
|
|
|
struct State {
|
|
|
|
public_address: String,
|
|
|
|
default_route: Option<String>,
|
|
|
|
routes: BTreeMap<String, String>,
|
|
|
|
renderer: RwLock<Handlebars>,
|
|
|
|
}
|
|
|
|
|
2019-12-15 08:07:36 -08:00
|
|
|
fn main() -> Result<(), Error> {
|
|
|
|
let config_file = match File::open("bunbun.toml") {
|
|
|
|
Ok(file) => file,
|
|
|
|
Err(_) => {
|
|
|
|
eprintln!("Unable to find a bunbun.toml file. Creating default!");
|
|
|
|
let mut fd = OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.create_new(true)
|
|
|
|
.open("bunbun.toml")
|
|
|
|
.expect("Unable to write to directory!");
|
|
|
|
fd.write_all(DEFAULT_CONFIG)?;
|
|
|
|
File::open("bunbun.toml")?
|
|
|
|
}
|
|
|
|
};
|
2019-12-15 09:49:16 -08:00
|
|
|
|
|
|
|
let renderer = compile_templates();
|
2019-12-15 08:07:36 -08:00
|
|
|
let conf: Config = serde_yaml::from_reader(config_file).unwrap();
|
2019-12-15 09:49:16 -08:00
|
|
|
let state = Arc::from(State {
|
|
|
|
public_address: conf.public_address,
|
|
|
|
default_route: conf.default_route,
|
|
|
|
routes: conf.routes,
|
|
|
|
renderer: RwLock::new(renderer),
|
|
|
|
});
|
|
|
|
|
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
|
|
|
.data(state.clone())
|
|
|
|
.service(hop)
|
|
|
|
.service(list)
|
|
|
|
.service(index)
|
|
|
|
.service(opensearch)
|
|
|
|
})
|
|
|
|
.bind(&conf.bind_address)?
|
|
|
|
.run()
|
|
|
|
}
|
2019-12-15 08:07:36 -08:00
|
|
|
|
2019-12-15 09:49:16 -08:00
|
|
|
fn compile_templates() -> Handlebars {
|
|
|
|
let mut handlebars = Handlebars::new();
|
|
|
|
handlebars
|
|
|
|
.register_template_string(
|
|
|
|
"index",
|
|
|
|
String::from_utf8_lossy(include_bytes!("templates/index.hbs")),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
handlebars
|
|
|
|
.register_template_string(
|
|
|
|
"opensearch",
|
|
|
|
String::from_utf8_lossy(include_bytes!("templates/bunbunsearch.xml")),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
handlebars
|
2019-12-15 08:07:36 -08:00
|
|
|
}
|