Server

Struct Server 

Source
pub struct Server<S, L = Identity, SP = DefaultProvider> { /* private fields */ }
Available on crate feature 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>

Source

pub fn new(service: S) -> Self

Create a new server.

Source§

impl<S, L, SP> Server<S, L, SP>

Source

pub fn tls_config(self, config: impl Into<ServerTlsConfig>) -> Self

Available on crate features rustls or native-tls only.

Enable TLS with the specified configuration.

If not set, the server will not use TLS.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn set_max_headers(&mut self, max_headers: usize) -> &mut Self

👎Deprecated since 0.4.0: set_max_headers has been removed into http1_config
Available on crate feature http1 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.

Source

pub fn http1_config(&mut self) -> Http1Config<'_>

Available on crate feature http1 only.

Get configuration for http1 part.

Source

pub fn http2_config(&mut self) -> Http2Config<'_>

Available on crate feature http2 only.

Get configuration for http2 part.

Source

pub fn http1_only(self) -> Self

Available on crate feature http1 only.

Make server accept only HTTP/1.

Source

pub fn http2_only(self) -> Self

Available on crate feature http2 only.

Make server accept only HTTP/2.

Source

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.

Auto Trait Implementations§

§

impl<S, L, SP> Freeze for Server<S, L, SP>
where S: Freeze, L: Freeze, SP: Freeze,

§

impl<S, L = Identity, SP = DefaultProvider> !RefUnwindSafe for Server<S, L, SP>

§

impl<S, L, SP> Send for Server<S, L, SP>
where S: Send, L: Send, SP: Send,

§

impl<S, L = Identity, SP = DefaultProvider> !Sync for Server<S, L, SP>

§

impl<S, L, SP> Unpin for Server<S, L, SP>
where S: Unpin, L: Unpin, SP: Unpin,

§

impl<S, L = Identity, SP = DefaultProvider> !UnwindSafe for Server<S, L, SP>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more