| 1 | mod app; |
| 2 | mod components; |
| 3 | mod export; |
| 4 | mod format; |
| 5 | mod hub; |
| 6 | mod i18n; |
| 7 | mod matcher; |
| 8 | mod import; |
| 9 | mod media; |
| 10 | mod scheme; |
| 11 | mod store; |
| 12 | mod zipout; |
| 13 | mod editor_assets; |
| 14 | |
| 15 | use std::sync::Arc; |
| 16 | use std::time::Duration; |
| 17 | |
| 18 | |
| 19 | const TRASH_RETENTION_MS: i64 = 30 * 24 * 60 * 60 * 1000; |
| 20 | |
| 21 | const PURGE_EVERY: Duration = Duration::from_secs(60 * 60); |
| 22 | |
| 23 | |
| 24 | |
| 25 | fn spawn_trash_purge(store: Arc<store::Store>, hub: Arc<hub::Hub>) { |
| 26 | tokio::spawn(async move { |
| 27 | let mut tick = tokio::time::interval(PURGE_EVERY); |
| 28 | loop { |
| 29 | tick.tick().await; |
| 30 | match store.purge_trash(TRASH_RETENTION_MS).await { |
| 31 | Ok(ids) => { |
| 32 | for id in ids { |
| 33 | hub.forget(&id); |
| 34 | } |
| 35 | } |
| 36 | Err(err) => eprintln!("trash purge failed: {err:#}"), |
| 37 | } |
| 38 | } |
| 39 | }); |
| 40 | } |
| 41 | |
| 42 | #[tokio::main] |
| 43 | async fn main() -> anyhow::Result<()> { |
| 44 | let db_path = std::env::var("NOTE_DB").unwrap_or_else(|_| "notes.db".to_owned()); |
| 45 | let store = Arc::new(store::Store::open(&db_path).await?); |
| 46 | let hub = Arc::new(hub::Hub::new()); |
| 47 | let media = Arc::new(media::Media::open()?); |
| 48 | spawn_trash_purge(store.clone(), hub.clone()); |
| 49 | let fresh = store.clone(); |
| 50 | tokio::spawn(async move { fresh.keep_directory_fresh().await }); |
| 51 | let (builder, builder_store) = (hub.clone(), store.clone()); |
| 52 | tokio::spawn(async move { builder.build_pending(&builder_store).await }); |
| 53 | let (compacter, compacter_store) = (hub.clone(), store.clone()); |
| 54 | tokio::spawn(async move { compacter.compact_stale(&compacter_store).await }); |
| 55 | let evicter = hub.clone(); |
| 56 | tokio::spawn(async move { evicter.evict_idle().await }); |
| 57 | topcoat::start(app::router(store, hub, media)).await?; |
| 58 | Ok(()) |
| 59 | } |