summaryrefslogtreecommitdiff
path: root/rust/schist_backend/src/main.rs
diff options
context:
space:
mode:
authorJoe Carstairs <me@joeac.net>2024-11-17 09:29:32 +0000
committerJoe Carstairs <me@joeac.net>2024-11-17 09:30:04 +0000
commit10f071447594f2bdcec84a461df9ad648f71f44b (patch)
tree645924a4948f39a4d9e2ec270d79fc6c2a3a937a /rust/schist_backend/src/main.rs
parent1ae19eb3315c40e3f9062592c27fa76c11cf626c (diff)
Renames app to 'schist'
Diffstat (limited to 'rust/schist_backend/src/main.rs')
-rw-r--r--rust/schist_backend/src/main.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/rust/schist_backend/src/main.rs b/rust/schist_backend/src/main.rs
new file mode 100644
index 0000000..8f5a40e
--- /dev/null
+++ b/rust/schist_backend/src/main.rs
@@ -0,0 +1,51 @@
+use schist_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,
+ ],
+ )
+}