use skyzen::extract::Path;
use skyzen::routing::{CreateRouteNode, Route, Router};
#[cfg(target_arch = "wasm32")]
use skyzen::runtime::CfProperties;
use skyzen::runtime::WorkerContext;
use skyzen::Result as SkyResult;
async fn health() -> &'static str {
"OK"
}
async fn root() -> &'static str {
"Hello from Skyzen running at the edge!"
}
async fn greet(Path(name): Path<String>) -> String {
format!("Hello, {name}!")
}
async fn accept_work(context: WorkerContext) -> SkyResult<&'static str> {
context.wait_until(async {
tracing::info!("post-response work finished after the response was returned");
})?;
Ok("accepted")
}
#[cfg(target_arch = "wasm32")]
async fn where_am_i(cf: CfProperties) -> String {
format!(
"colo={} country={} tls={}",
cf.colo.as_deref().unwrap_or("unknown"),
cf.country.as_deref().unwrap_or("unknown"),
cf.tls_version.as_deref().unwrap_or("unknown"),
)
}
#[cfg(not(target_arch = "wasm32"))]
async fn where_am_i() -> &'static str {
"request.cf is a Cloudflare value and this build is not running on Workers"
}
fn build_router() -> Router {
Route::new((
"/".at(root),
"/health".at(health),
"/hello".route(("/{name}".at(greet),)),
"/readyz".at(|| async { "ready" }),
"/track".at(accept_work),
"/where-am-i".at(where_am_i),
))
.build()
}
#[skyzen::main]
fn worker() -> Router {
build_router()
}
#[cfg(target_arch = "wasm32")]
fn main() {}