url_pieces/
url_pieces.rs

1use vegemite::{
2    http_utils::IntoRawBytes,
3    run, sys,
4    systems::{Endpoint, UrlCollect, UrlPart},
5    Get, IntoResponse, Response, Route,
6};
7
8pub struct User(String);
9
10impl IntoResponse for User {
11    fn response(self) -> vegemite::systems::RawResponse {
12        let bytes = self.0.into_raw_bytes();
13
14        Response::builder()
15            .status(200)
16            .header("Content-Type", "text/html")
17            .header("Content-Length", format!("{}", bytes.len()))
18            .body(bytes)
19            .unwrap()
20    }
21}
22
23// Having `Endpoint` as a parameter after `UrlPart` ensures that there is only one trailing url
24// part in the url` This function will consume the next part regardless so be careful.
25fn user(_g: Get, part: UrlPart, _e: Endpoint) -> User {
26    User(part.0)
27}
28
29fn collect(_g: Get, collect: UrlCollect) -> Option<User> {
30    if let Some(part) = collect.0.into_iter().next() {
31        return Some(User(part));
32    } else {
33        None
34    }
35}
36
37fn main() {
38    let router = Route::empty()
39        .route("user", sys![user])
40        .route("chained", sys![collect]);
41
42    println!("Try connecting on a browser at 'http://localhost:8080/user/USERNAME'");
43
44    run("0.0.0.0:8080", router);
45}