Skip to main content

get

Function get 

Source
pub fn get<I: 'static, R, F>(pattern: impl Into<String>, f: F) -> Route<I, R>
where F: Fn(&mut Cx<'_, I>) -> Result + Send + Sync + 'static,
Expand description

A route with a GET handler (and so HEAD).

Examples found in repository?
examples/hello.rs (lines 20-27)
14fn main() -> Result<(), Box<dyn std::error::Error>> {
15    let app = App::builder()
16        .host("127.0.0.1:8091")
17        .development()
18        .base("/app")
19        .route(
20            get("/", |cx| {
21                let users = cx.url("/users/7");
22                cx.html(
23                    200,
24                    &format!("<h1>Hello</h1><a href=\"{users}\">user 7</a>"),
25                );
26                Ok(())
27            })
28            .public(),
29        )
30        .route(
31            get("/users/:id", |cx| {
32                let id: u64 = cx.param_as("id")?;
33                cx.text(200, &format!("user {id}"));
34                Ok(())
35            })
36            .public(),
37        )
38        .route(
39            Route::at("/contact")
40                .get(|cx| {
41                    cx.html(200, "<form method=post><button>Send</button></form>");
42                    Ok(())
43                })
44                .post(|cx| cx.see_other("/"))
45                .public(),
46        )
47        .build()?;
48
49    let server = serve_net::Server::builder()
50        .listener(serve_net::ListenerSpec::tcp("web", "127.0.0.1:8091"))
51        .serve(app)?;
52    println!("listening on http://127.0.0.1:8091/app/");
53    server.wait();
54    Ok(())
55}