Open db from path specified as cl arg
This commit is contained in:
parent
9b55cd1ce2
commit
ed3600a79a
2 changed files with 18 additions and 6 deletions
12
src/main.rs
12
src/main.rs
|
|
@ -1,6 +1,8 @@
|
||||||
mod state;
|
mod state;
|
||||||
mod r#static;
|
mod r#static;
|
||||||
|
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
use askama_axum::{IntoResponse, Response};
|
use askama_axum::{IntoResponse, Response};
|
||||||
use axum::{extract::State, http::StatusCode, routing::get, Router};
|
use axum::{extract::State, http::StatusCode, routing::get, Router};
|
||||||
|
|
@ -20,7 +22,12 @@ const VERSION: &str = concat!(
|
||||||
|
|
||||||
#[derive(Debug, clap::Parser)]
|
#[derive(Debug, clap::Parser)]
|
||||||
#[command(name = NAME, version = VERSION)]
|
#[command(name = NAME, version = VERSION)]
|
||||||
struct Args {}
|
struct Args {
|
||||||
|
/// Path to the repo's tablejohn database.
|
||||||
|
db: PathBuf,
|
||||||
|
/// Path to the git repo.
|
||||||
|
repo: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
fn set_up_logging() {
|
fn set_up_logging() {
|
||||||
let filter = tracing_subscriber::filter::Builder::default()
|
let filter = tracing_subscriber::filter::Builder::default()
|
||||||
|
|
@ -47,12 +54,13 @@ async fn index(State(db): State<SqlitePool>) -> Result<Response, Response> {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run() -> anyhow::Result<()> {
|
async fn run() -> anyhow::Result<()> {
|
||||||
|
// Parse args before any logging starts
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
|
||||||
set_up_logging();
|
set_up_logging();
|
||||||
info!("You are running {NAME} {VERSION}");
|
info!("You are running {NAME} {VERSION}");
|
||||||
|
|
||||||
let state = AppState::new().await?;
|
let state = AppState::new(&args.db).await?;
|
||||||
|
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.route("/", get(index))
|
.route("/", get(index))
|
||||||
|
|
|
||||||
12
src/state.rs
12
src/state.rs
|
|
@ -1,5 +1,7 @@
|
||||||
//! Globally accessible application state.
|
//! Globally accessible application state.
|
||||||
|
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
use axum::extract::FromRef;
|
use axum::extract::FromRef;
|
||||||
use sqlx::{
|
use sqlx::{
|
||||||
sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions, SqliteSynchronous},
|
sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions, SqliteSynchronous},
|
||||||
|
|
@ -7,8 +9,7 @@ use sqlx::{
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO Occasionally run PRAGMA optimize
|
// TODO Occasionally run PRAGMA optimize
|
||||||
// TODO Open db from path
|
async fn pool(db_path: &Path) -> sqlx::Result<SqlitePool> {
|
||||||
async fn pool() -> 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)
|
||||||
|
|
@ -20,6 +21,7 @@ async fn pool() -> sqlx::Result<SqlitePool> {
|
||||||
// https://www.sqlite.org/pragma.html#pragma_trusted_schema
|
// https://www.sqlite.org/pragma.html#pragma_trusted_schema
|
||||||
// The docs recommend always turning this off
|
// The docs recommend always turning this off
|
||||||
.pragma("trusted_schema", "false")
|
.pragma("trusted_schema", "false")
|
||||||
|
.filename(db_path)
|
||||||
.create_if_missing(true)
|
.create_if_missing(true)
|
||||||
.optimize_on_close(true, None);
|
.optimize_on_close(true, None);
|
||||||
|
|
||||||
|
|
@ -36,7 +38,9 @@ pub struct AppState {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
pub async fn new() -> anyhow::Result<Self> {
|
pub async fn new(db_path: &Path) -> anyhow::Result<Self> {
|
||||||
Ok(Self { db: pool().await? })
|
Ok(Self {
|
||||||
|
db: pool(db_path).await?,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue