From 1ae19eb3315c40e3f9062592c27fa76c11cf626c Mon Sep 17 00:00:00 2001 From: Joe Carstairs Date: Sun, 17 Nov 2024 09:23:28 +0000 Subject: Renames workspace folder --- rust/app/src/main.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 rust/app/src/main.rs (limited to 'rust/app/src/main.rs') diff --git a/rust/app/src/main.rs b/rust/app/src/main.rs new file mode 100644 index 0000000..dca6096 --- /dev/null +++ b/rust/app/src/main.rs @@ -0,0 +1,51 @@ +use budgeting_app_core::MIGRATIONS; +use diesel::SqliteConnection; +use diesel_migrations::MigrationHarness; +use rocket::fairing::AdHoc; +use rocket::http::ContentType; +use rocket::{launch, routes, Build, Rocket}; + +use crate::databases::UserDataDb; + +pub mod databases; +pub mod routes; + +fn run_database_migrations(connection: &mut SqliteConnection) { + connection + .run_pending_migrations(MIGRATIONS) + .expect("Error running database migrations"); +} + +#[launch] +async fn rocket() -> Rocket { + rocket::build() + .attach(UserDataDb::fairing()) + .attach(AdHoc::on_liftoff("Database Migration", |rocket| { + Box::pin(async move { + let user_data_db = UserDataDb::get_one(rocket).await.expect( + "Failed to get connection to user data database in order to run migrations", + ); + user_data_db.run(run_database_migrations).await; + }) + })) + .attach(AdHoc::on_response("Add CORS headers", |_req, res| { + Box::pin(async move { + res.set_header(ContentType::JSON); + res.set_header(rocket::http::Header::new( + "Access-Control-Allow-Origin", + "http://localhost:5173", + )); + }) + })) + .mount( + "/", + routes![ + routes::account::get_all_accounts, + routes::account::create_account, + routes::balance::get_all_category_balances, + routes::budget::get_current_category_budget_per_category, + routes::budget_update::get_all_budget_updates, + routes::category::get_all_categories, + ], + ) +} -- cgit v1.2.3