diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs index 1850b01..8ad4bf4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,41 @@ +use rocket::fairing::{self, AdHoc}; use rocket::fs::{relative, FileServer}; use rocket::http::Status; -use rocket::Request; use rocket::State; +use rocket::{Build, Request, Rocket}; +use rocket_db_pools::sqlx::{self, database}; +use rocket_db_pools::{Connection, Database}; use rocket_dyn_templates::{context, Template}; use std::borrow::Cow; mod scrobbles; mod skweets; +#[derive(Database)] +#[database("blossom")] +struct Blossom(sqlx::SqlitePool); + +async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result { + match Blossom::fetch(&rocket) { + Some(db) => match sqlx::migrate!().run(&**db).await { + Ok(_) => Ok(rocket), + Err(e) => { + error!("failed to init blossom database: {}", e); + Err(rocket) + } + }, + None => Err(rocket), + } +} + +fn stage() -> AdHoc { + AdHoc::on_ignite("blossom database stage", |rocket| async { + rocket + .attach(Blossom::init()) + .attach(AdHoc::try_on_ignite("database migrations", run_migrations)) + }) +} + struct Clients { listenbrainz: listenbrainz::raw::Client, skinnyverse: mastodon_async::Mastodon, @@ -62,6 +90,7 @@ async fn main() -> Result<(), rocket::Error> { skinny_data.base = Cow::from("https://skinnyver.se"); let _rocket = rocket::build() + .attach(stage()) .manage(Clients { listenbrainz: listenbrainz::raw::Client::new(), skinnyverse: mastodon_async::Mastodon::from(skinny_data), |