Skip to content

Commit bf2b06f

Browse files
samscott89thomasgauvin
authored andcommitted
[Workers] Fix various Rust example formatting issues (#23650)
1 parent 690cb54 commit bf2b06f

File tree

5 files changed

+132
-120
lines changed

5 files changed

+132
-120
lines changed

src/content/docs/workers/examples/basic-auth.mdx

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -260,21 +260,22 @@ export default {
260260
```
261261
262262
</TabItem> <TabItem label="Rust" icon="seti:rust">
263+
263264
```rs
264265
use base64::prelude::*;
265266
use worker::*;
266267

267268
#[event(fetch)]
268269
async fn fetch(req: Request, env: Env, _ctx: Context) -> Result<Response> {
269-
let basic_user = "admin";
270-
// You will need an admin password. This should be
271-
// attached to your Worker as an encrypted secret.
272-
// Refer to https://developers.cloudflare.com/workers/configuration/secrets/
273-
let basic_pass = match env.secret("PASSWORD") {
274-
Ok(s) => s.to_string(),
275-
Err(_) => "password".to_string(),
276-
};
277-
let url = req.url()?;
270+
let basic_user = "admin";
271+
// You will need an admin password. This should be
272+
// attached to your Worker as an encrypted secret.
273+
// Refer to https://developers.cloudflare.com/workers/configuration/secrets/
274+
let basic_pass = match env.secret("PASSWORD") {
275+
Ok(s) => s.to_string(),
276+
Err(_) => "password".to_string(),
277+
};
278+
let url = req.url()?;
278279

279280
match url.path() {
280281
"/" => Response::ok("Anyone can access the homepage."),
@@ -328,10 +329,10 @@ let url = req.url()?;
328329
}
329330
_ => Response::error("Not Found.", 404),
330331
}
331-
332332
}
333333

334-
````
334+
```
335+
335336
</TabItem> <TabItem label="Hono" icon="seti:typescript">
336337
337338
```ts
@@ -346,17 +347,17 @@ import { basicAuth } from "hono/basic-auth";
346347

347348
// Define environment interface
348349
interface Env {
349-
Bindings: {
350+
Bindings: {
350351
USERNAME: string;
351-
PASSWORD: string;
352-
};
352+
PASSWORD: string;
353+
};
353354
}
354355

355356
const app = new Hono<Env>();
356357

357358
// Public homepage - accessible to everyone
358359
app.get("/", (c) => {
359-
return c.text("Anyone can access the homepage.");
360+
return c.text("Anyone can access the homepage.");
360361
});
361362

362363
// Admin route - protected with Basic Auth
@@ -365,19 +366,19 @@ app.get(
365366
async (c, next) => {
366367
const auth = basicAuth({
367368
username: c.env.USERNAME,
368-
password: c.env.PASSWORD
369-
})
369+
password: c.env.PASSWORD,
370+
});
370371

371372
return await auth(c, next);
372373
},
373374
(c) => {
374375
return c.text("🎉 You have private access!", 200, {
375376
"Cache-Control": "no-store",
376377
});
377-
}
378+
},
378379
);
379380

380381
export default app;
381-
````
382+
```
382383
383384
</TabItem> </Tabs>

src/content/docs/workers/examples/cors-header-proxy.mdx

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -583,67 +583,69 @@ async def on_fetch(request):
583583
```
584584

