Struct astra::Server

source ·
pub struct Server { /* private fields */ }
Expand description

An HTTP server.

use astra::{Body, Request, Response, Server};

Server::bind("localhost:3000")
    .serve(|mut req: Request, _info| {
        println!("incoming {:?}", req.uri());
        Response::new(Body::new("Hello World!"))
    })
    .expect("failed to start server");

See the crate-level documentation for details.

Implementations§

source§

impl Server

source

pub fn bind(addr: impl ToSocketAddrs) -> Server

Binds a server to the provided address.

use astra::Server;
use std::net::SocketAddr;

let server = Server::bind("localhost:3000");
let server = Server::bind(SocketAddr::from(([127, 0, 0, 1], 3000)));
Panics

This method will panic if binding to the address fails.

source

pub fn serve<S>(self, service: S) -> Result<()>where S: Service + Sync,

Serve incoming connections with the provided service.

use astra::{Body, Request, Response, Server};

Server::bind("localhost:3000")
    .serve(|mut req: Request, _| {
        println!("incoming {:?}", req.uri());
        Response::new(Body::new("Hello World!"))
    })
    .expect("failed to start server");
source

pub fn serve_clone<S>(self, service: S) -> Result<()>where S: Service + Clone,

Like Self::serve but does not wrap service in an Arc and expects it to implement Clone and Sync internally.

source

pub fn max_workers(self, val: usize) -> Self

Sets the maximum number of threads in the worker pool.

By default, the limit is 15 threads per CPU core.

source

pub fn worker_keep_alive(self, val: Duration) -> Self

Sets how long to keep alive an idle thread in the worker pool.

By default, the timeout is set to 6 seconds.

source

pub fn http1_keep_alive(self, val: bool) -> Self

Sets whether to use keep-alive for HTTP/1 connections.

Default is true.

source

pub fn http1_half_close(self, val: bool) -> Self

Set whether HTTP/1 connections should support half-closures.

Clients can chose to shutdown their write-side while waiting for the server to respond. Setting this to true will prevent closing the connection immediately if read detects an EOF in the middle of a request.

Default is false.

source

pub fn http1_max_buf_size(self, val: usize) -> Self

Set the maximum buffer size.

Default is ~ 400kb.

source

pub fn http1_pipeline_flush(self, val: bool) -> Self

Sets whether to bunch up HTTP/1 writes until the read buffer is empty.

This isn’t really desirable in most cases, only really being useful in silly pipeline benchmarks.

source

pub fn http1_writev(self, enabled: bool) -> Self

Set whether HTTP/1 connections should try to use vectored writes, or always flatten into a single buffer.

Note that setting this to false may mean more copies of body data, but may also improve performance when an IO transport doesn’t support vectored writes well, such as most TLS implementations.

Setting this to true will force hyper to use queued strategy which may eliminate unnecessary cloning on some TLS backends

Default is auto. In this mode hyper will try to guess which mode to use

source

pub fn http1_title_case_headers(self, val: bool) -> Self

Set whether HTTP/1 connections will write header names as title case at the socket level.

Note that this setting does not affect HTTP/2.

Default is false.

source

pub fn http1_preserve_header_case(self, val: bool) -> Self

Set whether to support preserving original header cases.

Currently, this will record the original cases received, and store them in a private extension on the Request. It will also look for and use such an extension in any provided Response.

Since the relevant extension is still private, there is no way to interact with the original cases. The only effect this can have now is to forward the cases in a proxy-like fashion.

Note that this setting does not affect HTTP/2.

Default is false.

source

pub fn http1_only(self, val: bool) -> Self

Sets whether HTTP/1 is required.

Default is false.

source

pub fn local_addr(&self) -> Option<Result<SocketAddr>>

Get the local address of the bound socket

Trait Implementations§

source§

impl Default for Server

source§

fn default() -> Server

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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<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