feat: Move index route to it's own module

This commit is contained in:
Nathan McCarty 2023-07-26 02:34:13 -04:00
parent 801aa7144f
commit a689a064f4
Signed by: thatonelutenist
SSH Key Fingerprint: SHA256:3elIBybO7zXuCg+/os7OlO2fwfRPXmObQjcHXBf7Hfg
4 changed files with 15 additions and 20 deletions

View File

@ -1,10 +1,15 @@
use axum::{response::Html, routing::get, Router}; use axum::{routing::get, Router};
use std::net::SocketAddr; use std::net::SocketAddr;
use stranger_site::routes::index::index;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
// build our application with a route // 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 // run it
let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
@ -14,7 +19,3 @@ async fn main() {
.await .await
.unwrap(); .unwrap();
} }
async fn handler() -> Html<&'static str> {
Html("<h1>Hello, World!</h1>")
}

View File

@ -1,14 +1 @@
pub fn add(left: usize, right: usize) -> usize { pub mod routes;
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}

1
src/routes.rs Normal file
View File

@ -0,0 +1 @@
pub mod index;

6
src/routes/index.rs Normal file
View File

@ -0,0 +1,6 @@
use axum::response::Html;
/// Render the front page
pub async fn index() -> Html<String> {
Html("<h1>Hello, World!</h1>".to_string())
}