🏗️ Elegant & Highly Performant Async Gemini Server Framework for the Modern Age
async framework gemini-protocol protocol gemini rust
0
fork

Configure Feed

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

feat(router): Add options system

Fuwn 215d8529 47df6e61

+67 -13
+1 -1
examples/fix_path.rs
··· 22 22 windmark::router::Router::new() 23 23 .set_private_key_file("windmark_private.pem") 24 24 .set_certificate_file("windmark_public.pem") 25 - .set_fix_path(true) 25 + .add_options(&[windmark::router_option::RouterOption::TrimTrailingSlashes]) 26 26 .mount( 27 27 "/close", 28 28 windmark::success!("Visit '/close/'; you should be close enough!"),
+1
src/lib.rs
··· 36 36 pub mod prelude; 37 37 pub mod response; 38 38 pub mod router; 39 + pub mod router_option; 39 40 pub mod utilities; 40 41 41 42 #[macro_use]
+59 -12
src/router.rs
··· 18 18 #![allow(clippy::significant_drop_tightening)] 19 19 20 20 use std::{ 21 + collections::HashSet, 21 22 error::Error, 22 23 fmt::Write, 23 24 future::IntoFuture, ··· 49 50 }, 50 51 module::{AsyncModule, Module}, 51 52 response::Response, 53 + router_option::RouterOption, 52 54 utilities, 53 55 }; 54 56 ··· 107 109 port: i32, 108 110 async_modules: Arc<AsyncMutex<Vec<Box<dyn AsyncModule + Send>>>>, 109 111 modules: Arc<Mutex<Vec<Box<dyn Module + Send>>>>, 110 - fix_path: bool, 112 + options: HashSet<RouterOption>, 111 113 listener_address: String, 112 114 } 113 115 ··· 419 421 url.set_path("/"); 420 422 } 421 423 422 - let fixed_path = if self.fix_path { 423 - utilities::normalize_path_slashes(url.path()) 424 - } else { 425 - url.path().to_string() 426 - }; 424 + let fixed_path = 425 + if self.options.contains(&RouterOption::TrimTrailingSlashes) { 426 + utilities::normalize_path_slashes(url.path()) 427 + } else { 428 + url.path().to_string() 429 + }; 427 430 let route = &mut self.routes.at(&fixed_path); 428 431 let peer_certificate = stream.ssl().peer_certificate(); 429 432 let hook_context = HookContext::new( ··· 967 970 self 968 971 } 969 972 970 - /// Performs a case-insensitive lookup of routes, using the case corrected 971 - /// path if successful. Missing/ extra trailing slashes are also corrected. 973 + /// Add optional features to the router 972 974 /// 973 975 /// # Examples 974 976 /// 975 977 /// ```rust 976 - /// windmark::router::Router::new().set_fix_path(true); 978 + /// use windmark::router_option::RouterOption; 979 + /// 980 + /// windmark::router::Router::new() 981 + /// .add_options(&[RouterOption::TrimTrailingSlashes]); 977 982 /// ``` 978 - pub const fn set_fix_path(&mut self, fix_path: bool) -> &mut Self { 979 - self.fix_path = fix_path; 983 + pub fn add_options(&mut self, options: &[RouterOption]) -> &mut Self { 984 + for option in options { 985 + self.options.insert(*option); 986 + } 987 + 988 + self 989 + } 990 + 991 + /// Toggle optional features for the router 992 + /// 993 + /// # Examples 994 + /// 995 + /// ```rust 996 + /// use windmark::router_option::RouterOption; 997 + /// 998 + /// windmark::router::Router::new() 999 + /// .toggle_options(&[RouterOption::TrimTrailingSlashes]); 1000 + /// ``` 1001 + pub fn toggle_options(&mut self, options: &[RouterOption]) -> &mut Self { 1002 + for option in options { 1003 + if self.options.contains(option) { 1004 + self.options.remove(option); 1005 + } else { 1006 + self.options.insert(*option); 1007 + } 1008 + } 1009 + 1010 + self 1011 + } 1012 + 1013 + /// Remove optional features from the router 1014 + /// 1015 + /// # Examples 1016 + /// 1017 + /// ```rust 1018 + /// use windmark::router_option::RouterOption; 1019 + /// 1020 + /// windmark::router::Router::new() 1021 + /// .remove_options(&[RouterOption::TrimTrailingSlashes]); 1022 + /// ``` 1023 + pub fn remove_options(&mut self, options: &[RouterOption]) -> &mut Self { 1024 + for option in options { 1025 + self.options.remove(option); 1026 + } 980 1027 981 1028 self 982 1029 } ··· 1031 1078 port: 1965, 1032 1079 modules: Arc::new(Mutex::new(vec![])), 1033 1080 async_modules: Arc::new(AsyncMutex::new(vec![])), 1034 - fix_path: false, 1081 + options: HashSet::new(), 1035 1082 private_key_content: None, 1036 1083 certificate_content: None, 1037 1084 listener_address: "0.0.0.0".to_string(),
+6
src/router_option.rs
··· 1 + /// Options that can be set for the `Router` 2 + #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] 3 + pub enum RouterOption { 4 + /// Trim trailing slashes from the URL path 5 + TrimTrailingSlashes, 6 + }