Skip to main content

text

Function text 

Source
pub fn text(body: impl Into<String>) -> Response
Expand description

Build a 200 OK response with text/plain content type.

Examples found in repository?
examples/homepage.rs (line 59)
50async fn main() -> std::io::Result<()> {
51    let addr: SocketAddr = ([127, 0, 0, 1], 3000).into();
52    let router = with_defaults(Router::new())
53        .get("/whoami", |req, _params| async move {
54            let id = req
55                .ctx()
56                .get::<RequestId>()
57                .map(|r| r.0.to_string())
58                .unwrap_or_else(|| "unknown".into());
59            Ok::<Response, Error>(text(format!("your request id is req-{id}\n")))
60        })
61        .get("/me", |req, _params| async move {
62            let id = require_auth(req.ctx())?;
63            Ok::<Response, Error>(text(format!("hello {}\n", id.user_id)))
64        })
65        .get("/admin-only", |req, _params| async move {
66            let id = require_admin(req.ctx())?;
67            Ok::<Response, Error>(text(format!("hello admin {}\n", id.user_id)))
68        })
69        .get("/crash", |_req, _params| async {
70            Err::<Response, Error>(Error::Internal("simulated failure".into()))
71        })
72        .get("/unauth", |_req, _params| async {
73            Err::<Response, Error>(Error::Unauthorized)
74        })
75        .wrap(request_id)
76        .wrap(authenticate)
77        .wrap(logger);
78    Server::bind(addr).serve_router(router).await
79}