Include repo in AppState

This commit is contained in:
Joscha 2023-08-04 16:33:46 +02:00
parent 4914b03fcd
commit 6f95d58e11
4 changed files with 1010 additions and 9 deletions

1000
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -9,6 +9,7 @@ askama = { version = "0.12.0", features = ["with-axum"] }
askama_axum = "0.3.0" askama_axum = "0.3.0"
axum = { version = "0.6.19", features = ["macros"] } axum = { version = "0.6.19", features = ["macros"] }
clap = { version = "4.3.19", features = ["derive", "deprecated"] } clap = { version = "4.3.19", features = ["derive", "deprecated"] }
gix = "0.51.0"
mime_guess = "2.0.4" mime_guess = "2.0.4"
rust-embed = "6.8.1" rust-embed = "6.8.1"
sqlx = { version = "0.7.1", features = ["runtime-tokio", "sqlite"] } sqlx = { version = "0.7.1", features = ["runtime-tokio", "sqlite"] }

View file

@ -79,7 +79,7 @@ async fn run() -> anyhow::Result<()> {
set_up_logging(args.verbose); set_up_logging(args.verbose);
info!("You are running {NAME} {VERSION}"); info!("You are running {NAME} {VERSION}");
let state = AppState::new(&args.db).await?; let state = AppState::new(&args.db, &args.repo).await?;
let app = Router::new() let app = Router::new()
.route("/", get(index)) .route("/", get(index))

View file

@ -1,8 +1,9 @@
//! Globally accessible application state. //! Globally accessible application state.
use std::path::Path; use std::{path::Path, sync::Arc};
use axum::extract::FromRef; use axum::extract::FromRef;
use gix::ThreadSafeRepository;
use sqlx::{ use sqlx::{
sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions, SqliteSynchronous}, sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions, SqliteSynchronous},
SqlitePool, SqlitePool,
@ -10,7 +11,7 @@ use sqlx::{
use tracing::{info, debug}; use tracing::{info, debug};
// TODO Occasionally run PRAGMA optimize // TODO Occasionally run PRAGMA optimize
async fn pool(db_path: &Path) -> sqlx::Result<SqlitePool> { async fn open_db(db_path: &Path) -> sqlx::Result<SqlitePool> {
let options = SqliteConnectOptions::new() let options = SqliteConnectOptions::new()
// https://www.sqlite.org/pragma.html#pragma_journal_mode // https://www.sqlite.org/pragma.html#pragma_journal_mode
.journal_mode(SqliteJournalMode::Wal) .journal_mode(SqliteJournalMode::Wal)
@ -35,15 +36,22 @@ async fn pool(db_path: &Path) -> sqlx::Result<SqlitePool> {
Ok(pool) Ok(pool)
} }
fn open_repo(repo_path: &Path) -> anyhow::Result<ThreadSafeRepository> {
info!("Opening repo at {}", repo_path.display());
Ok(ThreadSafeRepository::open(repo_path)?)
}
#[derive(Clone, FromRef)] #[derive(Clone, FromRef)]
pub struct AppState { pub struct AppState {
pub db: SqlitePool, pub db: SqlitePool,
pub repo: Arc<ThreadSafeRepository>,
} }
impl AppState { impl AppState {
pub async fn new(db_path: &Path) -> anyhow::Result<Self> { pub async fn new(db_path: &Path, repo_path: &Path) -> anyhow::Result<Self> {
Ok(Self { Ok(Self {
db: pool(db_path).await?, db: open_db(db_path).await?,
repo: Arc::new(open_repo(repo_path)?),
}) })
} }