use async_net::TcpListener;
use executor_core::smol::SmolGlobal;
use futures_lite::stream;
use serde::Serialize;
use skyzen::{
hyper::Hyper,
routing::{CreateRouteNode, Route, Router},
utils::Json,
Server,
};
#[derive(Serialize)]
struct StatusResponse {
status: &'static str,
runtime: &'static str,
}
async fn health() -> &'static str {
"OK"
}
async fn status() -> Json<StatusResponse> {
Json(StatusResponse {
status: "running",
runtime: "smol",
})
}
async fn hello() -> &'static str {
"Hello from Skyzen on smol runtime!"
}
fn build_router() -> Router {
Route::new((
"/health".at(health),
"/status".at(status),
"/hello".at(hello),
))
.build()
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
smol::block_on(async {
let router = build_router();
let addr = "127.0.0.1:3000";
let listener = TcpListener::bind(addr).await?;
println!("Embedded Skyzen server listening on http://{addr}");
println!("Using smol runtime to demonstrate multi-runtime support");
println!();
println!("Try these endpoints:");
println!(" curl http://{addr}/health");
println!(" curl http://{addr}/status");
println!(" curl http://{addr}/hello");
let connections = Box::pin(stream::unfold(listener, |listener| async move {
let result = listener.accept().await;
Some((result.map(|(stream, _addr)| stream), listener))
}));
Hyper
.serve(
SmolGlobal,
|err| eprintln!("Connection error: {err}"),
connections,
router,
)
.await;
Ok(())
})
}