Skip to content

Commit

Permalink
Add more example code
Browse files Browse the repository at this point in the history
  • Loading branch information
kpcyrd committed Feb 20, 2024
1 parent e7ccf3c commit 49d39de
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 3 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ wasm-opt = false
crate-type = ["cdylib"]

[dependencies]
html-escape = "0.2.13"
worker = "0.0.18"

[profile.release]
Expand Down
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,63 @@ wrangler dev
- [worker-rs Documentation](https://docs.rs/worker)
- [worker-rs Repository](https://github.com/cloudflare/workers-rs)

```rust
use worker::*;

/// Configure `wrangler.toml` to run this function periodically
///
/// Can be tested with `curl -v "http://localhost:8787/__scheduled?cron=*+*+*+*+*"`
/// However you need to use `wrangler dev --test-scheduled`
#[event(scheduled)]
async fn scheduled(event: ScheduledEvent, _env: Env, _ctx: ScheduleContext) {
console_debug!("cronjob: {event:?}");
}

/// This function handles all requests
#[event(fetch)]
async fn main(req: Request, env: Env, _ctx: Context) -> Result<Response> {
let router = Router::new();

router
.get_async("/", |req, _ctx| async move {
let mut response = Response::from_html(format!(
"<!DOCTYPE html>
<html>
<head>
<title>Hello, World! 🐱</title>
</head>
<body>
<h1>🎷🐛</h1>
<pre>{}</pre>
</body>
</html>
",
html_escape::encode_text(&format!("{req:#?}"))
))?;
// https://github.com/cloudflare/workers-rs/pull/447
response
.headers_mut()
.set("Content-Type", "text/html; charset=utf-8")?;
Ok(response)
})
.post_async("/upload", file_upload)
.run(req, env)
.await
}

/// Request handler for `/upload`
async fn file_upload(mut req: Request, _ctx: RouteContext<()>) -> Result<Response> {
let Some(file) = req.form_data().await?.get("file") else {
return Response::error("Bad Request", 400);
};
let FormEntry::File(buf) = file else {
return Response::error("`file` part of POST form must be a file", 400);
};

Response::ok(format!("size = {}\n", buf.bytes().await?.len()))
}
```

<a href="https://repology.org/project/wrangler/versions"><img align="right" src="https://repology.org/badge/vertical-allrepos/wrangler.svg" alt="Packaging status"></a>

## Using `npx wrangler`
Expand Down
53 changes: 50 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,56 @@
use worker::*;

/// Configure `wrangler.toml` to run this function periodically
///
/// Can be tested with `curl -v "http://localhost:8787/__scheduled?cron=*+*+*+*+*"`
/// However you need to use `wrangler dev --test-scheduled`
#[event(scheduled)]
async fn scheduled(event: ScheduledEvent, _env: Env, _ctx: ScheduleContext) {
console_debug!("cronjob: {event:?}");
}

/// This function handles all requests
#[event(fetch)]
async fn main(req: Request, env: Env, ctx: Context) -> Result<Response> {
console_debug!("Received request: {req:?}");
Response::ok("Hello, World!")
async fn main(req: Request, env: Env, _ctx: Context) -> Result<Response> {
let router = Router::new();

router
.get_async("/", |req, _ctx| async move {
let mut response = Response::from_html(format!(
"<!DOCTYPE html>
<html>
<head>
<title>Hello, World! 🐱</title>
</head>
<body>
<h1>🎷🐛</h1>
<pre>{}</pre>
</body>
</html>
",
html_escape::encode_text(&format!("{req:#?}"))
))?;
// https://github.com/cloudflare/workers-rs/pull/447
response
.headers_mut()
.set("Content-Type", "text/html; charset=utf-8")?;
Ok(response)
})
.post_async("/upload", file_upload)
.run(req, env)
.await
}

/// Request handler for `/upload`
async fn file_upload(mut req: Request, _ctx: RouteContext<()>) -> Result<Response> {
let Some(file) = req.form_data().await?.get("file") else {
return Response::error("Bad Request", 400);
};
let FormEntry::File(buf) = file else {
return Response::error("`file` part of POST form must be a file", 400);
};

Response::ok(format!("size = {}\n", buf.bytes().await?.len()))
}

#[cfg(test)]
Expand Down
3 changes: 3 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name = "worker-rust"
main = "build/worker/shim.mjs"
compatibility_date = "2023-06-28"

#[triggers]
#crons = ["0 * * * *"]

[build]
# imo this should be provided by `npx wrangler`
command = "(command -v worker-build >/dev/null || cargo install -q worker-build) && worker-build --release"

0 comments on commit 49d39de

Please sign in to comment.