pub struct HttpServerBuilder<T, L>{ /* private fields */ }Expand description
HttpServerBuilder is a builder for configuring and initializing an HttpServer.
It allows for setting up the server address, adding services, and configuring shared state.
Implementations§
Source§impl<T, L> HttpServerBuilder<T, L>where
T: ToSocketAddrs + Default + Debug + Clone,
L: Layer<HandlerFn> + Clone + Send + 'static,
L::Service: Service<(HttpRequest, HttpPayload), Response = HttpResponse, Error = Error> + Send + 'static,
<L::Service as Service<(HttpRequest, HttpPayload)>>::Future: Send + 'static,
impl<T, L> HttpServerBuilder<T, L>where
T: ToSocketAddrs + Default + Debug + Clone,
L: Layer<HandlerFn> + Clone + Send + 'static,
L::Service: Service<(HttpRequest, HttpPayload), Response = HttpResponse, Error = Error> + Send + 'static,
<L::Service as Service<(HttpRequest, HttpPayload)>>::Future: Send + 'static,
Sourcepub fn app_state<S: Send + Sync + 'static>(self, state: S) -> Self
pub fn app_state<S: Send + Sync + 'static>(self, state: S) -> Self
Adds shared application state to be accessible in request handlers.
State is stored in a HashMap and keyed based on the TypeId of the state object.
§Arguments
state: A state object of typeSthat implementsSend + Sync + 'static.
§Returns
The builder instance with the shared state added.
§Examples
struct MyState {
state: String
}
let builder = HttpServer::builder()
.with_state(MyState { state: "Hello, world!".to_string() })
.bind("127.0.0.1:8080");Examples found in repository?
188async fn main() -> Result<(), HttpServerError> {
189 logger::init_tracing()?;
190 let state = State {
191 dir: "/Users/emil/projects/tosic-http/target/doc".to_string(),
192 };
193
194 let server = HttpServerBuilder::default()
195 .app_state(state)
196 .bind("0.0.0.0:4221")
197 //.service_method(Method::POST, "/", test_handler)
198 .route(post("/", test_handler).get(test_get))
199 //.service_method(Method::GET, "/bad", not_working)
200 .service(not_working)
201 .service(test_fn)
202 //.service(website)
203 .build()
204 .await?;
205
206 match server.serve().await {
207 Ok(_) => (),
208 Err(e) => panic!("Failed to serve: {}", e),
209 }
210
211 Ok(())
212}Sourcepub fn service_method<H, Args>(
self,
method: Method,
path: &str,
handler: H,
) -> Self
pub fn service_method<H, Args>( self, method: Method, path: &str, handler: H, ) -> Self
Adds a service handler to the server.
§Arguments
method: The HTTP method for the service endpoint.path: The path of the service endpoint.handler: A handler implementing theHandlertrait, defining a service endpoint.
§Returns
The builder instance with the handler added.
§Examples
async fn basic_handler() -> impl Responder<Body = BoxBody> {
HttpResponse::new(200)
}
let builder = HttpServer::builder()
.service_method(Method::GET, "/", basic_handler)
.bind("127.0.0.1:8080");Sourcepub fn service<H, Args>(self, handler: H) -> Self
pub fn service<H, Args>(self, handler: H) -> Self
Adds a service handler to the server.
§Arguments
handler: A handler implementing theHandler&HttpServicetraits.
§Returns
The builder instance with the handler added.
§Examples
#[get("/")]
async fn basic_handler() -> impl Responder<Body = BoxBody> {
HttpResponse::new(200)
}
let builder = HttpServer::builder()
.service(basic_handler)
.bind("127.0.0.1:8080");Examples found in repository?
111async fn main() -> Result<(), HttpServerError> {
112 logger::init_tracing()?;
113
114 let server = HttpServerBuilder::default()
115 .bind("0.0.0.0:4221")
116 .service(devices)
117 .build()
118 .await?;
119
120 match server.serve().await {
121 Ok(_) => (),
122 Err(e) => panic!("Failed to serve: {}", e),
123 }
124
125 Ok(())
126}More examples
188async fn main() -> Result<(), HttpServerError> {
189 logger::init_tracing()?;
190 let state = State {
191 dir: "/Users/emil/projects/tosic-http/target/doc".to_string(),
192 };
193
194 let server = HttpServerBuilder::default()
195 .app_state(state)
196 .bind("0.0.0.0:4221")
197 //.service_method(Method::POST, "/", test_handler)
198 .route(post("/", test_handler).get(test_get))
199 //.service_method(Method::GET, "/bad", not_working)
200 .service(not_working)
201 .service(test_fn)
202 //.service(website)
203 .build()
204 .await?;
205
206 match server.serve().await {
207 Ok(_) => (),
208 Err(e) => panic!("Failed to serve: {}", e),
209 }
210
211 Ok(())
212}Sourcepub fn route(self, route_builder: RouteBuilder<'_>) -> Self
pub fn route(self, route_builder: RouteBuilder<'_>) -> Self
Adds a route to the server.
§Arguments
route_builder: A builder for defining the route.
§Returns
The builder instance with the route added.
Examples found in repository?
188async fn main() -> Result<(), HttpServerError> {
189 logger::init_tracing()?;
190 let state = State {
191 dir: "/Users/emil/projects/tosic-http/target/doc".to_string(),
192 };
193
194 let server = HttpServerBuilder::default()
195 .app_state(state)
196 .bind("0.0.0.0:4221")
197 //.service_method(Method::POST, "/", test_handler)
198 .route(post("/", test_handler).get(test_get))
199 //.service_method(Method::GET, "/bad", not_working)
200 .service(not_working)
201 .service(test_fn)
202 //.service(website)
203 .build()
204 .await?;
205
206 match server.serve().await {
207 Ok(_) => (),
208 Err(e) => panic!("Failed to serve: {}", e),
209 }
210
211 Ok(())
212}Sourcepub fn bind(self, addr: T) -> Self
pub fn bind(self, addr: T) -> Self
Sets the address the server will bind to.
§Arguments
addr: The address for the server to bind, implementingToSocketAddrs.
§Returns
Returns the builder instance with the binding address set.
§Examples
let builder = HttpServer::builder()
.bind("127.0.0.1:8080");Examples found in repository?
111async fn main() -> Result<(), HttpServerError> {
112 logger::init_tracing()?;
113
114 let server = HttpServerBuilder::default()
115 .bind("0.0.0.0:4221")
116 .service(devices)
117 .build()
118 .await?;
119
120 match server.serve().await {
121 Ok(_) => (),
122 Err(e) => panic!("Failed to serve: {}", e),
123 }
124
125 Ok(())
126}More examples
188async fn main() -> Result<(), HttpServerError> {
189 logger::init_tracing()?;
190 let state = State {
191 dir: "/Users/emil/projects/tosic-http/target/doc".to_string(),
192 };
193
194 let server = HttpServerBuilder::default()
195 .app_state(state)
196 .bind("0.0.0.0:4221")
197 //.service_method(Method::POST, "/", test_handler)
198 .route(post("/", test_handler).get(test_get))
199 //.service_method(Method::GET, "/bad", not_working)
200 .service(not_working)
201 .service(test_fn)
202 //.service(website)
203 .build()
204 .await?;
205
206 match server.serve().await {
207 Ok(_) => (),
208 Err(e) => panic!("Failed to serve: {}", e),
209 }
210
211 Ok(())
212}Sourcepub async fn build(self) -> Result<HttpServer<L>>
pub async fn build(self) -> Result<HttpServer<L>>
Builds and initializes the HttpServer with the current configuration.
§Errors
Returns io::Error if there was an error initializing the server.
§Examples
let server = HttpServer::builder()
.bind("127.0.0.1:8080")
.build()
.await
.unwrap();Examples found in repository?
111async fn main() -> Result<(), HttpServerError> {
112 logger::init_tracing()?;
113
114 let server = HttpServerBuilder::default()
115 .bind("0.0.0.0:4221")
116 .service(devices)
117 .build()
118 .await?;
119
120 match server.serve().await {
121 Ok(_) => (),
122 Err(e) => panic!("Failed to serve: {}", e),
123 }
124
125 Ok(())
126}More examples
188async fn main() -> Result<(), HttpServerError> {
189 logger::init_tracing()?;
190 let state = State {
191 dir: "/Users/emil/projects/tosic-http/target/doc".to_string(),
192 };
193
194 let server = HttpServerBuilder::default()
195 .app_state(state)
196 .bind("0.0.0.0:4221")
197 //.service_method(Method::POST, "/", test_handler)
198 .route(post("/", test_handler).get(test_get))
199 //.service_method(Method::GET, "/bad", not_working)
200 .service(not_working)
201 .service(test_fn)
202 //.service(website)
203 .build()
204 .await?;
205
206 match server.serve().await {
207 Ok(_) => (),
208 Err(e) => panic!("Failed to serve: {}", e),
209 }
210
211 Ok(())
212}Trait Implementations§
Source§impl<T, L> Clone for HttpServerBuilder<T, L>
impl<T, L> Clone for HttpServerBuilder<T, L>
Source§fn clone(&self) -> HttpServerBuilder<T, L>
fn clone(&self) -> HttpServerBuilder<T, L>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more