Rocket: Use fairings for routes of sub-files

Instead of adding all routes manually in the Launch function for Rocket,
implement fairings for all the different files that adds the routing to
the rocket in a self-contained manner.
This commit is contained in:
trotFunky 2024-07-27 21:22:34 +01:00
parent 120472354c
commit 222acbb4f8
6 changed files with 28 additions and 10 deletions

View file

@ -16,8 +16,9 @@ A list of things that could be implemented/added to the application, some of the
- [ ] Move the database queries to their own functions - [ ] Move the database queries to their own functions
- [ ] Cache those results - [ ] Cache those results
- [ ] Centralize Markdown parsing ? - [ ] Centralize Markdown parsing ?
- [ ] Use fairings for the different elements ? - [x] Use fairings for the different elements
- [ ] Use guards for User calls ? - [ ] Use guards for User calls ?
- [ ] Use SQLite Row ID for User IDs rather than regular IDs, for randomness ?
# Dependencies # Dependencies

View file

@ -8,6 +8,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
use argon2::{Argon2, PasswordHash, PasswordVerifier}; use argon2::{Argon2, PasswordHash, PasswordVerifier};
use blake2::{Blake2b512, Digest}; use blake2::{Blake2b512, Digest};
use blake2::digest::FixedOutput; use blake2::digest::FixedOutput;
use rocket::fairing::AdHoc;
use crate::database_records::{AuthTokens, PlayerLoginInfo, Vote}; use crate::database_records::{AuthTokens, PlayerLoginInfo, Vote};
use crate::{database, week}; use crate::{database, week};
use database::Db; use database::Db;
@ -210,3 +211,9 @@ pub async fn logout(week: u8, mut db: Connection<Db>, cookies: &CookieJar<'_>) -
Redirect::to(uri!(week::week(week))) Redirect::to(uri!(week::week(week)))
} }
pub fn stage() -> AdHoc {
AdHoc::on_ignite("Auth stage", |rocket| async {
rocket.mount("/", routes![login, logout])
})
}

View file

@ -36,13 +36,11 @@ async fn index(mut db: Connection<Db>) -> Redirect {
fn rocket() -> _ { fn rocket() -> _ {
rocket::build() rocket::build()
.mount("/", FileServer::from(relative!("static_files"))) .mount("/", FileServer::from(relative!("static_files")))
.mount("/", routes![index, .attach(auth::stage())
vote::fetch_vote_data, vote::vote, .attach(week::stage())
truth::create_truth, truth::edit_truth, .attach(truth::stage())
week::week, week::update_week, week::set_last_week, week::create_week, .attach(vote::stage())
auth::login, auth::logout]) .mount("/", routes![index])
.attach(database::stage()) .attach(database::stage())
.attach(Template::fairing()) .attach(Template::fairing())
} }
// TODO: Random Row ID

View file

@ -5,6 +5,7 @@ use rocket::response::Redirect;
use rocket_db_pools::{sqlx, Connection}; use rocket_db_pools::{sqlx, Connection};
use pulldown_cmark::{Parser, Options}; use pulldown_cmark::{Parser, Options};
use rocket::fairing::AdHoc;
use sqlx::Row; use sqlx::Row;
use crate::{auth, database, week}; use crate::{auth, database, week};
@ -110,3 +111,9 @@ pub async fn create_truth(week: u8, form: Form<TruthUpdateForm>,
Redirect::to(uri!(week::week(week))) Redirect::to(uri!(week::week(week)))
} }
pub fn stage() -> AdHoc {
AdHoc::on_ignite("Truth stage", |rocket| async {
rocket.mount("/", routes![create_truth, edit_truth])
})
}

View file

@ -164,9 +164,8 @@ pub async fn fetch_vote_data(week: u8, mut db: Connection<database::Db>, cookies
Some(Json(VoteData{truth_count: truth_count, votes: vote_data})) Some(Json(VoteData{truth_count: truth_count, votes: vote_data}))
} }
// FIXME:
pub fn stage() -> AdHoc { pub fn stage() -> AdHoc {
AdHoc::on_ignite("SQLx Stage", |rocket| async { AdHoc::on_ignite("Vote stage", |rocket| async {
rocket.mount("/", routes![vote, fetch_vote_data]) rocket.mount("/", routes![vote, fetch_vote_data])
}) })
} }

View file

@ -210,3 +210,9 @@ pub async fn create_week(week: u8, mut db: Connection<Db>, cookies: &CookieJar<'
} }
} }
} }
pub fn stage() -> AdHoc {
AdHoc::on_ignite("Week stage", |rocket| async {
rocket.mount("/", routes![week, create_week, update_week, set_last_week])
})
}