Rework request routing slightly
This commit is contained in:
parent
076c5308f3
commit
08e4523f94
|
@ -1,6 +1,5 @@
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
use std::ops::Deref;
|
|
||||||
|
|
||||||
use tiny_http::Header;
|
use tiny_http::Header;
|
||||||
use tiny_http::Request;
|
use tiny_http::Request;
|
||||||
|
@ -31,8 +30,7 @@ pub fn go(domains: &[&str]) {
|
||||||
/// Each route represents a set of logic that should be used to respond to the request,
|
/// Each route represents a set of logic that should be used to respond to the request,
|
||||||
/// and the necessary information from the request in order to do so
|
/// and the necessary information from the request in order to do so
|
||||||
pub enum Route<'a> {
|
pub enum Route<'a> {
|
||||||
Next(&'a str),
|
PrevOrNext(&'a str, isize),
|
||||||
Prev(&'a str),
|
|
||||||
NotFound,
|
NotFound,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,20 +38,17 @@ impl<'a> Route<'a> {
|
||||||
|
|
||||||
/// Given a request, parse out the appropriate Route for that request
|
/// Given a request, parse out the appropriate Route for that request
|
||||||
pub fn parse_request(r: &'a Request) -> Self {
|
pub fn parse_request(r: &'a Request) -> Self {
|
||||||
let segments: Vec<_> = r.url()
|
let (first_segment, referrer_domain) = r.url()
|
||||||
.split('/')
|
.trim_matches('/')
|
||||||
.filter(|s| !s.is_empty())
|
.split_once('/')
|
||||||
.collect();
|
.unwrap_or((r.url(), "missing_referrer_domain"));
|
||||||
|
|
||||||
// TODO automatically detect a fallback url from the request's Referer header
|
match first_segment {
|
||||||
let referrer_domain = segments.get(1).map(|s| *s).unwrap_or("missing_referrer_domain");
|
"next" => {
|
||||||
|
Route::PrevOrNext(referrer_domain, 1)
|
||||||
match segments.get(0).map(Deref::deref) {
|
|
||||||
Some("next") => {
|
|
||||||
Route::Next(referrer_domain)
|
|
||||||
},
|
},
|
||||||
Some("prev") => {
|
"prev" => {
|
||||||
Route::Prev(referrer_domain)
|
Route::PrevOrNext(referrer_domain, -1)
|
||||||
},
|
},
|
||||||
_ => Route::NotFound,
|
_ => Route::NotFound,
|
||||||
}
|
}
|
||||||
|
@ -62,13 +57,7 @@ impl<'a> Route<'a> {
|
||||||
/// Generate a Response for this route
|
/// Generate a Response for this route
|
||||||
pub fn render<'b>(&self, domains: &[&'b str]) -> Response<Cursor<Cow<'b, [u8]>>> {
|
pub fn render<'b>(&self, domains: &[&'b str]) -> Response<Cursor<Cow<'b, [u8]>>> {
|
||||||
match self {
|
match self {
|
||||||
Self::Next(this_domain) | Self::Prev(this_domain) => {
|
Self::PrevOrNext(this_domain, offset) => {
|
||||||
|
|
||||||
let offset: isize = if let Self::Next(_) = self {
|
|
||||||
1
|
|
||||||
} else {
|
|
||||||
-1
|
|
||||||
};
|
|
||||||
|
|
||||||
let destination = domains.iter()
|
let destination = domains.iter()
|
||||||
.position(|d| d.starts_with(this_domain))
|
.position(|d| d.starts_with(this_domain))
|
||||||
|
|
Loading…
Reference in a new issue