data ServerHandle
A copyable token that can stop a server from a handler or another task (Server.handle()).
Example:
use std.http { Server, ServerConfig, ServerHandle, Router, Request, Reply, Client, Header, HttpConfig, Response, Middleware, Next, withMiddleware, requireHeader, logRequests, timing, WebSocket, WsConfig, SseWriter, BodyWriter, chunks }
fn body(req: Request) -> str {
match req.text() {
str t => t
DecodeError _ => ""
}
}
fn run() -> () | NetError | DecodeError | Panicked net, io, time {
let srv = Server.bind(ServerConfig{ port: 0, followNode: false })?
defer srv.close()
let stop = srv.handle()
let base = "http://127.0.0.1:{srv.port()}"
let router = Router.new().post("/stop", (req: Request) => {
stop.shutdown()
Reply.text("bye")
})
scope {
spawn stop.serve(router)
print((Client.postText("{base}/stop", "")?).text()?)
}?
}
fn main() io, net, time {
match run() {
NetError _ => print("net error")
DecodeError _ => print("decode error")
Panicked _ => print("panicked")
_ => print("stopped")
}
}Вывод
bye stopped
@derive(Copy)
pub(api) data ServerHandleПотокобезопасность. copyable; all methods may be called from any task.
С версии. 1.0.0-beta
Методы
shutdown | ()As Server.shutdown(). |
connections | () -> i64Open connections of the server (idle keep-alive ones included). |
port | () -> u16The port the server listens on. |
serveWith | (handler: Handler) -> () | NetError net, io, timeAs Server.serveWith, from another task (spawn handle.serve(router)). |
serve | (router: Router) -> () | NetError net, io, timeAs Server.serve, from another task (spawn handle.serve(router)). |
Определено в: std/http/http.oxl:4049