GWibisono

main.rs part 2

Feb 15th, 2026
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.85 KB | Source Code | 0 0
  1. // Import the core types we need from axum
  2. // use axum::{routing::get, Router};
  3. use axum::{
  4.     routing::{get, post, put, delete},
  5.     Router
  6. };
  7. mod webs;
  8.  
  9. #[tokio::main]
  10. async fn main() {
  11.     // Initialize tracing for request logging
  12.     tracing_subscriber::fmt::init();
  13.     // Build our router with a single route
  14. /*
  15.     let app = Router::new()
  16.         .route("/", get(webs::root_handler));
  17. */
  18.     let app = build_router();
  19.     // Create a TCP listener on port 3000
  20.     let listener = tokio::net::TcpListener::bind("0.0.0.0:3000")
  21.         .await
  22.         .unwrap();
  23.  
  24.     println!("Server running on http://localhost:3000");
  25.  
  26.     // Start serving requests
  27.     axum::serve(listener, app).await.unwrap();
  28. }
  29.  
  30. // Build a router with RESTful routes for a users resource
  31. fn build_router() -> Router {
  32.     Router::new()
  33.         // Basic routes
  34.         .route("/", get(index))
  35.         // Nested path with multiple methods
  36.         //.route("/users", get(list_users).post(create_user))
  37.         // Path parameters use :param_name syntax
  38.         //.route("/users/:id", get(get_user).put(update_user).delete(delete_user))
  39.         // You can nest routers for better organization
  40.         .nest("/api/v1", api_routes())
  41. }
  42.  
  43. // Separate router for API routes - keeps main router clean
  44. fn api_routes() -> Router {
  45.     Router::new()
  46.         .routes("/", get(webs::api_result).post(webs::api_result) )
  47.         .route("/health", get(health_check))
  48.         .route("/status", get(status))
  49. }
  50.  
  51. // Handler functions are just async functions that return something
  52. // implementing IntoResponse
  53. // async fn root_handler_before() -> &'static str {
  54. //     "Hello from Axum!"
  55.  
  56. // }
  57.  
  58. async fn index() -> &'static str {
  59.    "API Root"
  60. }
  61.  
  62. async fn health_check() -> &'static str {
  63.     "Health OK"
  64. }
  65.  
  66. async fn status() -> &'static str {
  67.    "status: Running"
  68. }
Tags: rust
Advertisement
Add Comment
Please, Sign In to add comment