diff --git a/Cargo.toml b/Cargo.toml index 37bd15c..e44ebaf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,9 +12,7 @@ members = [ [workspace.dependencies] anyhow = "1.0" -axum = "0.7.5" dotenv = "0.15" sea-orm-cli = "1.0.1" sea-orm = "1.0.1" thiserror = "1.0" -tokio = "1.39.1" diff --git a/README.md b/README.md index 05d7aec..c3f958d 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ cargo tauri build ### Création d'une migration ```bash -sea-orm-cli migrate generate +sea-orm-cli migrate generate ``` Cette commande génère un fichier de migration à adapter dans le dossier `migration/src`. diff --git a/crates/backend/Cargo.toml b/crates/backend/Cargo.toml index b2fffd0..8fe4ba0 100644 --- a/crates/backend/Cargo.toml +++ b/crates/backend/Cargo.toml @@ -7,11 +7,21 @@ edition = "2021" anyhow = "1.0.89" axum = "0.7.6" listenfd = "1.0.1" -thiserror.workspace = true tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] } +sea-orm = { workspace = true, features = [ + # Same `ASYNC_RUNTIME` and `DATABASE_DRIVER` as in the migration crate + "sqlx-sqlite", + "runtime-tokio-rustls", + "macros", +] } +thiserror.workspace = true + +entity = { path = "../../entity" } +migration = { path = "../../migration" } utils = { path = "../utils" } [dev-dependencies] cargo-watch = "8.5.2" +sea-orm-cli.workspace = true systemfd = "0.4.3" diff --git a/crates/backend/README.md b/crates/backend/README.md index f709a46..97281a4 100644 --- a/crates/backend/README.md +++ b/crates/backend/README.md @@ -10,6 +10,15 @@ En développement, le mécanisme de hot-reload nécessite de disposer de `cargo- cargo install cargo-watch systemfd ``` +## Configuration + +> Astuce : lorsqu'on exécute directement la crate `backend` à des fins de développement, le système de configuration n'utilisera pas l'éventuel fichier `.env` situé à la racine du workspace Rust. Pour éviter de dupliquer le fichier `.env`, il est possible de créer un lien symbolique vers le fichier `.env` de la crate `backend` : + +```bash +cd crates/backend +ln -s ../../.env .env +``` + ## Développement Pour lancer le serveur en mode développement, exécutez la commande suivante : diff --git a/crates/app/src/db.rs b/crates/backend/src/db.rs similarity index 100% rename from crates/app/src/db.rs rename to crates/backend/src/db.rs diff --git a/crates/backend/src/lib.rs b/crates/backend/src/lib.rs index 72ee327..150174a 100644 --- a/crates/backend/src/lib.rs +++ b/crates/backend/src/lib.rs @@ -2,10 +2,13 @@ use anyhow::Error as AnyError; use axum::http::{StatusCode, Uri}; use axum::response::{IntoResponse, Response}; use axum::{routing::get, Router}; +use sea_orm::{DatabaseConnection, DbErr}; use thiserror::Error; use ::utils::config::{load_config, ConfigError}; +mod db; + #[derive(Error, Debug)] pub enum InitError { #[error(transparent)] @@ -17,10 +20,17 @@ pub fn init() -> Result<(), InitError> { Ok(()) } -pub fn get_router() -> Router { - Router::new() +#[derive(Clone)] +pub struct AppState { + db_connection: DatabaseConnection, +} + +pub async fn get_router() -> Result { + let db_connection = db::get_connection().await?; + + Ok(Router::new() .route("/", get(|| async { "Hello, world!" })) - .fallback(fallback) + .fallback(fallback)) } async fn fallback(uri: Uri) -> (StatusCode, String) { diff --git a/crates/backend/src/main.rs b/crates/backend/src/main.rs index e9a489f..840e751 100644 --- a/crates/backend/src/main.rs +++ b/crates/backend/src/main.rs @@ -10,16 +10,17 @@ pub enum BackendError { ServeTCPListener(#[from] std::io::Error), #[error("Error while initialising the backend")] InitError(#[from] InitError), + #[error("Error with the database connection")] + DatabaseConnection(#[from] sea_orm::DbErr), } #[tokio::main] async fn main() -> Result<(), BackendError> { init()?; - let app = get_router(); + let app = get_router().await?; let mut listenfd = ListenFd::from_env(); - let listener = match listenfd.take_tcp_listener(0)? { // if we are given a tcp listener on listen fd 0, we use that one Some(listener) => { diff --git a/crates/desktop/Cargo.toml b/crates/desktop/Cargo.toml index da25acb..0e24fba 100644 --- a/crates/desktop/Cargo.toml +++ b/crates/desktop/Cargo.toml @@ -13,16 +13,12 @@ crate-type = ["lib", "cdylib", "staticlib"] tauri-build = { version = "2.0.0-rc", features = [] } [dependencies] -axum.workspace = true bytes = "1.6.1" http = "1.1.0" serde = { version = "1", features = ["derive"] } serde_json = "1" tauri = { version = "2.0.0-rc", features = [] } tauri-plugin-shell = "2.0.0-rc" -thiserror.workspace = true tower = "0.4.13" -tokio.workspace = true - -app = { path = "../app" } +thiserror.workspace = true