From a689a064f4021a9c4a7a7a5065f758f4b0d4f829 Mon Sep 17 00:00:00 2001 From: Nathan McCarty Date: Wed, 26 Jul 2023 02:34:13 -0400 Subject: [PATCH] feat: Move index route to it's own module --- src/bin/stranger-site-server.rs | 13 +++++++------ src/lib.rs | 15 +-------------- src/routes.rs | 1 + src/routes/index.rs | 6 ++++++ 4 files changed, 15 insertions(+), 20 deletions(-) create mode 100644 src/routes.rs create mode 100644 src/routes/index.rs diff --git a/src/bin/stranger-site-server.rs b/src/bin/stranger-site-server.rs index 2b6f918..19ef87e 100644 --- a/src/bin/stranger-site-server.rs +++ b/src/bin/stranger-site-server.rs @@ -1,10 +1,15 @@ -use axum::{response::Html, routing::get, Router}; +use axum::{routing::get, Router}; use std::net::SocketAddr; +use stranger_site::routes::index::index; + #[tokio::main] async fn main() { // build our application with a route - let app = Router::new().route("/", get(handler)); + let app = Router::new() + .route("/", get(index)) + .route("/index", get(index)) + .route("/index.html", get(index)); // run it let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); @@ -14,7 +19,3 @@ async fn main() { .await .unwrap(); } - -async fn handler() -> Html<&'static str> { - Html("

Hello, World!

") -} diff --git a/src/lib.rs b/src/lib.rs index 7d12d9a..6a664ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1 @@ -pub fn add(left: usize, right: usize) -> usize { - left + right -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } -} +pub mod routes; diff --git a/src/routes.rs b/src/routes.rs new file mode 100644 index 0000000..33edc95 --- /dev/null +++ b/src/routes.rs @@ -0,0 +1 @@ +pub mod index; diff --git a/src/routes/index.rs b/src/routes/index.rs new file mode 100644 index 0000000..367689d --- /dev/null +++ b/src/routes/index.rs @@ -0,0 +1,6 @@ +use axum::response::Html; + +/// Render the front page +pub async fn index() -> Html { + Html("

Hello, World!

".to_string()) +}