♻️ Simple & Efficient Gemini-to-HTTP Proxy fuwn.net
proxy gemini-protocol protocol gemini http rust
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(response): move configuration into struct

Fuwn caeddebb 28fbcfde

+48 -25
+8 -3
src/html.rs
··· 13 13 pub fn from_gemini( 14 14 response: &germ::request::Response, 15 15 url: &Url, 16 - is_proxy: bool, 16 + configuration: &crate::response::configuration::Configuration, 17 17 ) -> Option<(String, String)> { 18 18 let ast_tree = germ::ast::Ast::from_string( 19 19 response.content().as_ref().map_or_else(String::default, String::clone), ··· 92 92 && href.contains("gemini://") 93 93 && !surface 94 94 { 95 - if is_proxy 95 + if (configuration.is_proxy()) 96 + || configuration.is_no_css() 96 97 || href 97 98 .trim_start_matches("gemini://") 98 99 .trim_end_matches('/') ··· 102 103 .unwrap() 103 104 != &url.host().unwrap().to_string().as_str() 104 105 { 105 - href = format!("/proxy/{}", href.trim_start_matches("gemini://")); 106 + href = format!( 107 + "/{}/{}", 108 + if configuration.is_no_css() { "nocss" } else { "proxy" }, 109 + href.trim_start_matches("gemini://") 110 + ); 106 111 } else { 107 112 href = href.trim_start_matches("gemini://").replacen( 108 113 &if let Some(host) = url.host() {
+1 -2
src/main.rs
··· 14 14 mod response; 15 15 mod url; 16 16 17 - #[macro_use] 18 - extern crate log; 17 + #[macro_use] extern crate log; 19 18 20 19 use {actix_web::web, response::default, std::env::var}; 21 20
+9 -11
src/response.rs
··· 1 + pub mod configuration; 2 + 1 3 use { 2 4 crate::url::from_path as url_from_path, 3 5 actix_web::{Error, HttpResponse}, ··· 22 24 ); 23 25 } 24 26 25 - let mut is_proxy = false; 26 - let mut is_raw = false; 27 - let mut is_nocss = false; 27 + let mut configuration = configuration::Configuration::new(); 28 28 let url = match url_from_path( 29 29 &format!("{}{}", req.path(), { 30 30 if !req.query_string().is_empty() || req.uri().to_string().ends_with('?') ··· 35 35 } 36 36 }), 37 37 false, 38 - &mut is_proxy, 39 - &mut is_raw, 40 - &mut is_nocss, 38 + &mut configuration, 41 39 ) { 42 40 Ok(url) => url, 43 41 Err(e) => { ··· 94 92 95 93 timer = Instant::now(); 96 94 97 - let mut html_context = if is_raw { 95 + let mut html_context = if configuration.is_raw() { 98 96 String::new() 99 97 } else { 100 98 format!( ··· 107 105 ) 108 106 }; 109 107 let gemini_html = 110 - crate::html::from_gemini(&response, &url, is_proxy).unwrap(); 108 + crate::html::from_gemini(&response, &url, &configuration).unwrap(); 111 109 let gemini_title = gemini_html.0; 112 110 let convert_time_taken = timer.elapsed(); 113 111 114 - if is_raw { 112 + if configuration.is_raw() { 115 113 html_context.push_str( 116 114 &response.content().as_ref().map_or_else(String::default, String::clone), 117 115 ); ··· 123 121 ); 124 122 } 125 123 126 - if is_nocss { 124 + if configuration.is_no_css() { 127 125 html_context.push_str(&gemini_html.1); 128 126 129 127 return Ok( ··· 142 140 "<link rel=\"stylesheet\" type=\"text/css\" href=\"{stylesheet}\">", 143 141 )); 144 142 } 145 - } else if !is_nocss { 143 + } else if !configuration.is_no_css() { 146 144 html_context.push_str(&format!(r#"<link rel="stylesheet" href="https://latex.vercel.app/style.css"><style>{CSS}</style>"#)); 147 145 148 146 if let Ok(primary) = var("PRIMARY_COLOUR") {
+23
src/response/configuration.rs
··· 1 + pub struct Configuration { 2 + is_proxy: bool, 3 + is_raw: bool, 4 + is_no_css: bool, 5 + } 6 + 7 + impl Configuration { 8 + pub const fn new() -> Self { 9 + Self { is_proxy: false, is_raw: false, is_no_css: false } 10 + } 11 + 12 + pub const fn is_proxy(&self) -> bool { self.is_proxy } 13 + 14 + pub const fn is_raw(&self) -> bool { self.is_raw } 15 + 16 + pub const fn is_no_css(&self) -> bool { self.is_no_css } 17 + 18 + pub fn set_proxy(&mut self, is_proxy: bool) { self.is_proxy = is_proxy; } 19 + 20 + pub fn set_raw(&mut self, is_raw: bool) { self.is_raw = is_raw; } 21 + 22 + pub fn set_no_css(&mut self, is_no_css: bool) { self.is_no_css = is_no_css; } 23 + }
+7 -9
src/url.rs
··· 3 3 pub fn from_path( 4 4 path: &str, 5 5 fallback: bool, 6 - is_proxy: &mut bool, 7 - is_raw: &mut bool, 8 - is_nocss: &mut bool, 6 + configuration: &mut crate::response::configuration::Configuration, 9 7 ) -> Result<Url, url::ParseError> { 10 8 Ok( 11 9 #[allow(clippy::blocks_in_conditions)] 12 10 match Url::try_from(&*if path.starts_with("/proxy") { 13 - *is_proxy = true; 11 + configuration.set_proxy(true); 14 12 15 13 format!( 16 14 "gemini://{}{}", ··· 18 16 if fallback { "/" } else { "" } 19 17 ) 20 18 } else if path.starts_with("/x") { 21 - *is_proxy = true; 19 + configuration.set_proxy(true); 22 20 23 21 format!( 24 22 "gemini://{}{}", ··· 26 24 if fallback { "/" } else { "" } 27 25 ) 28 26 } else if path.starts_with("/raw") { 29 - *is_proxy = true; 30 - *is_raw = true; 27 + configuration.set_proxy(true); 28 + configuration.set_raw(true); 31 29 32 30 format!( 33 31 "gemini://{}{}", ··· 35 33 if fallback { "/" } else { "" } 36 34 ) 37 35 } else if path.starts_with("/nocss") { 38 - *is_proxy = true; 39 - *is_nocss = true; 36 + configuration.set_proxy(true); 37 + configuration.set_no_css(true); 40 38 41 39 format!( 42 40 "gemini://{}{}",