aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFuwn <[email protected]>2022-04-01 10:55:27 +0000
committerFuwn <[email protected]>2022-04-01 10:55:27 +0000
commit2350187df9a89e72d8220463cd92cb811592d52c (patch)
tree13b76e25f1fcefd1a5a4ce0da4026a49b1a38d6c /src
parentfix(default): fallback routing (diff)
downloadseptember-2350187df9a89e72d8220463cd92cb811592d52c.tar.xz
september-2350187df9a89e72d8220463cd92cb811592d52c.zip
format(url): tidy fallback
Diffstat (limited to 'src')
-rw-r--r--src/main.rs85
1 files changed, 36 insertions, 49 deletions
diff --git a/src/main.rs b/src/main.rs
index a69cf51..601de83 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -188,48 +188,28 @@ fn gemini_to_html(response: &Response, url: &Url) -> (String, String) {
(title, response_string)
}
-#[allow(clippy::unused_async, clippy::future_not_send, clippy::too_many_lines)]
-async fn default(req: actix_web::HttpRequest) -> Result<HttpResponse, Error> {
- // Try to construct a Gemini URL
- let url = match Url::try_from(&*if req.path().starts_with("/proxy") {
- format!("gemini://{}", req.path().replace("/proxy/", ""))
- } else if req.path().starts_with("/x") {
- format!("gemini://{}", req.path().replace("/x/", ""))
- } else {
- // Try to set `ROOT` as `ROOT` environment variable, or use
- // `"gemini://fuwn.me"` as default.
- format!(
- "{}{}",
- {
- if let Ok(root) = var("ROOT") {
- root
- } else {
- warn!(
- "could not use ROOT from environment variables, proceeding with \
- default root: gemini://fuwn.me"
- );
-
- "gemini://fuwn.me".to_string()
- }
- },
- req.path()
- )
- }) {
- Ok(url) => url,
- Err(e) => {
- return Ok(HttpResponse::Ok().body(e.to_string()));
- }
- };
- let fallback_url =
- match Url::try_from(&*if req.path().starts_with("/proxy") {
- format!("gemini://{}/", req.path().replace("/proxy/", ""))
- } else if req.path().starts_with("/x") {
- format!("gemini://{}/", req.path().replace("/x/", ""))
+fn make_url(
+ path: &str,
+ fallback: bool,
+) -> Result<Url, gmi::url::UrlParseError> {
+ Ok(
+ match Url::try_from(&*if path.starts_with("/proxy") {
+ format!(
+ "gemini://{}{}",
+ path.replace("/proxy/", ""),
+ if fallback { "/" } else { "" }
+ )
+ } else if path.starts_with("/x") {
+ format!(
+ "gemini://{}{}",
+ path.replace("/x/", ""),
+ if fallback { "/" } else { "" }
+ )
} else {
// Try to set `ROOT` as `ROOT` environment variable, or use
// `"gemini://fuwn.me"` as default.
format!(
- "{}{}/",
+ "{}{}{}",
{
if let Ok(root) = var("ROOT") {
root
@@ -242,14 +222,20 @@ async fn default(req: actix_web::HttpRequest) -> Result<HttpResponse, Error> {
"gemini://fuwn.me".to_string()
}
},
- req.path()
+ path,
+ if fallback { "/" } else { "" }
)
}) {
Ok(url) => url,
- Err(e) => {
- return Ok(HttpResponse::Ok().body(e.to_string()));
- }
- };
+ Err(e) => return Err(e),
+ },
+ )
+}
+
+#[allow(clippy::unused_async, clippy::future_not_send, clippy::too_many_lines)]
+async fn default(req: actix_web::HttpRequest) -> Result<HttpResponse, Error> {
+ // Try to construct a Gemini URL
+ let url = make_url(req.path(), false).unwrap();
// Make a request to get Gemini content and time it.
let mut timer = Instant::now();
let mut response = match gmi::request::make_request(&url) {
@@ -259,12 +245,13 @@ async fn default(req: actix_web::HttpRequest) -> Result<HttpResponse, Error> {
}
};
if response.data.is_empty() {
- response = match gmi::request::make_request(&fallback_url) {
- Ok(response) => response,
- Err(e) => {
- return Ok(HttpResponse::Ok().body(e.to_string()));
- }
- };
+ response =
+ match gmi::request::make_request(&make_url(req.path(), true).unwrap()) {
+ Ok(response) => response,
+ Err(e) => {
+ return Ok(HttpResponse::Ok().body(e.to_string()));
+ }
+ };
}
let response_time_taken = timer.elapsed();