pub struct HostBuilder { /* private fields */ }Implementations§
Source§impl HostBuilder
impl HostBuilder
pub fn new() -> Self
pub fn register<F>(self, f: F) -> Self
pub fn configure<F>(self, f: F) -> Self
pub fn mode(self, mode: AppMode) -> Self
Sourcepub fn add_options<T>(self, section: &str) -> Self
pub fn add_options<T>(self, section: &str) -> Self
自动绑定 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()pub fn use_spa(self, root: impl Into<String>) -> Self
Sourcepub fn no_spa(self) -> Self
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.
pub fn use_cors(self, config: CorsConfig) -> Self
Sourcepub fn add_authentication(self) -> Self
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()Sourcepub fn add_memory_cache(self) -> Self
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;Sourcepub fn add_memory_cache_with(self, max_entries: usize) -> Self
pub fn add_memory_cache_with(self, max_entries: usize) -> Self
Register a memory cache with custom configuration.
Sourcepub fn use_middleware<T: IMiddleware + Default + 'static>(self) -> Self
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()Sourcepub fn use_middleware_with<F>(self, factory: F) -> Self
pub fn use_middleware_with<F>(self, factory: F) -> Self
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()Sourcepub fn add_health_check<F>(self, name: impl Into<String>, check: F) -> Self
pub fn add_health_check<F>(self, name: impl Into<String>, check: F) -> Self
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()