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