Chihwei
Chihwei
CDCloudflare Developers
Created by Chihwei on 6/18/2024 in #workers-help
worker-rs: http hello world
@Jeroen
fn router() -> Router {
Router::new()
.route("/", get(root))
.route("/zhiwei", get(zhiwei))
}

#[event(fetch)]
async fn fetch(
req: HttpRequest,
env: Env,
ctx: Context,
) -> WorkerResult<axum::http::Response<axum::body::Body>> {
console_error_panic_hook::set_once();
Ok(router().call(req).await?)
}

pub async fn root() -> &'static str {
"Hello World!"
}

pub async fn zhiwei() -> &'static str {
"Hello Zhiwei!"
}
fn router() -> Router {
Router::new()
.route("/", get(root))
.route("/zhiwei", get(zhiwei))
}

#[event(fetch)]
async fn fetch(
req: HttpRequest,
env: Env,
ctx: Context,
) -> WorkerResult<axum::http::Response<axum::body::Body>> {
console_error_panic_hook::set_once();
Ok(router().call(req).await?)
}

pub async fn root() -> &'static str {
"Hello World!"
}

pub async fn zhiwei() -> &'static str {
"Hello Zhiwei!"
}
This is work I got, but I can't guarantee it's the most ideal way
8 replies
CDCloudflare Developers
Created by Chihwei on 6/18/2024 in #workers-help
worker-rs: http hello world
Thanks! Most of those examples are not with http feature flag turned on, which they are using older Response defined in worker-rs, rather than more widely used rust http crate. I later found I could change the wrapped type of Result to assign it a string, like Result<String>. I am a beginner of Rust, so let me know if I was wrong on anything.
8 replies