poems/
hello_tracing.rs

1use poem::{
2    handler, http::uri::Uri, listener::TcpListener, middleware::Tracing, EndpointExt, Route, Server,
3};
4
5#[derive(clap::Parser)]
6pub struct Options {
7    #[clap(long, env = "HOST", default_value = "127.0.0.1")]
8    /// Set host
9    host: String,
10    #[clap(short = 'p', long, env = "PORT", default_value = "8080")]
11    /// Set listening port
12    port: String,
13}
14
15impl Options {
16    fn host(&self) -> String {
17        self.host.clone()
18    }
19    fn port(&self) -> String {
20        self.port.clone()
21    }
22    fn hostport(&self) -> String {
23        format!("{}:{}", self.host(), self.port())
24    }
25    fn url(&self) -> String {
26        format!("http://{}", self.hostport())
27    }
28    fn addr(&self) -> std::net::SocketAddr {
29        self.hostport().parse().unwrap()
30    }
31    pub async fn run(&mut self) -> Result<(), anyhow::Error> {
32        tracing_subscriber::fmt::init();
33        let app = Route::new().at("*", hello).with(Tracing);
34        dbg!(self.url());
35        Ok(Server::new(TcpListener::bind(self.addr()))
36            .name("hello")
37            .run(app)
38            .await?)
39    }
40}
41
42#[handler]
43pub fn hello(uri: &Uri) -> String {
44    let rpath = uri.path();
45    format!("hello, {}", rpath)
46}