boitalettres/examples/simple.rs

40 lines
1.1 KiB
Rust
Raw Normal View History

use miette::{IntoDiagnostic, Result};
2022-05-23 01:20:06 +00:00
2022-05-09 17:15:52 +00:00
use boitalettres::proto::{Request, Response};
use boitalettres::server::accept::addr::{AddrIncoming, AddrStream};
use boitalettres::server::Server;
2022-05-19 08:18:25 +00:00
use tracing_subscriber;
2022-05-09 17:15:52 +00:00
async fn handle_req(req: Request) -> Result<Response> {
2022-05-23 01:20:06 +00:00
use imap_codec::types::response::{Capability, Data};
2022-05-09 17:15:52 +00:00
tracing::debug!("Got request: {:#?}", req);
2022-05-23 01:20:06 +00:00
let capabilities = vec![Capability::Imap4Rev1, Capability::Idle];
let body = vec![Data::Capability(capabilities)];
Ok(Response::ok("Done")?.with_body(body))
2022-05-09 17:15:52 +00:00
}
#[tokio::main]
async fn main() -> eyre::Result<()> {
2022-05-19 08:18:25 +00:00
tracing_subscriber::fmt::init();
2022-05-09 17:15:52 +00:00
let incoming = AddrIncoming::new("127.0.0.1:4567")
.await
.into_diagnostic()?;
2022-05-09 17:15:52 +00:00
let make_service = tower::service_fn(|addr: &AddrStream| {
tracing::debug!(remote_addr = %addr.remote_addr, local_addr = %addr.local_addr, "accept");
2022-05-20 14:29:43 +00:00
let service = tower::ServiceBuilder::new().service_fn(handle_req);
futures::future::ok::<_, std::convert::Infallible>(service)
2022-05-09 17:15:52 +00:00
});
let server = Server::new(incoming).serve(make_service);
server.await.map_err(Into::into)
2022-05-09 17:15:52 +00:00
}