use crate::ws_middleware::ws_middleware;
use poem::{
handler, http::uri::Uri, listener::TcpListener, EndpointExt, IntoResponse, 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 ws_url(&self) -> String {
format!("ws://{}/ws", self.hostport())
}
fn addr(&self) -> std::net::SocketAddr {
self.hostport().parse().unwrap()
}
pub async fn run(&mut self) -> Result<(), anyhow::Error> {
let app = Route::new().at("*", hello).around(ws_middleware);
dbg!(self.url());
dbg!(self.ws_url());
Ok(Server::new(TcpListener::bind(self.addr()))
.name("hello")
.run(app)
.await?)
}
}
#[handler]
pub fn hello(uri: &Uri) -> impl IntoResponse {
let rpath = uri.path();
return format!("hello, {}", rpath);
}