use structs instead of hashmaps for templates
This commit is contained in:
parent
f67d461512
commit
5ea3b8da5b
2 changed files with 51 additions and 24 deletions
58
src/main.rs
58
src/main.rs
|
@ -16,6 +16,8 @@ use std::io::Write;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
mod template_args;
|
||||||
|
|
||||||
/// https://url.spec.whatwg.org/#fragment-percent-encode-set
|
/// https://url.spec.whatwg.org/#fragment-percent-encode-set
|
||||||
static FRAGMENT_ENCODE_SET: &AsciiSet =
|
static FRAGMENT_ENCODE_SET: &AsciiSet =
|
||||||
&CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
|
&CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
|
||||||
|
@ -75,23 +77,20 @@ fn hop(
|
||||||
let data = data.read().unwrap();
|
let data = data.read().unwrap();
|
||||||
|
|
||||||
match resolve_hop(&query.to, &data.routes, &data.default_route) {
|
match resolve_hop(&query.to, &data.routes, &data.default_route) {
|
||||||
(Some(path), args) => {
|
(Some(path), args) => HttpResponse::Found()
|
||||||
let mut template_args = HashMap::new();
|
.header(
|
||||||
template_args.insert(
|
header::LOCATION,
|
||||||
"query",
|
data
|
||||||
utf8_percent_encode(&args, FRAGMENT_ENCODE_SET).to_string(),
|
.renderer
|
||||||
);
|
.render_template(
|
||||||
|
&path,
|
||||||
HttpResponse::Found()
|
&template_args::query(
|
||||||
.header(
|
utf8_percent_encode(&args, FRAGMENT_ENCODE_SET).to_string(),
|
||||||
header::LOCATION,
|
),
|
||||||
data
|
)
|
||||||
.renderer
|
.unwrap(),
|
||||||
.render_template(&path, &template_args)
|
)
|
||||||
.unwrap(),
|
.finish(),
|
||||||
)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
(None, _) => HttpResponse::NotFound().body("not found"),
|
(None, _) => HttpResponse::NotFound().body("not found"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -133,23 +132,34 @@ fn resolve_hop(
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn index(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
fn index(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
||||||
let data = data.read().unwrap();
|
let data = data.read().unwrap();
|
||||||
let mut template_args = HashMap::new();
|
HttpResponse::Ok().body(
|
||||||
template_args.insert("hostname", &data.public_address);
|
data
|
||||||
HttpResponse::Ok()
|
.renderer
|
||||||
.body(data.renderer.render("index", &template_args).unwrap())
|
.render(
|
||||||
|
"index",
|
||||||
|
&template_args::hostname(data.public_address.clone()),
|
||||||
|
)
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/bunbunsearch.xml")]
|
#[get("/bunbunsearch.xml")]
|
||||||
fn opensearch(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
fn opensearch(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
||||||
let data = data.read().unwrap();
|
let data = data.read().unwrap();
|
||||||
let mut template_args = HashMap::new();
|
|
||||||
template_args.insert("hostname", &data.public_address);
|
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.header(
|
.header(
|
||||||
header::CONTENT_TYPE,
|
header::CONTENT_TYPE,
|
||||||
"application/opensearchdescription+xml",
|
"application/opensearchdescription+xml",
|
||||||
)
|
)
|
||||||
.body(data.renderer.render("opensearch", &template_args).unwrap())
|
.body(
|
||||||
|
data
|
||||||
|
.renderer
|
||||||
|
.render(
|
||||||
|
"opensearch",
|
||||||
|
&template_args::hostname(data.public_address.clone()),
|
||||||
|
)
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Dynamic variables that either need to be present at runtime, or can be
|
/// Dynamic variables that either need to be present at runtime, or can be
|
||||||
|
|
17
src/template_args.rs
Normal file
17
src/template_args.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
pub fn query(query: String) -> impl Serialize {
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct TemplateArgs {
|
||||||
|
query: String,
|
||||||
|
}
|
||||||
|
TemplateArgs { query }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn hostname(hostname: String) -> impl Serialize {
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct TemplateArgs {
|
||||||
|
pub hostname: String,
|
||||||
|
}
|
||||||
|
TemplateArgs { hostname }
|
||||||
|
}
|
Loading…
Reference in a new issue