initial work
This commit is contained in:
commit
577870b12c
5 changed files with 2039 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
**/*.rs.bk
|
1918
Cargo.lock
generated
Normal file
1918
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
13
Cargo.toml
Normal file
13
Cargo.toml
Normal 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
8
bunbun.toml
Normal 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
98
src/main.rs
Normal 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()
|
||||
}
|
Loading…
Reference in a new issue