added redirects

This commit is contained in:
Arlo Filley 2024-03-23 00:13:42 +00:00
parent caa177571b
commit 52624bd056
3 changed files with 24 additions and 12 deletions

Binary file not shown.

View File

@ -14,21 +14,25 @@ use crate::cors::CORS;
extern crate rocket; extern crate rocket;
use rocket::{ use rocket::{
fs::{relative, FileServer}, fs::{relative, FileServer},
response::Redirect, Build,
Build, Rocket Rocket
}; };
mod api; mod api;
mod catchers; mod catchers;
mod redirects;
use crate::api::leaderboard::leaderboard; use crate::api::leaderboard::leaderboard;
use crate::api::sql::Database; use crate::api::sql::Database;
use crate::api::test::{create_test, new_test}; use crate::api::test::{create_test, new_test};
use crate::api::user::{get_tests, login, sign_up}; use crate::api::user::{get_tests, login, sign_up};
use catchers::not_found::api_not_found; use crate::catchers::not_found::api_not_found;
use catchers::not_found::frontend_not_found; use crate::catchers::not_found::frontend_not_found;
use catchers::not_found::documentation_not_found; use crate::catchers::not_found::documentation_not_found;
use crate::redirects::leaderboard_redirect;
use crate::redirects::typing_redirect;
// Imports for sql, see sql.rs for more information // Imports for sql, see sql.rs for more information
@ -39,11 +43,6 @@ fn test() -> &'static str {
"Hello World! I'm A rocket Webserver" "Hello World! I'm A rocket Webserver"
} }
#[get("/")]
async fn typing_redirect() -> Redirect {
Redirect::to(uri!("/typing/index.html"))
}
/// The main function which builds and launches the /// The main function which builds and launches the
/// webserver with all appropriate routes and fileservers /// webserver with all appropriate routes and fileservers
#[launch] #[launch]
@ -69,12 +68,14 @@ async fn rocket() -> Rocket<Build> {
// hosts the fileserver // hosts the fileserver
.mount("/typing", routes![typing_redirect])
.mount("/typing", FileServer::from(relative!("public"))) .mount("/typing", FileServer::from(relative!("public")))
.register("/typing", catchers![frontend_not_found]) .register("/typing", catchers![frontend_not_found])
// The state which allows routes to access the database // The state which allows routes to access the database
.manage(Database::new().await.unwrap()) .manage(Database::new().await.unwrap())
// Redirects
.mount("/typing", routes![typing_redirect])
.mount("/leaderboard", routes![leaderboard_redirect])
// testing only, should return "Hello world" // testing only, should return "Hello world"
.mount("/test", routes![test]) .mount("/test", routes![test])

11
src/redirects.rs Normal file
View File

@ -0,0 +1,11 @@
use rocket::response::Redirect;
#[get("/")]
pub async fn typing_redirect() -> Redirect {
Redirect::to(uri!("/typing/index.html"))
}
#[get("/")]
pub async fn leaderboard_redirect() -> Redirect {
Redirect::to(uri!("/typing/leaderboard/index.html"))
}