-
-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(examples): Annotate c,d prefixed examples with code comments (#1057
) * docs(examples): Annotate a,b,g,h prefixed examples with code comments * docs(examples): Annotate c,d,gprefixed examples with code comments
- Loading branch information
Showing
26 changed files
with
347 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,23 @@ | ||
use salvo::prelude::*; | ||
|
||
// Handler that returns a simple "Hello World" response | ||
#[handler] | ||
async fn hello() -> &'static str { | ||
"Hello World" | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// Initialize logging system | ||
tracing_subscriber::fmt().init(); | ||
|
||
// CachingHeader must be before Compression. | ||
// Set up router with caching headers and compression middleware | ||
// CachingHeader must be before Compression to properly set cache control headers | ||
let router = Router::with_hoop(CachingHeaders::new()) | ||
.hoop(Compression::new().min_length(0)) | ||
.hoop(Compression::new().min_length(0)) // Enable compression for all responses | ||
.get(hello); | ||
|
||
// Bind server to port 5800 and start serving | ||
let acceptor = TcpListener::new("0.0.0.0:5800").bind().await; | ||
Server::new(acceptor).serve(router).await; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,51 @@ | ||
use salvo::prelude::*; | ||
|
||
// Custom error type for demonstration | ||
struct CustomError; | ||
|
||
// Implement Writer trait for CustomError to customize error response | ||
#[async_trait] | ||
impl Writer for CustomError { | ||
async fn write(self, _req: &mut Request, _depot: &mut Depot, res: &mut Response) { | ||
// Set response status code to 500 and custom error message | ||
res.status_code(StatusCode::INTERNAL_SERVER_ERROR); | ||
res.render("custom error"); | ||
} | ||
} | ||
|
||
// Handler that returns an anyhow error for testing error handling | ||
#[handler] | ||
async fn handle_anyhow() -> Result<(), anyhow::Error> { | ||
Err(anyhow::anyhow!("handled anyhow error")) | ||
} | ||
|
||
// Handler that returns an eyre error for testing error handling | ||
#[handler] | ||
async fn handle_eyre() -> eyre::Result<()> { | ||
Err(eyre::Report::msg("handled eyre error")) | ||
} | ||
|
||
// Handler that returns our custom error type | ||
#[handler] | ||
async fn handle_custom() -> Result<(), CustomError> { | ||
Err(CustomError) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// Initialize logging system | ||
tracing_subscriber::fmt().init(); | ||
|
||
// Set up router with three error handling endpoints: | ||
// - /anyhow : demonstrates anyhow error handling | ||
// - /eyre : demonstrates eyre error handling | ||
// - /custom : demonstrates custom error handling | ||
let router = Router::new() | ||
.push(Router::with_path("anyhow").get(handle_anyhow)) | ||
.push(Router::with_path("eyre").get(handle_eyre)) | ||
.push(Router::with_path("custom").get(handle_custom)); | ||
|
||
// Bind server to port 5800 and start serving | ||
let acceptor = TcpListener::new("0.0.0.0:5800").bind().await; | ||
Server::new(acceptor).serve(router).await; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
use salvo::prelude::*; | ||
|
||
// Handler that deliberately panics to demonstrate panic catching | ||
#[handler] | ||
async fn hello() { | ||
panic!("panic error!"); | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// Initialize logging system | ||
tracing_subscriber::fmt().init(); | ||
|
||
// Set up router with CatchPanic middleware to handle panics gracefully | ||
// This prevents the server from crashing when a panic occurs in a handler | ||
let router = Router::new().hoop(CatchPanic::new()).get(hello); | ||
|
||
// Bind server to port 5800 and start serving | ||
let acceptor = TcpListener::new("0.0.0.0:5800").bind().await; | ||
Server::new(acceptor).serve(router).await; | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.