Add centrage storage struct
This commit is contained in:
parent
4919a1ffe1
commit
e7601b7100
@ -24,6 +24,7 @@ use flake_tracker::{
|
||||
FlakeRevision,
|
||||
FlakeUri,
|
||||
InputModel,
|
||||
Storage,
|
||||
},
|
||||
templates::{
|
||||
FlakeInfoTemplate,
|
||||
@ -32,10 +33,6 @@ use flake_tracker::{
|
||||
RevisionTemplate,
|
||||
},
|
||||
};
|
||||
use sqlx::{
|
||||
SqlitePool,
|
||||
sqlite::SqlitePoolOptions,
|
||||
};
|
||||
|
||||
struct AppError(anyhow::Error);
|
||||
|
||||
@ -81,18 +78,17 @@ fn render_template<T: Template>(t: &T) -> anyhow::Result<Response> {
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
db: SqlitePool,
|
||||
storage: Storage,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let db = SqlitePoolOptions::new()
|
||||
.connect("sqlite://flake-tracker.db")
|
||||
let storage = Storage::connect("sqlite://flake-tracker.db")
|
||||
.await
|
||||
.context("Failed to connect to database")?;
|
||||
|
||||
let mut state = AppState {
|
||||
db,
|
||||
storage,
|
||||
};
|
||||
|
||||
let mut app = Router::new()
|
||||
@ -129,7 +125,7 @@ async fn route_flakes(
|
||||
GROUP BY uri
|
||||
ORDER BY uri
|
||||
")
|
||||
.fetch_all(&state.db)
|
||||
.fetch_all(&state.storage.db)
|
||||
.await
|
||||
.context("Failed to fetch data from database")?;
|
||||
|
||||
@ -152,7 +148,7 @@ async fn route_flake_info(
|
||||
ORDER BY last_modified
|
||||
")
|
||||
.bind(&uri)
|
||||
.fetch_all(&state.db)
|
||||
.fetch_all(&state.storage.db)
|
||||
.await
|
||||
.context("Failed to fetch data from database")?;
|
||||
|
||||
@ -179,7 +175,7 @@ async fn route_revision(
|
||||
ORDER BY input_name
|
||||
")
|
||||
.bind(&revision_uri)
|
||||
.fetch_all(&state.db)
|
||||
.fetch_all(&state.storage.db)
|
||||
.await
|
||||
.context("Failed to fetch data from database")?;
|
||||
|
||||
|
@ -1,10 +1,34 @@
|
||||
use anyhow::{
|
||||
Context,
|
||||
Result,
|
||||
};
|
||||
use crate::utils::{
|
||||
urlencode,
|
||||
};
|
||||
use sqlx::{
|
||||
FromRow,
|
||||
SqlitePool,
|
||||
sqlite::SqlitePoolOptions,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Storage {
|
||||
pub db: SqlitePool,
|
||||
}
|
||||
|
||||
impl Storage {
|
||||
pub async fn connect(db_connection_string: &str) -> Result<Self> {
|
||||
let db = SqlitePoolOptions::new()
|
||||
.connect(db_connection_string)
|
||||
.await
|
||||
.context("Failed to connect to database")?;
|
||||
|
||||
Ok(Self {
|
||||
db,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(FromRow)]
|
||||
pub struct FlakeRevisionRow {
|
||||
pub revision_uri: String,
|
||||
|
Loading…
x
Reference in New Issue
Block a user