Last active
June 4, 2025 21:14
-
-
Save AlperRehaYAZGAN/d9c829860ac99488c258af77bf3cd575 to your computer and use it in GitHub Desktop.
A simple Cloudflare Workers Rust HTTP Router Sample from Official Example: https://github.com/cloudflare/workers-rs/tree/main/examples/router
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "router-on-workers" | |
version = "0.0.1" | |
edition = "2021" | |
[package.metadata.release] | |
release = false | |
[lib] | |
crate-type = ["cdylib"] | |
[dependencies] | |
serde = "1.0.188" | |
worker = { version="0.5.0", features=['http'] } | |
worker-macros = { version="0.5.0", features=['http'] } | |
console_error_panic_hook = { version = "0.1.1" } | |
http = "1.1" | |
serde_json = "1.0.140" | |
[dev-dependencies] | |
tokio = { version = "1", features = ["full"] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/lib.rs | |
use serde::{Deserialize, Serialize}; | |
use serde_json::Value; | |
use worker::*; | |
#[derive(Debug, Deserialize, Serialize)] | |
struct GenericResponse { | |
status: u16, | |
message: String, | |
} | |
#[event(fetch)] | |
async fn main(req: Request, env: Env, _ctx: Context) -> Result<Response> { | |
Router::new().get_async("/", handle_get).run(req, env).await | |
} | |
pub async fn handle_get(req: Request, _ctx: RouteContext<()>) -> worker::Result<Response> { | |
// ?id=1 | |
let url = req.url()?; | |
let id = url | |
.query_pairs() | |
.find(|(key, _)| key == "id") | |
.map(|(_, value)| value.to_string()) | |
.unwrap_or("1".to_string()); | |
// make json placeholder api | |
let mut response = | |
Fetch::Url(format!("https://jsonplaceholder.typicode.com/posts/{}", id).parse()?) | |
.send() | |
.await?; | |
let body = response.text().await?; | |
let json: Value = serde_json::from_str(&body)?; | |
// install serde_json by using cargo add serde_json | |
Response::from_json(&json) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "router-on-workers", | |
"version": "0.0.1", | |
"private": true, | |
"scripts": { | |
"deploy": "wrangler deploy", | |
"dev": "wrangler dev --local" | |
}, | |
"devDependencies": { | |
"wrangler": "^3" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name = "router-on-workers" | |
main = "build/worker/shim.mjs" | |
compatibility_date = "2023-03-22" | |
[build] | |
command = "cargo install worker-build && worker-build --release" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment