hello_world/hello_world.rs
1//! Hello World Example - Minimal fastapi_rust Application
2//!
3//! This example demonstrates the most basic fastapi_rust setup:
4//! - Creating an application with a single GET endpoint
5//! - Defining a simple request handler
6//! - Testing with the built-in TestClient
7//!
8//! # Running This Example
9//!
10//! ```bash
11//! cargo run --example hello_world
12//! ```
13//!
14//! # Expected Output
15//!
16//! ```text
17//! GET / -> 200 OK
18//! Response: Hello, World!
19//! ```
20
21// Import the core types from fastapi
22use fastapi::core::{
23 App, // The application builder and container
24 Request, // Incoming HTTP request
25 RequestContext, // Request context (contains Cx, request ID, etc.)
26 Response, // HTTP response to send back
27 ResponseBody, // Response body types
28 TestClient, // Built-in test client for making requests
29};
30
31/// A simple request handler that returns "Hello, World!"
32///
33/// # Parameters
34///
35/// - `_ctx`: The request context (unused in this simple example)
36/// - `_req`: The incoming request (unused - we return the same response for any request)
37///
38/// # Returns
39///
40/// An HTTP 200 OK response with "Hello, World!" as the body.
41///
42/// # Note
43///
44/// Handlers in fastapi_rust are functions that take a RequestContext
45/// and mutable Request, and return a Response. They can be sync or async.
46fn hello_handler(_ctx: &RequestContext, _req: &mut Request) -> std::future::Ready<Response> {
47 // Create a 200 OK response with a plain text body
48 std::future::ready(Response::ok().body(ResponseBody::Bytes(b"Hello, World!".to_vec())))
49}
50
51fn main() {
52 println!("fastapi_rust Hello World Example");
53 println!("================================\n");
54
55 // Build the application
56 //
57 // App::builder() creates a new application builder that lets you:
58 // - Add routes for different HTTP methods
59 // - Configure middleware
60 // - Set application state
61 // - Define exception handlers
62 let app = App::builder()
63 // Register a GET handler for the root path "/"
64 //
65 // This is equivalent to:
66 // @app.get("/")
67 // def hello():
68 // return "Hello, World!"
69 // in Python FastAPI
70 .get("/", hello_handler)
71 // Build the final immutable App
72 .build();
73
74 println!("App created with {} route(s)\n", app.route_count());
75
76 // Create a test client to make requests to our app
77 //
78 // TestClient wraps any Handler (including App) and provides
79 // a convenient API for making HTTP requests in tests.
80 let client = TestClient::new(app);
81
82 // Make a GET request to "/"
83 println!("Making request: GET /");
84 let response = client.get("/").send();
85
86 // Check the response
87 println!(
88 "GET / -> {} {}",
89 response.status().as_u16(),
90 response.status().canonical_reason()
91 );
92 println!("Response: {}\n", response.text());
93
94 // Verify success
95 assert_eq!(response.status().as_u16(), 200);
96 assert_eq!(response.text(), "Hello, World!");
97
98 // Try a path that doesn't exist - should get 404
99 println!("Making request: GET /not-found");
100 let response = client.get("/not-found").send();
101 println!(
102 "GET /not-found -> {} {}",
103 response.status().as_u16(),
104 response.status().canonical_reason()
105 );
106 assert_eq!(response.status().as_u16(), 404);
107
108 println!("\nAll assertions passed!");
109}