Skip to content

Commit

Permalink
docs(web): add document for example
Browse files Browse the repository at this point in the history
  • Loading branch information
zavakid committed Dec 10, 2024
1 parent a3b314f commit 94093f4
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion crates/web/examples/hello_world.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@
//! Basic example demonstrating how to create a simple web server using micro_web.
//! This example shows:
//! - How to define route handlers
//! - How to set up a router with middleware
//! - How to configure and start a server
use micro_web::wrapper::DateWrapper;
use micro_web::router::{get, Router};
use micro_web::{handler_fn, Server};


/// A simple handler that returns "hello world"
async fn hello_world() -> &'static str {
"hello world"
}

/// Default handler for unmatched handlers (404 responses)
///
/// This handler is called when no other handlers match the incoming request
async fn default_handler() -> &'static str {
"404 not found"
}

#[tokio::main]
async fn main() {
// Create a new router using the builder
let router = Router::builder()
// Add a route that matches GET requests to the root path "/"
// handler_fn converts our async function into a handler
.route("/", get(handler_fn(hello_world)))
// Add middleware that will add date headers to responses
.wrap(DateWrapper)
.build();

// Configure and start the server
Server::builder()
// Attach our router to handle incoming requests
.router(router)
// Set the address and port to listen on
.bind("127.0.0.1:3000")
// Set a handler for requests that don't match any routes
.default_handler(handler_fn(default_handler))
// Build the server
.build()
.unwrap()
// Start the server and wait for it to finish
.start()
.await;
}

0 comments on commit 94093f4

Please sign in to comment.