tmtd/src/main.rs

53 lines
1.9 KiB
Rust

/*
* tmtd - Suckless To Do list
* Copyright (C) 2022 lemon.sh & famfo
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
use crate::{config::Config, database::Database};
use std::env;
use std::time::Duration;
use async_sqlx_session::PostgresSessionStore;
use tokio::task::JoinHandle;
use tokio::time::sleep;
use tracing::{error, info};
mod config;
mod database;
#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
let config_var = env::var("TMTD_CONFIG");
let config_path = config_var.as_deref().unwrap_or("tmtd.toml");
println!("Loading config from '{}'...", config_path);
let cfg = Config::load_from_file(config_path).await?;
info!(concat!("Initializing - tmtd ", env!("CARGO_PKG_VERSION")));
let database = Database::connect(&cfg.connection_string).await?;
let session_store = PostgresSessionStore::from_client(database.pool()).with_table_name("sessions");
session_store.migrate().await?;
Ok(())
}
fn spawn_session_cleanup_task(store: &PostgresSessionStore, period: Duration) -> JoinHandle<()> {
let store = store.clone();
tokio::spawn(async move {
loop {
sleep(period).await;
if let Err(error) = store.cleanup().await {
error!("cleanup error: {}", error);
}
}
})
}