nestforge_http/
factory.rs1use std::net::SocketAddr;
2
3use anyhow::Result;
4use axum::Router;
5use nestforge_core::{Container, ModuleDefinition};
6use tower_http::trace::TraceLayer;
7
8pub struct NestForgeFactory<M: ModuleDefinition> {
20 _marker: std::marker::PhantomData<M>,
21 container: Container,
22}
23
24impl<M: ModuleDefinition> NestForgeFactory<M> {
25 pub fn create() -> Result<Self> {
26 let container = Container::new();
27
28 M::register(&container)?;
30
31 Ok(Self {
32 _marker: std::marker::PhantomData,
33 container,
34 })
35 }
36
37 pub async fn listen(self, port: u16) -> Result<()> {
38 let mut app: Router<Container> = Router::new();
43
44 for controller_router in M::controllers() {
48 app = app.merge(controller_router);
49 }
50
51 let app = app
56 .with_state(self.container.clone())
57 .layer(TraceLayer::new_for_http());
58
59 let addr = SocketAddr::from(([127, 0, 0, 1], port));
60 let listener = tokio::net::TcpListener::bind(addr).await?;
61
62 println!("🦀 NestForge running on http://{}", addr);
63
64 axum::serve(listener, app).await?;
65 Ok(())
66 }
67}