#![allow(unused)] pub use self::error::{Error, Result}; use std::net::SocketAddr; use axum::extract::{Path, Query}; use axum::http::{Method, Uri}; use axum::response::{Html, IntoResponse, Response}; use axum::routing::{get, get_service}; use axum::{middleware, Json, Router}; use axum::Server; use serde::Deserialize; use tower_http::services::ServeDir; mod error; mod web; #[derive(Debug, Deserialize)] struct HelloParams { name: Option, } #[tokio::main] async fn main() { let routes_all = Router::new() .merge(routes_hello()) .merge(web::routes_login::routes()) .layer(middleware::map_response(main_response_mapper)) .fallback_service(routes_static()); let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); println!("->> Listening on {addr}\n"); Server::bind(&addr) .serve(routes_all.into_make_service()) .await .unwrap(); } async fn main_response_mapper(res: Response) -> Response { println!("->> {:<12} - main_response_mapper", "HANDLER"); println!(); res } fn routes_static() -> Router { Router::new().nest_service("/", get_service(ServeDir::new("./"))) } fn routes_hello() -> Router { Router::new().route("/hello", get(handler_hello)) .route("/hello2/:name", get(handler_hello2)) } // e.g. `hello?name=jen` async fn handler_hello(Query(params): Query) -> impl IntoResponse { println!("->> {:<12} - handler_hello - {params:?}", "HANDLER"); let name = params.name.as_deref().unwrap_or("World!"); Html(format!("Hello {name}!")) } // e.g. `hello/jen` async fn handler_hello2(Path(name): Path) -> impl IntoResponse { println!("->> {:<12} - handler_hello_path - {name:?}", "HANDLER"); Html(format!("Hello {name}!")) }