Created
June 20, 2025 06:08
-
-
Save kuc-arc-f/d176d608fbebb64038bcde2e5b37803c to your computer and use it in GitHub Desktop.
workers Rust API-1
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 = "workers-rs-2" | |
version = "0.1.0" | |
edition = "2021" | |
# https://github.com/rustwasm/wasm-pack/issues/1247 | |
[package.metadata.wasm-pack.profile.release] | |
wasm-opt = false | |
[lib] | |
crate-type = ["cdylib"] | |
[dependencies] | |
worker = { version = "0.6.0", features = ["d1"] } | |
serde = { version = "1.0.188", features = ["derive"] } | |
[dev-dependencies] | |
tokio = { version = "1", features = ["full"] } | |
[profile.release] | |
lto = true | |
strip = true | |
codegen-units = 1 |
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
use serde::{Deserialize, Serialize}; | |
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("/foo", handle_get) | |
.post_async("/bar", handle_post) | |
.delete_async("/baz", handle_delete) | |
.run(req, env) | |
.await | |
} | |
pub async fn handle_get(_: Request, _ctx: RouteContext<()>) -> worker::Result<Response> { | |
Response::from_json(&GenericResponse { | |
status: 200, | |
message: "You reached a GET route!".to_string(), | |
}) | |
} | |
pub async fn handle_post(_: Request, _ctx: RouteContext<()>) -> worker::Result<Response> { | |
Response::from_json(&GenericResponse { | |
status: 200, | |
message: "You reached a POST route!".to_string(), | |
}) | |
} | |
pub async fn handle_delete(_: Request, _ctx: RouteContext<()>) -> worker::Result<Response> { | |
Response::from_json(&GenericResponse { | |
status: 200, | |
message: "You reached a DELETE route!".to_string(), | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment