initial work

This commit is contained in:
Edward Shen 2019-12-15 11:07:36 -05:00
commit 577870b12c
Signed by: edward
GPG key ID: F350507060ED6C90
5 changed files with 2039 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
**/*.rs.bk

1918
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

13
Cargo.toml Normal file
View file

@ -0,0 +1,13 @@
[package]
name = "bunbun"
version = "0.1.0"
authors = ["Edward Shen <code@eddie.sh>"]
edition = "2018"
description = "Re-implementation of bunny1 in Rust"
publish = false
[dependencies]
actix-web = "1.0"
serde = "1.0"
serde_yaml = "0.8"
handlebars = "2.0"

8
bunbun.toml Normal file
View file

@ -0,0 +1,8 @@
bind_address: "127.0.0.1:8080"
routes:
# Meta
ls: "/ls"
# Google
g: "https://google.com/search?q={{query}}"
yt: "https://www.youtube.com/results?search_query={{query}}"

98
src/main.rs Normal file
View file

@ -0,0 +1,98 @@
use actix_web::{
get,
http::header,
web::{Data, Query},
App, HttpResponse, HttpServer, Responder,
};
use handlebars::Handlebars;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fs::{File, OpenOptions};
use std::io::{Error, Write};
use std::sync::Arc;
static DEFAULT_CONFIG: &'static [u8] = br#"
bind_address: "127.0.0.1:8080"
routes:
g: "https://google.com/search?q={{query}}"
"#;
#[derive(Deserialize)]
struct SearchQuery {
to: String,
}
#[get("/ls")]
fn list(data: Data<Arc<BTreeMap<String, String>>>) -> impl Responder {
let mut resp = String::new();
for (k, v) in data.iter() {
resp.push_str(&format!("{}: {}\n", k, v));
}
HttpResponse::Ok().body(resp)
}
#[get("/hop")]
fn hop(data: Data<Arc<BTreeMap<String, String>>>, query: Query<SearchQuery>) -> impl Responder {
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");
}
// This struct is used until anonymous structs can be made
#[derive(Serialize)]
struct Filler {
query: String,
}
match data.get(command.unwrap()) {
Some(template) => HttpResponse::Found()
.header(
header::LOCATION,
reg.render_template(template, &Filler { query: args })
.unwrap(),
)
.finish(),
None => HttpResponse::NotFound().body("not found"),
}
}
#[derive(Deserialize)]
struct Config {
bind_address: String,
routes: BTreeMap<String, String>,
}
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")?
}
};
let conf: Config = serde_yaml::from_reader(config_file).unwrap();
let routes = Arc::from(conf.routes);
HttpServer::new(move || App::new().data(routes.clone()).service(hop).service(list))
.bind(&conf.bind_address)?
.run()
}