585585
</TabItem> <TabItem label="Rust" icon="seti:rust">
586+
586587
```rs
587588
use std::{borrow::Cow, collections::HashMap};
588589
use worker::*;
589590

590-
fn raw*html_response(html: &str) -> Result<Response> {
591-
Response::from_html(html)
591+
fn raw_html_response(html: &str) -> Result<Response> {
592+
Response::from_html(html)
592593
}
593594
async fn handle_request(req: Request, api_url: &str) -> Result<Response> {
594-
let url = req.url().unwrap();
595-
let mut api_url2 = url
596-
.query_pairs()
597-
.find(|x| x.0 == Cow::Borrowed("apiurl"))
598-
.unwrap()
599-
.1
600-
.to_string();
601-
if api_url2 == String::from("") {
602-
api_url2 = api_url.to_string();
603-
}
604-
let mut request = req.clone_mut()?;
605-
\*request.path_mut()? = api_url2.clone();
606-
if let url::Origin::Tuple(origin, *, _) = Url::parse(&api_url2)?.origin() {
607-
(\*request.headers_mut()?).set("Origin", &origin)?;
608-
}
609-
let mut response = Fetch::Request(request).send().await?.cloned()?;
610-
let headers = response.headers_mut();
611-
if let url::Origin::Tuple(origin, _, \_) = url.origin() {
612-
headers.set("Access-Control-Allow-Origin", &origin)?;
613-
headers.set("Vary", "Origin")?;
614-
}
595+
let url = req.url().unwrap();
596+
let mut api_url2 = url
597+
.query_pairs()
598+
.find(|x| x.0 == Cow::Borrowed("apiurl"))
599+
.unwrap()
600+
.1
601+
.to_string();
602+
if api_url2 == String::from("") {
603+
api_url2 = api_url.to_string();
604+
}
605+
let mut request = req.clone_mut()?;
606+
*request.path_mut()? = api_url2.clone();
607+
if let url::Origin::Tuple(origin, _, _) = Url::parse(&api_url2)?.origin() {
608+
(*request.headers_mut()?).set("Origin", &origin)?;
609+
}
610+
let mut response = Fetch::Request(request).send().await?.cloned()?;
611+
let headers = response.headers_mut();
612+
if let url::Origin::Tuple(origin, _, _) = url.origin() {
613+
headers.set("Access-Control-Allow-Origin", &origin)?;
614+
headers.set("Vary", "Origin")?;
615+
}
615616

616617
Ok(response)
617-
618618
}
619619

620-
fn handle*options(req: Request, cors_headers: &HashMap<&str, &str>) -> Result<Response> {
621-
let headers: Vec<*> = req.headers().keys().collect();
622-
if [
623-
"access-control-request-method",
624-
"access-control-request-headers",
625-
"origin",
626-
]
627-
.iter()
628-
.all(|i| headers.contains(&i.to_string()))
629-
{
630-
let mut headers = Headers::new();
631-
for (k, v) in cors_headers.iter() {
632-
headers.set(k, v)?;
633-
}
634-
return Ok(Response::empty()?.with_headers(headers));
620+
fn handle_options(req: Request, cors_headers: &HashMap<&str, &str>) -> Result<Response> {
621+
let headers: Vec<_> = req.headers().keys().collect();
622+
if [
623+
"access-control-request-method",
624+
"access-control-request-headers",
625+
"origin",
626+
]
627+
.iter()
628+
.all(|i| headers.contains(&i.to_string()))
629+
{
630+
let mut headers = Headers::new();
631+
for (k, v) in cors_headers.iter() {
632+
headers.set(k, v)?;
633+
}
634+
return Ok(Response::empty()?.with_headers(headers));
635+
}
636+
Response::empty()
635637
}
636-
Response::empty()
637-
} #[event(fetch)]
638-
async fn fetch(req: Request, \_env: Env, \_ctx: Context) -> Result<Response> {
639-
let cors_headers = HashMap::from([
640-
("Access-Control-Allow-Origin", "*"),
641-
("Access-Control-Allow-Methods", "GET,HEAD,POST,OPTIONS"),
642-
("Access-Control-Max-Age", "86400"),
643-
]);
644-
let api_url = "https://examples.cloudflareworkers.com/demos/demoapi";
645-
let proxy_endpoint = "/corsproxy/";
646-
let demo_page = format!(
638+
639+
#[event(fetch)]
640+
async fn fetch(req: Request, _env: Env, _ctx: Context) -> Result<Response> {
641+
let cors_headers = HashMap::from([
642+
("Access-Control-Allow-Origin", "*"),
643+
("Access-Control-Allow-Methods", "GET,HEAD,POST,OPTIONS"),
644+
("Access-Control-Max-Age", "86400"),
645+
]);
646+
let api_url = "https://examples.cloudflareworkers.com/demos/demoapi";
647+
let proxy_endpoint = "/corsproxy/";
648+
let demo_page = format!(
647649
r#"
648650
649651
<!DOCTYPE html>
@@ -696,7 +698,7 @@ return response.json()
696698
</body>
697699
</html>
698700
"#
699-
);
701+
);
700702

701703
if req.url()?.path().starts_with(proxy_endpoint) {
702704
match req.method() {
@@ -708,7 +710,6 @@ return response.json()
708710
raw_html_response(&demo_page)
709711

710712
}
711-
712713
```
713714
</TabItem> </Tabs>
714-
```
715+

src/content/docs/workers/examples/logging-headers.mdx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,17 @@ async def on_fetch(request):
5959
```
6060

6161
</TabItem> <TabItem label="Rust" icon="seti:rust">
62+
6263
```rs
6364
use worker::*;
6465

6566
#[event(fetch)]
66-
async fn fetch(req: HttpRequest, \_env: Env, \_ctx: Context) -> Result<Response> {
67-
console_log!("{:?}", req.headers());
68-
Response::ok("hello world")
67+
async fn fetch(req: HttpRequest, _env: Env, _ctx: Context) -> Result<Response> {
68+
console_log!("{:?}", req.headers());
69+
Response::ok("hello world")
6970
}
71+
```
7072

71-
````
7273
</TabItem> <TabItem label="Hono" icon="seti:typescript">
7374

7475
```ts
@@ -98,7 +99,7 @@ app.get('*', (c) => {
9899
});
99100

100101
export default app;
101-
````
102+
```
102103

103104
</TabItem> </Tabs>
104105

src/content/docs/workers/examples/security-headers.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ async def on_fetch(request):
258258
```
259259

260260
</TabItem> <TabItem label="Rust" icon="seti:rust">
261+
261262
```rs
262263
use std::collections::HashMap;
263264
use worker::*;
@@ -329,7 +330,8 @@ async fn fetch(req: Request, _env: Env, _ctx: Context) -> Result<Response> {
329330

330331
}
331332

332-
````
333+
```
334+
333335
</TabItem> <TabItem label="Hono" icon="seti:typescript">
334336

335337
```ts

0 commit comments

Comments
 (0)