summaryrefslogtreecommitdiff
path: root/rust/app/src/main.rs
diff options
context:
space:
mode:
authorJoe Carstairs <me@joeac.net>2024-11-17 09:23:28 +0000
committerJoe Carstairs <me@joeac.net>2024-11-17 09:23:28 +0000
commit1ae19eb3315c40e3f9062592c27fa76c11cf626c (patch)
tree09d9ce380795971f4b50e1d4b0ca3c57c9e1db1a /rust/app/src/main.rs
parent1a494937021561595d0fb29e26fb707aef9e55de (diff)
Renames workspace folder
Diffstat (limited to 'rust/app/src/main.rs')
-rw-r--r--rust/app/src/main.rs51
1 files changed, 51 insertions, 0 deletions
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<Build> {
+ 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,
+ ],
+ )
+}