Skip to main content

HostBuilder

Struct HostBuilder 

Source
pub struct HostBuilder { /* private fields */ }

Implementations§

Source§

impl HostBuilder

Source

pub fn new() -> Self

Source

pub fn register<F>(self, f: F) -> Self

Source

pub fn configure<F>(self, f: F) -> Self
where F: FnOnce(&mut HostAppBuilder) + Send + 'static,

Source

pub fn mode(self, mode: AppMode) -> Self

Source

pub fn add_options<T>(self, section: &str) -> Self
where T: for<'de> Deserialize<'de> + Default + Send + Sync + 'static,

自动绑定 appsettings 配置节到 T,并以 Arc<T> 注册到 DI 容器。

参考 ASP.NET Core 的 services.Configure<T>(configuration.GetSection(...)): 框架在 build() 时读取已合并的 appsettings(含 {Mode} overlay 与 env override), 取 section 子节点反序列化为 T,注册为单例供 handler 通过 Arc<T> 注入。

Host::builder()
    .add_options::<SiteConfig>("Site")
    .build()
Source

pub fn use_spa(self, root: impl Into<String>) -> Self

Source

pub fn no_spa(self) -> Self

Explicitly disable SPA, including auto-detection.

By default, the framework auto-detects wwwroot/ under app_base(). Call this when you want a pure API host without SPA fallback, or in tests to isolate framework behavior from filesystem state.

Source

pub fn use_cors(self, config: CorsConfig) -> Self

Source

pub fn add_authentication(self) -> Self

Register JWT authentication service and middleware.

参考 ASP.NET Core 的 services.AddAuthentication() + app.UseAuthentication(): 启用 JWT Bearer Token 认证,中间件自动添加到管道。授权(#[authorize]) 由框架通过编译期元数据自动收集,无需显式调用。

Host::builder()
    .add_authentication()
    .build()
Source

pub fn add_memory_cache(self) -> Self

Register a memory cache instance for use via IDistributedCache trait.

The cache is registered as a singleton in the DI container. Handlers can access it by implementing From<Arc<MemoryCache>>.

Host::builder()
    .add_memory_cache()
    .build()
    .run().await;
Source

pub fn add_memory_cache_with(self, max_entries: usize) -> Self

Register a memory cache with custom configuration.

Source

pub fn use_middleware<T: IMiddleware + Default + 'static>(self) -> Self

Register a middleware by type.

Middleware T is registered as Singleton in the DI container and automatically collected by provider.get_all::<dyn IMiddleware>() during build().

Host::builder()
    .use_middleware::<RequestIdMiddleware>()
    .build()
Source

pub fn use_middleware_with<F>(self, factory: F) -> Self
where F: Fn() -> Arc<dyn IMiddleware> + Send + Sync + 'static,

Register a middleware with a custom factory function.

Use this when the middleware requires constructor parameters:

Host::builder()
    .use_middleware_with(|| {
        Arc::new(RateLimitMiddleware::new(10.0, 20)) as Arc<dyn IMiddleware>
    })
    .build()
Source

pub fn add_health_check<F>(self, name: impl Into<String>, check: F) -> Self
where F: Fn() -> HealthStatus + Send + Sync + 'static,

Register a health check probe.

Health checks are exposed via /health and /health/ready endpoints following RFC 8407 (application/health+json content type).

Host::builder()
    .add_health_check("database", || -> HealthStatus {
        if db_ping() { HealthStatus::pass() } else { HealthStatus::fail("db unreachable") }
    })
    .build()
Source

pub fn build(self) -> Host

Trait Implementations§

Source§

impl Default for HostBuilder

Source§

fn default() -> Self

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

Auto Trait Implementations§

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> IClaimsCarrier for T
where T: Send,

Source§

fn set_claims(&mut self, _claims: Option<Box<dyn IClaims>>)

Default no-op. Overridden by inherent set_claims on types that carry claims.
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