pub struct Server { /* private fields */ }Expand description
A reusable HTTP runtime server.
This type is experimental and its API may change without notice.
The server binds a TCP listener, accepts connections, and dispatches them
to a Service implementation. It owns the full connection lifecycle:
parsing, normalization, timeouts, connection tracking, and graceful shutdown.
§Example
use eggserve_core::server::{Server, RuntimeConfig, service_fn, Request};
use eggserve_core::primitives::canonical::{Response, StatusCode, ResponseBody};
let server = Server::builder()
.runtime(RuntimeConfig::builder()
.bind("127.0.0.1:8000".parse().unwrap())
.build()?)
.build()?;
let handle = server.start_with_service(service_fn(|_req: Request| async {
Ok(Response::builder()
.status(StatusCode::OK)
.body(ResponseBody::Bytes(b"hello".to_vec()))
.unwrap())
})).await?;
handle.ready().await?;
println!("listening on {}", handle.local_addr());
// ... serve requests ...
handle.shutdown();
handle.wait().await?;Implementations§
Source§impl Server
impl Server
Sourcepub fn builder() -> ServerBuilder
pub fn builder() -> ServerBuilder
Create a new server builder with default configuration.
Source§impl Server
impl Server
Sourcepub async fn start(self) -> Result<ServerHandle, ServerError>
pub async fn start(self) -> Result<ServerHandle, ServerError>
Start the server with the built-in static file service.
Starts the statically constructed service using the shared generic
accept loop. The serve config must have been set via
ServerBuilder::serve_config.
Sourcepub async fn start_with_service<S: Service>(
self,
service: S,
) -> Result<ServerHandle, ServerError>
pub async fn start_with_service<S: Service>( self, service: S, ) -> Result<ServerHandle, ServerError>
Start the server with a custom service.
The custom service does not require a static root or serve configuration. The runtime creates only transport state (semaphores, lifecycle) and passes it to the accept loop and connection pipeline.