use poem::{handler, http::uri::Uri, listener::TcpListener, Route, Server};
#[derive(clap::Parser)]
pub struct Options {
#[clap(long, env = "HOST", default_value = "127.0.0.1")]
host: String,
#[clap(short = 'p', long, env = "PORT", default_value = "8080")]
port: String,
}
impl Options {
fn host(&self) -> String {
self.host.clone()
}
fn port(&self) -> String {
self.port.clone()
}
fn hostport(&self) -> String {
format!("{}:{}", self.host(), self.port())
}
fn url(&self) -> String {
format!("http://{}", self.hostport())
}
fn addr(&self) -> std::net::SocketAddr {
self.hostport().parse().unwrap()
}
pub async fn run(&mut self) -> Result<(), anyhow::Error> {
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var("RUST_LOG", "poem=debug");
}
let app = Route::new().at("*", hello);
dbg!(self.url());
Ok(Server::new(TcpListener::bind(self.addr()))
.name("hello")
.run(app)
.await?)
}
}
#[handler]
pub fn hello(uri: &Uri) -> String {
let rpath = uri.path();
format!("hello, {}", rpath)
}