Add AppState, DB and Snafu
This commit is contained in:
parent
e8529bdbee
commit
a3c0b194e1
11 changed files with 2512 additions and 143 deletions
27
src/state/mod.rs
Normal file
27
src/state/mod.rs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
use sea_orm::{Database, DatabaseConnection};
|
||||
use snafu::prelude::*;
|
||||
|
||||
use crate::state::config::AppConfig;
|
||||
use error::*;
|
||||
|
||||
pub mod config;
|
||||
pub mod error;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AppState {
|
||||
pub config: AppConfig,
|
||||
pub db: DatabaseConnection,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub async fn new() -> Result<Self, AppStateError> {
|
||||
let config: AppConfig = AppConfig::new().await.context(ConfigSnafu)?;
|
||||
|
||||
let db: DatabaseConnection =
|
||||
Database::connect(format!("sqlite:{}?mode=rwc", &config.database_path))
|
||||
.await
|
||||
.context(SqliteSnafu)?;
|
||||
|
||||
Ok(Self { config, db })
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue