๐Ÿ“ฆ heaths / function-rustlang

๐Ÿ“„ main.rs ยท 27 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27// Copyright 2024 Heath Stewart.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

use handler::{default_port, hello};
use std::{collections::HashMap, env, net::Ipv4Addr};
use warp::{http::Response, Filter};

#[tokio::main]
async fn main() {
    let hello_endpoint = warp::get()
        .and(warp::path("api"))
        .and(warp::path("hello"))
        .and(warp::query::<HashMap<String, String>>())
        .map(|p: HashMap<String, String>| {
            let body = hello(p.get("name").map(|name| name.as_str()));
            Response::builder()
                .header("content-type", "text/plain")
                .header("x-powered-by", format!("rustc/{}", env!("RUSTC_VERSION")))
                .body(body)
        });

    let port = default_port().expect("custom handler port number");
    warp::serve(hello_endpoint)
        .run((Ipv4Addr::LOCALHOST, port))
        .await
}