Compare commits
6 commits
606620ae6a
...
996924582a
Author | SHA1 | Date | |
---|---|---|---|
996924582a | |||
320e41c6f2 | |||
e11e514c12 | |||
be505480c5 | |||
64aec75659 | |||
c195efd1b4 |
2 changed files with 196 additions and 145 deletions
2
rustfmt.toml
Normal file
2
rustfmt.toml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
tab_spaces = 2
|
||||||
|
use_field_init_shorthand = true
|
127
src/main.rs
127
src/main.rs
|
@ -8,14 +8,49 @@ use handlebars::Handlebars;
|
||||||
use hotwatch::{Event, Hotwatch};
|
use hotwatch::{Event, Hotwatch};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::collections::{BTreeMap, HashMap};
|
use std::collections::{BTreeMap, HashMap};
|
||||||
|
use std::error::Error;
|
||||||
|
use std::fmt;
|
||||||
use std::fs::{File, OpenOptions};
|
use std::fs::{File, OpenOptions};
|
||||||
use std::io::{Error, Write};
|
use std::io::Write;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
static DEFAULT_CONFIG: &[u8] = include_bytes!("../bunbun.default.toml");
|
static DEFAULT_CONFIG: &[u8] = include_bytes!("../bunbun.default.toml");
|
||||||
static CONFIG_FILE: &str = "bunbun.toml";
|
static CONFIG_FILE: &str = "bunbun.toml";
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum BunBunError {
|
||||||
|
IoError(std::io::Error),
|
||||||
|
ParseError(serde_yaml::Error),
|
||||||
|
WatchError(hotwatch::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for BunBunError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
BunBunError::IoError(e) => e.fmt(f),
|
||||||
|
BunBunError::ParseError(e) => e.fmt(f),
|
||||||
|
BunBunError::WatchError(e) => e.fmt(f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error for BunBunError {}
|
||||||
|
|
||||||
|
macro_rules! from_error {
|
||||||
|
($from:ty, $to:ident) => {
|
||||||
|
impl From<$from> for BunBunError {
|
||||||
|
fn from(e: $from) -> Self {
|
||||||
|
BunBunError::$to(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
from_error!(std::io::Error, IoError);
|
||||||
|
from_error!(serde_yaml::Error, ParseError);
|
||||||
|
from_error!(hotwatch::Error, WatchError);
|
||||||
|
|
||||||
#[get("/ls")]
|
#[get("/ls")]
|
||||||
fn list(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
fn list(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
||||||
let data = data.read().unwrap();
|
let data = data.read().unwrap();
|
||||||
|
@ -54,7 +89,8 @@ fn hop(data: Data<Arc<RwLock<State>>>, query: Query<SearchQuery>) -> impl Respon
|
||||||
Some(template) => HttpResponse::Found()
|
Some(template) => HttpResponse::Found()
|
||||||
.header(
|
.header(
|
||||||
header::LOCATION,
|
header::LOCATION,
|
||||||
data.renderer
|
data
|
||||||
|
.renderer
|
||||||
.render_template(template, &template_args)
|
.render_template(template, &template_args)
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
)
|
)
|
||||||
|
@ -72,7 +108,8 @@ fn hop(data: Data<Arc<RwLock<State>>>, query: Query<SearchQuery>) -> impl Respon
|
||||||
HttpResponse::Found()
|
HttpResponse::Found()
|
||||||
.header(
|
.header(
|
||||||
header::LOCATION,
|
header::LOCATION,
|
||||||
data.renderer
|
data
|
||||||
|
.renderer
|
||||||
.render_template(data.routes.get(route).unwrap(), &template_args)
|
.render_template(data.routes.get(route).unwrap(), &template_args)
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
)
|
)
|
||||||
|
@ -119,47 +156,32 @@ struct State {
|
||||||
renderer: Handlebars,
|
renderer: Handlebars,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Error> {
|
fn main() -> Result<(), BunBunError> {
|
||||||
let config_file = match File::open(CONFIG_FILE) {
|
let conf = read_config(CONFIG_FILE)?;
|
||||||
Ok(file) => file,
|
|
||||||
Err(_) => {
|
|
||||||
eprintln!("Unable to find a {} file. Creating default!", CONFIG_FILE);
|
|
||||||
let mut fd = OpenOptions::new()
|
|
||||||
.write(true)
|
|
||||||
.create_new(true)
|
|
||||||
.open(CONFIG_FILE)
|
|
||||||
.expect("Unable to write to directory!");
|
|
||||||
fd.write_all(DEFAULT_CONFIG)?;
|
|
||||||
File::open(CONFIG_FILE)?
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let renderer = compile_templates();
|
let renderer = compile_templates();
|
||||||
let conf: Config = serde_yaml::from_reader(config_file).unwrap();
|
|
||||||
let state = Arc::from(RwLock::new(State {
|
let state = Arc::from(RwLock::new(State {
|
||||||
public_address: conf.public_address,
|
public_address: conf.public_address,
|
||||||
default_route: conf.default_route,
|
default_route: conf.default_route,
|
||||||
routes: conf.routes,
|
routes: conf.routes,
|
||||||
renderer: renderer,
|
renderer,
|
||||||
}));
|
}));
|
||||||
let state_ref = state.clone();
|
let state_ref = state.clone();
|
||||||
|
|
||||||
let mut watch =
|
let mut watch = Hotwatch::new_with_custom_delay(Duration::from_millis(500))?;
|
||||||
Hotwatch::new_with_custom_delay(Duration::from_millis(500)).expect("Failed to init watch");
|
|
||||||
|
|
||||||
watch
|
watch.watch(CONFIG_FILE, move |e: Event| {
|
||||||
.watch(CONFIG_FILE, move |e: Event| {
|
|
||||||
if let Event::Write(_) = e {
|
if let Event::Write(_) = e {
|
||||||
let config_file = File::open(CONFIG_FILE).unwrap();
|
|
||||||
let conf: Config = serde_yaml::from_reader(config_file).unwrap();
|
|
||||||
let state = state.clone();
|
|
||||||
let mut state = state.write().unwrap();
|
let mut state = state.write().unwrap();
|
||||||
|
match read_config(CONFIG_FILE) {
|
||||||
|
Ok(conf) => {
|
||||||
state.public_address = conf.public_address;
|
state.public_address = conf.public_address;
|
||||||
state.default_route = conf.default_route;
|
state.default_route = conf.default_route;
|
||||||
state.routes = conf.routes;
|
state.routes = conf.routes;
|
||||||
}
|
}
|
||||||
})
|
Err(e) => eprintln!("Config is malformed: {}", e),
|
||||||
.expect("failed to watch");
|
}
|
||||||
|
}
|
||||||
|
})?;
|
||||||
|
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
|
@ -170,27 +192,54 @@ fn main() -> Result<(), Error> {
|
||||||
.service(opensearch)
|
.service(opensearch)
|
||||||
})
|
})
|
||||||
.bind(&conf.bind_address)?
|
.bind(&conf.bind_address)?
|
||||||
.run()
|
.run()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_config(config_file_path: &str) -> Result<Config, BunBunError> {
|
||||||
|
let config_file = match File::open(config_file_path) {
|
||||||
|
Ok(file) => file,
|
||||||
|
Err(_) => {
|
||||||
|
eprintln!(
|
||||||
|
"Unable to find a {} file. Creating default!",
|
||||||
|
config_file_path
|
||||||
|
);
|
||||||
|
let mut fd = OpenOptions::new()
|
||||||
|
.write(true)
|
||||||
|
.create_new(true)
|
||||||
|
.open(config_file_path)
|
||||||
|
.expect("Unable to write to directory!");
|
||||||
|
fd.write_all(DEFAULT_CONFIG)?;
|
||||||
|
File::open(config_file_path)?
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(serde_yaml::from_reader(config_file)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compile_templates() -> Handlebars {
|
fn compile_templates() -> Handlebars {
|
||||||
let mut handlebars = Handlebars::new();
|
let mut handlebars = Handlebars::new();
|
||||||
|
|
||||||
|
macro_rules! register_template {
|
||||||
|
( $( $template:expr ),* ) => {
|
||||||
|
$(
|
||||||
handlebars
|
handlebars
|
||||||
.register_template_string(
|
.register_template_string(
|
||||||
"index",
|
$template,
|
||||||
String::from_utf8_lossy(include_bytes!("templates/index.hbs")),
|
String::from_utf8_lossy(
|
||||||
|
include_bytes!(concat!("templates/", $template, ".hbs")))
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
)*
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
register_template!("index", "list");
|
||||||
|
|
||||||
handlebars
|
handlebars
|
||||||
.register_template_string(
|
.register_template_string(
|
||||||
"opensearch",
|
"opensearch",
|
||||||
String::from_utf8_lossy(include_bytes!("templates/bunbunsearch.xml")),
|
String::from_utf8_lossy(include_bytes!(concat!("templates/", "bunbunsearch.xml"))),
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
handlebars
|
|
||||||
.register_template_string(
|
|
||||||
"list",
|
|
||||||
String::from_utf8_lossy(include_bytes!("templates/list.hbs")),
|
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
handlebars
|
handlebars
|
||||||
|
|
Loading…
Reference in a new issue