enhance(server):完成服务器启动。
This commit is contained in:
		| @@ -5,6 +5,7 @@ use tracing::{error, info}; | |||||||
|  |  | ||||||
| mod logging; | mod logging; | ||||||
| mod products; | mod products; | ||||||
|  | mod server_routes; | ||||||
|  |  | ||||||
| #[tokio::main] | #[tokio::main] | ||||||
| async fn main() { | async fn main() { | ||||||
| @@ -16,4 +17,22 @@ async fn main() { | |||||||
|         Err(e) => error!("Failed to load products: {}", e), |         Err(e) => error!("Failed to load products: {}", e), | ||||||
|         Ok(_) => info!("Products loaded successfully"), |         Ok(_) => info!("Products loaded successfully"), | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     let main_route = server_routes::ServerMainRouter::new(); | ||||||
|  |     // 启动服务 | ||||||
|  |     let bind_addr = String::from("0.0.0.0:3000"); | ||||||
|  |     info!("Server listen on [{}]", bind_addr); | ||||||
|  |     let listener = match tokio::net::TcpListener::bind(bind_addr).await { | ||||||
|  |         Ok(listener) => listener, | ||||||
|  |         Err(e) => { | ||||||
|  |             error!("Failed to bind to port 80: {}", e); | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  |     }; | ||||||
|  |     if axum::serve(listener, main_route.into_make_service()) | ||||||
|  |         .await | ||||||
|  |         .is_err() | ||||||
|  |     { | ||||||
|  |         error!("Failed to start server"); | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										50
									
								
								license_server/src/server_routes.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								license_server/src/server_routes.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,50 @@ | |||||||
|  | use std::ops::Deref; | ||||||
|  |  | ||||||
|  | use axum::{routing::IntoMakeService, Router}; | ||||||
|  |  | ||||||
|  | /// 系统服务核心路由控制组件。 | ||||||
|  | pub struct ServerMainRouter { | ||||||
|  |     main_router: Router, | ||||||
|  | } | ||||||
|  |  | ||||||
|  | impl ServerMainRouter { | ||||||
|  |     /// 构建新的系统服务路由组件。 | ||||||
|  |     pub fn new() -> Box<ServerMainRouter> { | ||||||
|  |         Box::new(ServerMainRouter { | ||||||
|  |             main_router: Router::new(), | ||||||
|  |         }) | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /// 将系统路由转换为可以供绑定使用的服务。 | ||||||
|  |     pub fn into_make_service(self) -> IntoMakeService<Router> { | ||||||
|  |         self.main_router.into_make_service() | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /// 注册新的子路由进入系统服务路由。 | ||||||
|  |     /// | ||||||
|  |     /// - `route`:要注册的子路由。 | ||||||
|  |     pub fn register<R>(self, route: R) -> Box<Self> | ||||||
|  |     where | ||||||
|  |         R: Into<Router>, | ||||||
|  |     { | ||||||
|  |         Box::new(ServerMainRouter { | ||||||
|  |             main_router: self.main_router.merge(route), | ||||||
|  |         }) | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /// 批量注册子路由进入系统服务路由。 | ||||||
|  |     /// | ||||||
|  |     /// - `routes`:要注册的子路由列表。 | ||||||
|  |     pub fn registers<I, R>(self, routes: I) -> Box<Self> | ||||||
|  |     where | ||||||
|  |         R: Deref<Target = Router>, | ||||||
|  |         I: Iterator<Item = R>, | ||||||
|  |     { | ||||||
|  |         let router = routes.into_iter().fold(self.main_router, |router, route| { | ||||||
|  |             router.merge(route.clone()) | ||||||
|  |         }); | ||||||
|  |         Box::new(ServerMainRouter { | ||||||
|  |             main_router: router, | ||||||
|  |         }) | ||||||
|  |     } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user