Skip to main content

Server

Struct Server 

Source
pub struct Server<I: ReqInterceptor = DummyInterceptor> {
    pub port: u16,
    pub tls_param: Option<TlsParam>,
    pub interceptor: Option<I>,
    pub idle_timeout: Duration,
    /* private fields */
}
Expand description

HTTP/HTTPS 服务器核心结构

§泛型参数

  • I: 请求拦截器类型,必须实现 ReqInterceptor trait

§字段

  • port: 监听端口
  • tls_param: TLS 配置参数 (可选)
  • router: Axum 路由
  • interceptor: 请求拦截器实例 (可选)
  • idle_timeout: 连接空闲超时时间
  • shutdown_rx: 关闭信号接收器

Fields§

§port: u16§tls_param: Option<TlsParam>§interceptor: Option<I>§idle_timeout: Duration

Implementations§

Source§

impl<I> Server<I>
where I: ReqInterceptor + Clone + Send + Sync + 'static,

Source

pub fn with_interceptor<R>(self: Server<I>, interceptor: R) -> Server<R>
where R: ReqInterceptor + Clone + Send + Sync + 'static,

设置请求拦截器

用于将服务器的拦截器类型更改为新的类型

§类型参数
  • R: 新的拦截器类型
§参数
  • interceptor: 新的拦截器实例
§返回

返回配置了新拦截器的服务器实例

Source

pub fn with_tls_param(self, tls_param: Option<TlsParam>) -> Self

设置 TLS 参数

§参数
  • tls_param: TLS 配置参数,为 None 时禁用 TLS
§返回

返回配置了 TLS 的服务器实例

Source

pub fn with_timeout(self, timeout: Duration) -> Self

设置连接空闲超时时间

§参数
  • timeout: 超时时长
§返回

返回配置了超时的服务器实例

Source

pub async fn run(self) -> Result<(), Error>

启动服务器

根据 TLS 配置启动 HTTP 或 HTTPS 服务器,并监听关闭信号

§返回
  • Ok(()): 服务器成功启动并正常关闭
  • Err(std::io::Error): 启动或运行过程中出现 I/O 错误
§错误
  • 端口绑定失败
  • TLS 证书加载失败
  • 网络 I/O 错误

Auto Trait Implementations§

§

impl<I> Freeze for Server<I>
where I: Freeze,

§

impl<I = DummyInterceptor> !RefUnwindSafe for Server<I>

§

impl<I> Send for Server<I>

§

impl<I> Sync for Server<I>
where I: Sync,

§

impl<I> Unpin for Server<I>
where I: Unpin,

§

impl<I = DummyInterceptor> !UnwindSafe for Server<I>

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