update config file format

This commit is contained in:
Edward Shen 2019-12-24 00:05:56 -05:00
parent 9f4577f0ed
commit e1d70e2c4f
Signed by: edward
GPG key ID: F350507060ED6C90
5 changed files with 77 additions and 28 deletions

View file

@ -6,19 +6,30 @@ bind_address: "127.0.0.1:8080"
public_address: "localhost:8080" public_address: "localhost:8080"
# A default route, if no route is was matched. If none were matched, the entire # A default route, if no route is was matched. If none were matched, the entire
# query is used as the query for the default route. This field is optional. # query is used as the query for the default route. This field is optional, but
# highly recommended for ease-of-use.
default_route: "g" default_route: "g"
routes: # A list containing route groups. Each route group must have a name and a
# mapping of routes, with an optional description field. Each route mapping may
# contain "{{query}}", which will be populated by the user's search query. This
# input is percent-escaped. If multiple routes are defined, then the later
# defined route is used.
groups: groups:
meta: -
name: "Meta commands"
# Meta description: "Commands for bunbun"
ls: "/ls" routes:
help: "/ls" ls: &ls "/ls"
list: "/ls" help: *ls
# Google list: *ls
-
name: "Google"
routes:
g: "https://google.com/search?q={{query}}" g: "https://google.com/search?q={{query}}"
yt: "https://www.youtube.com/results?search_query={{query}}" yt: "https://www.youtube.com/results?search_query={{query}}"
-
name: "Uncategorized routes"
description: "One-off routes with no specific grouping"
routes:
r: "https://reddit.com/r/{{query}}" r: "https://reddit.com/r/{{query}}"

View file

@ -21,5 +21,5 @@ args:
- config: - config:
short: "c" short: "c"
long: "config" long: "config"
default_value: "/etc/bunbun.toml" default_value: "/etc/bunbun.yaml"
help: Specify the location of the config file to read from. Needs read/write permissions. help: Specify the location of the config file to read from. Needs read/write permissions.

View file

@ -5,7 +5,7 @@ use handlebars::Handlebars;
use hotwatch::{Event, Hotwatch}; use hotwatch::{Event, Hotwatch};
use libc::daemon; use libc::daemon;
use log::{debug, error, info, trace, warn}; use log::{debug, error, info, trace, warn};
use serde::Deserialize; use serde::{Deserialize, Serialize};
use std::cmp::min; use std::cmp::min;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
@ -61,6 +61,8 @@ from_error!(log::SetLoggerError, LoggerInitError);
pub struct State { pub struct State {
public_address: String, public_address: String,
default_route: Option<String>, default_route: Option<String>,
groups: Vec<RouteGroup>,
/// Cached, flattened mapping of all routes and their destinations.
routes: HashMap<String, String>, routes: HashMap<String, String>,
renderer: Handlebars, renderer: Handlebars,
} }
@ -84,7 +86,8 @@ fn main() -> Result<(), BunBunError> {
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: cache_routes(&conf.groups),
groups: conf.groups,
renderer, renderer,
})); }));
@ -111,7 +114,8 @@ fn main() -> Result<(), BunBunError> {
Ok(conf) => { 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 = cache_routes(&conf.groups);
state.groups = conf.groups;
info!("Successfully updated active state"); info!("Successfully updated active state");
} }
Err(e) => warn!("Failed to update config file: {}", e), Err(e) => warn!("Failed to update config file: {}", e),
@ -144,6 +148,9 @@ fn main() -> Result<(), BunBunError> {
Ok(()) Ok(())
} }
/// Initializes the logger based on the number of quiet and verbose flags passed
/// in. Usually, these values are mutually exclusive, that is, if the number of
/// verbose flags is non-zero then the quiet flag is zero, and vice versa.
fn init_logger( fn init_logger(
num_verbose_flags: u64, num_verbose_flags: u64,
num_quiet_flags: u64, num_quiet_flags: u64,
@ -171,6 +178,13 @@ struct Config {
bind_address: String, bind_address: String,
public_address: String, public_address: String,
default_route: Option<String>, default_route: Option<String>,
groups: Vec<RouteGroup>,
}
#[derive(Deserialize, Serialize)]
struct RouteGroup {
name: String,
description: Option<String>,
routes: HashMap<String, String>, routes: HashMap<String, String>,
} }
@ -210,6 +224,21 @@ fn read_config(config_file_path: &str) -> Result<Config, BunBunError> {
Ok(serde_yaml::from_str(&config_str)?) Ok(serde_yaml::from_str(&config_str)?)
} }
fn cache_routes(groups: &[RouteGroup]) -> HashMap<String, String> {
let mut mapping = HashMap::new();
for group in groups {
for (kw, dest) in &group.routes {
match mapping.insert(kw.clone(), dest.clone()) {
None => trace!("Inserting {} into mapping.", kw),
Some(old_value) => {
debug!("Overriding {} route from {} to {}.", kw, old_value, dest)
}
}
}
}
mapping
}
/// Returns an instance with all pre-generated templates included into the /// Returns an instance with all pre-generated templates included into the
/// binary. This allows for users to have a portable binary without needed the /// binary. This allows for users to have a portable binary without needed the
/// templates at runtime. /// templates at runtime.

View file

@ -23,7 +23,7 @@ const FRAGMENT_ENCODE_SET: &AsciiSet = &CONTROLS
#[get("/ls")] #[get("/ls")]
pub fn list(data: Data<Arc<RwLock<State>>>) -> impl Responder { pub fn list(data: Data<Arc<RwLock<State>>>) -> impl Responder {
let data = data.read().unwrap(); let data = data.read().unwrap();
HttpResponse::Ok().body(data.renderer.render("list", &data.routes).unwrap()) HttpResponse::Ok().body(data.renderer.render("list", &data.groups).unwrap())
} }
#[derive(Deserialize)] #[derive(Deserialize)]

View file

@ -14,20 +14,29 @@
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
} }
h1, p { margin: 0; } h1, p { margin: 0; }
table { margin-top: 1em; } table { margin-bottom: 1rem; }
td, th { padding: 0 0.5em; } header { display: flex; flex-wrap: wrap; align-items: baseline; margin-top: 2rem; }
header h2 { margin: 0 1rem 0 0; }
i { color: rgba(255, 255, 255, 0.5); }
td, th { padding: 0 0.5rem; }
.shortcut { text-align: right; } .shortcut { text-align: right; }
.target { text-align: left; width: 100%; }
</style> </style>
</head> </head>
<body> <body>
<h1>Bunbun Command List</h1> <h1>Bunbun Command List</h1>
<p><em>To edit this list, edit your <code>bunbun.toml</code> file.</em></p> <p><i>To edit this list, edit your <code>bunbun.toml</code> file.</i></p>
<main>
{{#each this}} {{!-- Iterate over RouteGroup --}}
<header><h2>{{this.name}}</h2><i>{{this.description}}</i></header>
<table> <table>
<tr> <tr>
<th>Shortcut</th> <th>Shortcut</th>
<th>Target</th> <th class="target">Target</th>
</tr> </tr>
{{#each this}}<tr><td class="shortcut">{{@key}}</td><td class="target">{{this}}</td></tr>{{/each}} {{#each this.routes}}<tr><td class="shortcut">{{@key}}</td><td class="target">{{this}}</td></tr>{{/each}}
</table> </table>
{{/each}}
</main>
</body> </body>
</html> </html>