diff options
| author | Joe Carstairs <me@joeac.net> | 2024-12-12 08:28:04 +0000 |
|---|---|---|
| committer | Joe Carstairs <me@joeac.net> | 2024-12-12 08:28:04 +0000 |
| commit | 54894f9de9bad4fecb2a116d59a03cdd003f84c0 (patch) | |
| tree | 6ce5517cecf23d4e93d7a381a374e1fa6c810cee /schist_backend/src/main.rs | |
| parent | 8a30bcbdf9264d235bf93da3df8e170cfde53d02 (diff) | |
Moves rust workspace to root
Diffstat (limited to 'schist_backend/src/main.rs')
| -rw-r--r-- | schist_backend/src/main.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/schist_backend/src/main.rs b/schist_backend/src/main.rs new file mode 100644 index 0000000..8395681 --- /dev/null +++ b/schist_backend/src/main.rs @@ -0,0 +1,48 @@ +use schist_schema::migrations::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::category::get_all_categories, + ], + ) +} |
