aboutsummaryrefslogtreecommitdiffstats
path: root/crates/rocie-server/src/app.rs
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-09-06 10:31:40 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-09-06 10:31:40 +0200
commit9a9d5c5880095adeb43a045dca638243c8f946e4 (patch)
tree86e0d23af339b3139efab15749aaf5b59aa0965b /crates/rocie-server/src/app.rs
parentchore: Initial commit (diff)
downloadserver-9a9d5c5880095adeb43a045dca638243c8f946e4.zip
feat: Provide basic API frame
Diffstat (limited to '')
-rw-r--r--crates/rocie-server/src/app.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/crates/rocie-server/src/app.rs b/crates/rocie-server/src/app.rs
new file mode 100644
index 0000000..ab8f764
--- /dev/null
+++ b/crates/rocie-server/src/app.rs
@@ -0,0 +1,57 @@
+use std::{env, path::PathBuf};
+
+use sqlx::{SqlitePool, sqlite::SqliteConnectOptions};
+
+use crate::storage::migrate::migrate_db;
+
+#[derive(Clone)]
+pub(crate) struct App {
+ pub(crate) db: SqlitePool,
+}
+
+impl App {
+ pub(crate) async fn new() -> Result<Self, app_create::Error> {
+ let db_path: PathBuf = PathBuf::from(env::var("ROCIE_DB_PATH")?);
+
+ let db = {
+ let options = SqliteConnectOptions::new()
+ .filename(&db_path)
+ .optimize_on_close(true, None)
+ .create_if_missing(true);
+
+ SqlitePool::connect_with(options).await.map_err(|err| {
+ app_create::Error::DbConnectionFailed {
+ inner: err,
+ db_path,
+ }
+ })?
+ };
+
+ let me = Self { db };
+
+ migrate_db(&me).await?;
+
+ Ok(me)
+ }
+}
+
+pub(crate) mod app_create {
+ use std::{env, path::PathBuf};
+
+ use crate::storage::migrate::migrate_db;
+
+ #[derive(thiserror::Error, Debug)]
+ pub(crate) enum Error {
+ #[error("The `ROCIE_DB_PATH` variable is not accessible: {0}")]
+ MissingDbVariable(#[from] env::VarError),
+
+ #[error("Failed to connect to the sqlite database at `{db_path}`, because: {inner}")]
+ DbConnectionFailed {
+ inner: sqlx::Error,
+ db_path: PathBuf,
+ },
+
+ #[error("Failed to migrate db to the current version")]
+ MigrateDb(#[from] migrate_db::Error),
+ }
+}