-
-
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 a,b,g,h prefixed examples with code comments (…
- Loading branch information
Showing
9 changed files
with
132 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,30 @@ | ||
use salvo::prelude::*; | ||
|
||
// Handler function that returns "Hello World" for any request | ||
#[handler] | ||
async fn hello() -> &'static str { | ||
"Hello World" | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// Initialize logging | ||
tracing_subscriber::fmt().init(); | ||
|
||
// Create a router and register the hello handler | ||
let mut router = Router::new().get(hello); | ||
|
||
// Set up a TCP listener on port 443 for HTTPS | ||
let listener = TcpListener::new("0.0.0.0:443") | ||
.acme() | ||
.cache_path("temp/letsencrypt") | ||
.add_domain("test.salvo.rs") // Replace this domain name with your own. | ||
.http01_challenge(&mut router) | ||
.quinn("0.0.0.0:443"); | ||
.acme() // Enable ACME for automatic SSL certificate management | ||
.cache_path("temp/letsencrypt") // Path to store the certificate cache | ||
.add_domain("test.salvo.rs") // replace with your domain | ||
.http01_challenge(&mut router) // Add routes to handle ACME challenge requests | ||
.quinn("0.0.0.0:443"); // Enable QUIC/HTTP3 support on the same port | ||
|
||
// Create an acceptor that listens on both port 80 (HTTP) and port 443 (HTTPS) | ||
let acceptor = listener.join(TcpListener::new("0.0.0.0:80")).bind().await; | ||
|
||
// Start the server with the configured acceptor and router | ||
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,21 +1,32 @@ | ||
use salvo::prelude::*; | ||
|
||
// This handler function responds with "Hello World" to any incoming request | ||
#[handler] | ||
async fn hello() -> &'static str { | ||
"Hello World" | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// Initialize the tracing subscriber for logging | ||
tracing_subscriber::fmt().init(); | ||
|
||
// Create a new router and register the hello handler | ||
let mut router = Router::new().get(hello); | ||
|
||
// Set up a TCP listener on port 443 for HTTPS | ||
let listener = TcpListener::new("0.0.0.0:443") | ||
.acme() | ||
.acme() // Enable ACME for automatic SSL certificate management | ||
// Use Let's Encrypt production server by default | ||
// Uncomment the following line to use the staging server for testing purposes | ||
// .directory("letsencrypt", salvo::conn::acme::LETS_ENCRYPT_STAGING) | ||
.cache_path("/temp/letsencrypt") | ||
.add_domain("test.salvo.rs") // Replace this domain name with your own. | ||
.http01_challenge(&mut router); | ||
.cache_path("/temp/letsencrypt") // Specify the path to store the certificate cache | ||
.add_domain("test.salvo.rs") // replace with your domain | ||
.http01_challenge(&mut router); // Add routes to handle ACME challenge requests | ||
|
||
// Create an acceptor that listens on both port 80 (HTTP) and port 443 (HTTPS) | ||
let acceptor = listener.join(TcpListener::new("0.0.0.0:80")).bind().await; | ||
|
||
// Start the server with the configured acceptor and router | ||
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,20 +1,27 @@ | ||
use salvo::prelude::*; | ||
|
||
// This handler function responds with "Hello World" to any incoming request | ||
#[handler] | ||
async fn hello() -> &'static str { | ||
"Hello World" | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// Initialize the tracing subscriber for logging | ||
tracing_subscriber::fmt().init(); | ||
|
||
// Create a new router and register the hello handler | ||
let router = Router::new().get(hello); | ||
|
||
// Set up a TCP listener on port 443 for HTTPS | ||
let acceptor = TcpListener::new("0.0.0.0:443") | ||
.acme() | ||
// .cache_path("temp/letsencrypt") | ||
.add_domain("test.salvo.rs") // Replace this domain name with your own. | ||
.acme() // Enable ACME for automatic SSL certificate management | ||
// .cache_path("temp/letsencrypt") // Specify the path to store the certificate cache (uncomment if needed) | ||
.add_domain("test.salvo.rs") // Replace this domain name with your own | ||
.bind() | ||
.await; | ||
|
||
// Start the server with the configured acceptor and router | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,31 @@ | ||
use salvo::prelude::*; | ||
|
||
// Handler demonstrating streaming response using response channel | ||
#[handler] | ||
async fn hello(res: &mut Response) { | ||
// Set response content type to plain text | ||
res.add_header("content-type", "text/plain", true).unwrap(); | ||
|
||
// Create a channel for streaming response data | ||
let mut tx = res.channel(); | ||
|
||
// Spawn async task to send data through the channel | ||
tokio::spawn(async move { | ||
tx.send_data("Hello world").await.unwrap(); | ||
}); | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// Initialize logging subsystem | ||
tracing_subscriber::fmt().init(); | ||
|
||
// Bind server to port 5800 | ||
let acceptor = TcpListener::new("0.0.0.0:5800").bind().await; | ||
|
||
// Create router with single endpoint | ||
let router = Router::new().get(hello); | ||
|
||
// Start serving requests | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,44 @@ | ||
use salvo::conn::rustls::{Keycert, RustlsConfig}; | ||
use salvo::prelude::*; | ||
|
||
// Handler function responding with "Hello World" for HTTP/3 requests | ||
#[handler] | ||
async fn hello() -> &'static str { | ||
"Hello World" | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// Initialize logging system | ||
tracing_subscriber::fmt().init(); | ||
|
||
// Load TLS certificate and private key from embedded PEM files | ||
let cert = include_bytes!("../certs/cert.pem").to_vec(); | ||
let key = include_bytes!("../certs/key.pem").to_vec(); | ||
|
||
// Create router with single endpoint | ||
let router = Router::new().get(hello); | ||
let config = RustlsConfig::new(Keycert::new().cert(cert.as_slice()).key(key.as_slice())); | ||
let listener = TcpListener::new(("0.0.0.0", 5800)).rustls(config.clone()); | ||
|
||
// Configure TLS settings using Rustls | ||
let config = RustlsConfig::new( | ||
Keycert::new() | ||
.cert(cert.as_slice()) | ||
.key(key.as_slice()) | ||
); | ||
|
||
let acceptor = QuinnListener::new(config.build_quinn_config().unwrap(), ("0.0.0.0", 5800)) | ||
// Create TCP listener with TLS encryption on port 5800 | ||
let listener = TcpListener::new(("0.0.0.0", 5800)) | ||
.rustls(config.clone()); | ||
|
||
// Create QUIC listener and combine with TCP listener | ||
let acceptor = QuinnListener::new( | ||
config.build_quinn_config().unwrap(), | ||
("0.0.0.0", 5800) | ||
) | ||
.join(listener) | ||
.bind() | ||
.await; | ||
|
||
// Start server supporting both HTTP/3 (QUIC) and HTTPS (TCP) | ||
Server::new(acceptor).serve(router).await; | ||
} |
Oops, something went wrong.