pub struct Server<S, L = Identity, SP = DefaultProvider> { /* private fields */ }server only.Expand description
High level HTTP server.
§Examples
use std::net::SocketAddr;
use volo::net::Address;
use volo_http::server::{
Server,
route::{Router, get},
};
async fn index() -> &'static str {
"Hello, World!"
}
let app = Router::new().route("/", get(index));
let addr = "[::]:8080".parse::<SocketAddr>().unwrap();
let addr = Address::from(addr);
Server::new(app).run(addr).await.unwrap();Implementations§
Source§impl<S> Server<S, Identity, DefaultProvider>
impl<S> Server<S, Identity, DefaultProvider>
Source§impl<S, L, SP> Server<S, L, SP>
impl<S, L, SP> Server<S, L, SP>
Sourcepub fn tls_config(self, config: impl Into<ServerTlsConfig>) -> Self
Available on crate features rustls or native-tls only.
pub fn tls_config(self, config: impl Into<ServerTlsConfig>) -> Self
rustls or native-tls only.Enable TLS with the specified configuration.
If not set, the server will not use TLS.
Sourcepub fn register_shutdown_hook(
self,
hook: impl FnOnce() -> BoxFuture<'static, ()> + 'static + Send,
) -> Self
pub fn register_shutdown_hook( self, hook: impl FnOnce() -> BoxFuture<'static, ()> + 'static + Send, ) -> Self
Register shutdown hook.
Hook functions will be called just before volo’s own gracefull existing code starts, in reverse order of registration.
Sourcepub fn layer<Inner>(self, layer: Inner) -> Server<S, Stack<Inner, L>, SP>
pub fn layer<Inner>(self, layer: Inner) -> Server<S, Stack<Inner, L>, SP>
Add a new inner layer to the server.
The layer’s Service should be Send + Sync + Clone + 'static.
§Order
Assume we already have two layers: foo and bar. We want to add a new layer baz.
The current order is: foo -> bar (the request will come to foo first, and then bar).
After we call .layer(baz), we will get: foo -> bar -> baz.
Sourcepub fn layer_front<Front>(self, layer: Front) -> Server<S, Stack<L, Front>, SP>
pub fn layer_front<Front>(self, layer: Front) -> Server<S, Stack<L, Front>, SP>
Add a new front layer to the server.
The layer’s Service should be Send + Sync + Clone + 'static.
§Order
Assume we already have two layers: foo and bar. We want to add a new layer baz.
The current order is: foo -> bar (the request will come to foo first, and then bar).
After we call .layer_front(baz), we will get: baz -> foo -> bar.
Sourcepub fn span_provider<P>(self, span_provider: P) -> Server<S, L, P>
pub fn span_provider<P>(self, span_provider: P) -> Server<S, L, P>
Set a SpanProvider to the server.
Server will enter the Span that created by SpanProvider::on_serve when starting to
serve a request, and call SpanProvider::leave_serve when leaving the serve function.
Sourcepub fn set_max_headers(&mut self, max_headers: usize) -> &mut Self
👎Deprecated since 0.4.0: set_max_headers has been removed into http1_configAvailable on crate feature http1 only.
pub fn set_max_headers(&mut self, max_headers: usize) -> &mut Self
set_max_headers has been removed into http1_confighttp1 only.Set the maximum number of headers.
When a request is received, the parser will reserve a buffer to store headers for optimal performance.
If server receives more headers than the buffer size, it responds to the client with “431 Request Header Fields Too Large”.
Note that headers is allocated on the stack by default, which has higher performance. After setting this value, headers will be allocated in heap memory, that is, heap memory allocation will occur for each request, and there will be a performance drop of about 5%.
Default is 100.
Sourcepub fn http1_config(&mut self) -> Http1Config<'_>
Available on crate feature http1 only.
pub fn http1_config(&mut self) -> Http1Config<'_>
http1 only.Get configuration for http1 part.
Sourcepub fn http2_config(&mut self) -> Http2Config<'_>
Available on crate feature http2 only.
pub fn http2_config(&mut self) -> Http2Config<'_>
http2 only.Get configuration for http2 part.
Sourcepub fn http1_only(self) -> Self
Available on crate feature http1 only.
pub fn http1_only(self) -> Self
http1 only.Make server accept only HTTP/1.
Sourcepub fn http2_only(self) -> Self
Available on crate feature http2 only.
pub fn http2_only(self) -> Self
http2 only.Make server accept only HTTP/2.
Sourcepub async fn run<MI, B>(self, mk_incoming: MI) -> Result<(), BoxError>where
S: Service<ServerContext, Request<B>> + Send + Sync + 'static,
S::Response: IntoResponse,
S::Error: IntoResponse,
L: Layer<S> + Send + Sync + 'static,
L::Service: Service<ServerContext, Request, Error = Infallible> + Send + Sync + 'static,
<L::Service as Service<ServerContext, Request>>::Response: IntoResponse,
SP: SpanProvider + Clone + Send + Sync + Unpin + 'static,
MI: MakeIncoming,
pub async fn run<MI, B>(self, mk_incoming: MI) -> Result<(), BoxError>where
S: Service<ServerContext, Request<B>> + Send + Sync + 'static,
S::Response: IntoResponse,
S::Error: IntoResponse,
L: Layer<S> + Send + Sync + 'static,
L::Service: Service<ServerContext, Request, Error = Infallible> + Send + Sync + 'static,
<L::Service as Service<ServerContext, Request>>::Response: IntoResponse,
SP: SpanProvider + Clone + Send + Sync + Unpin + 'static,
MI: MakeIncoming,
The main entry point for the server.