use cebolla::*;

use poem::{EndpointExt, Route, Server, get, handler, middleware::Tracing, web::Path};
use snafu::{ResultExt, Whatever};

#[handler]
fn hello(Path(name): Path<String>) -> String {
    format!("hello: {name}")
}

#[tokio::main]
async fn main() -> Result<(), Whatever> {
    println!("Connecting to tor network");
    let tor = Tor::new(TorConfig::default())
        .await
        .whatever_context("unable to connect to tor network")?;
    println!("Connected!");
    let service = tor
        .service("hello")
        .whatever_context("unable to create service")?;

    println!(
        "Onion address: {}",
        service.onion_address().unwrap().display_unredacted()
    );

    let app = Route::new().at("/hello/:name", get(hello)).with(Tracing);
    Server::new(service)
        .name("hello-world")
        .run(app)
        .await
        .expect("server error");
    Ok(())
}
