pub struct CustomRouterConfig {
pub name: Option<String>,
pub path_prefix: Option<String>,
}Expand description
Configuration for custom router attachment
Fields§
§name: Option<String>Optional name for the custom router (for logging/debugging)
path_prefix: Option<String>Optional path prefix for the router
Implementations§
Source§impl CustomRouterConfig
impl CustomRouterConfig
Sourcepub fn with_name(name: impl Into<String>) -> Self
pub fn with_name(name: impl Into<String>) -> Self
Create a new custom router configuration with a name
Sourcepub fn with_prefix(path_prefix: impl Into<String>) -> Self
pub fn with_prefix(path_prefix: impl Into<String>) -> Self
Create a new custom router configuration with a path prefix
Examples found in repository?
examples/simple_custom_router_test.rs (line 58)
30async fn main() -> Result<(), Box<dyn std::error::Error>> {
31 println!("🧪 Testing custom router functionality...");
32
33 // Create OAuth provider
34 let github_config = GitHubOAuthConfig {
35 client_id: "test_client_id".to_string(),
36 client_secret: "test_client_secret".to_string(),
37 redirect_uri: "http://localhost:8080/oauth/callback".to_string(),
38 scope: "read:user".to_string(),
39 provider_name: "github".to_string(),
40 };
41
42 let github_oauth_provider = GitHubOAuthProvider::new_github(github_config);
43 let oauth_provider = OAuthProvider::new(github_oauth_provider);
44
45 // Create custom routers
46 let health_router = Router::new()
47 .route("/health", get(health_check));
48
49 let api_router = Router::new()
50 .route("/status", get(api_status));
51
52 // Build microkernel server with custom routers
53 let microkernel: GitHubMicrokernelServer = MicrokernelServer::new()
54 .with_oauth_provider(oauth_provider)
55 .with_custom_router(health_router)
56 .with_custom_router_config(
57 api_router,
58 CustomRouterConfig::with_prefix("/api"),
59 );
60
61 // Test that the router can be built
62 let router = microkernel.build_router()?;
63 println!("✅ Router built successfully!");
64
65 // Print available endpoints
66 println!("\n📋 Server would have these endpoints:");
67 println!(" OAuth:");
68 println!(" GET /oauth/login");
69 println!(" GET /oauth/callback");
70 println!(" POST /oauth/token");
71 println!(" Custom:");
72 println!(" GET /health");
73 println!(" GET /api/status");
74
75 println!("\n🎉 Custom router functionality is working correctly!");
76 println!(" The server can be built and all routers are properly attached.");
77
78 Ok(())
79}Sourcepub fn with_name_and_prefix(
name: impl Into<String>,
path_prefix: impl Into<String>,
) -> Self
pub fn with_name_and_prefix( name: impl Into<String>, path_prefix: impl Into<String>, ) -> Self
Create a new custom router configuration with both name and path prefix
Examples found in repository?
examples/custom_router_example.rs (line 151)
107async fn main() -> Result<(), Box<dyn std::error::Error>> {
108 // Initialize tracing for logging
109 tracing_subscriber::fmt::init();
110
111 // Create GitHub OAuth provider
112 let github_config = GitHubOAuthConfig {
113 client_id: std::env::var("GITHUB_CLIENT_ID").unwrap_or_else(|_| "demo_client_id".to_string()),
114 client_secret: std::env::var("GITHUB_CLIENT_SECRET").unwrap_or_else(|_| "demo_client_secret".to_string()),
115 redirect_uri: "http://localhost:8080/oauth/callback".to_string(),
116 scope: "read:user".to_string(),
117 provider_name: "github".to_string(),
118 };
119
120 let github_oauth_provider = GitHubOAuthProvider::new_github(github_config);
121 let oauth_provider = OAuthProvider::new(github_oauth_provider);
122
123 // Create custom routers
124
125 // 1. Health and basic endpoints router
126 let health_router = Router::new()
127 .route("/health", get(health_check))
128 .route("/webhooks", post(webhook_handler));
129
130 // 2. Monitoring router with path prefix
131 let monitoring_router = Router::new()
132 .route("/metrics", get(metrics));
133
134 // 3. API router with versioned endpoints
135 let api_router = Router::new()
136 .route("/v1/status", get(api_status))
137 .route("/{version}/status", get(api_status));
138
139 // 4. Admin router with HTML interface
140 let admin_router = Router::new()
141 .route("/", get(admin_dashboard));
142
143 // Configure custom routers with different configurations
144 let custom_routers = vec![
145 // Health router without prefix (attached to root)
146 (health_router, CustomRouterConfig::default()),
147
148 // Monitoring router with prefix and name
149 (
150 monitoring_router,
151 CustomRouterConfig::with_name_and_prefix("Monitoring", "/monitoring"),
152 ),
153
154 // API router with prefix
155 (
156 api_router,
157 CustomRouterConfig::with_name_and_prefix("API", "/api"),
158 ),
159
160 // Admin router with prefix
161 (
162 admin_router,
163 CustomRouterConfig::with_name_and_prefix("Admin Dashboard", "/admin"),
164 ),
165 ];
166
167 // Build the microkernel server with OAuth provider and custom routers
168 let microkernel: GitHubMicrokernelServer = MicrokernelServer::new()
169 .with_oauth_provider(oauth_provider)
170 .with_custom_routers(custom_routers);
171
172 // Define the bind address
173 let bind_address: SocketAddr = "127.0.0.1:8080".parse()?;
174
175 // Print available endpoints
176 println!("🚀 Starting microkernel server with custom routers...");
177 println!("📍 Server listening on: http://{}", bind_address);
178 println!("\n📋 Available endpoints:");
179 println!(" OAuth:");
180 println!(" GET /oauth/login");
181 println!(" GET /oauth/callback");
182 println!(" POST /oauth/token");
183 println!(" Custom Endpoints:");
184 println!(" GET /health - Health check");
185 println!(" POST /webhooks - Webhook handler");
186 println!(" GET /monitoring/metrics - System metrics");
187 println!(" GET /api/v1/status - API status");
188 println!(" GET /api/{{version}}/status - Versioned API status");
189 println!(" GET /admin - Admin dashboard");
190 println!("\n🔧 Try these commands:");
191 println!(" curl http://localhost:8080/health");
192 println!(" curl http://localhost:8080/monitoring/metrics");
193 println!(" curl http://localhost:8080/api/v1/status");
194 println!(" open http://localhost:8080/admin");
195
196 // Start the server
197 microkernel.serve(bind_address).await?;
198
199 Ok(())
200}Trait Implementations§
Source§impl Clone for CustomRouterConfig
impl Clone for CustomRouterConfig
Source§fn clone(&self) -> CustomRouterConfig
fn clone(&self) -> CustomRouterConfig
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for CustomRouterConfig
impl Debug for CustomRouterConfig
Auto Trait Implementations§
impl Freeze for CustomRouterConfig
impl RefUnwindSafe for CustomRouterConfig
impl Send for CustomRouterConfig
impl Sync for CustomRouterConfig
impl Unpin for CustomRouterConfig
impl UnwindSafe for CustomRouterConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreCreates a shared type from an unshared type.