Setup de SeaORM + SQLite comme base de données #64

Merged
florian_briand merged 12 commits from feat/9_setup_db into main 2024-10-02 12:04:04 +02:00
8 changed files with 38 additions and 14 deletions
Showing only changes of commit 777b7f2425 - Show all commits

View File

@ -12,9 +12,7 @@ members = [
[workspace.dependencies] [workspace.dependencies]
anyhow = "1.0" anyhow = "1.0"
axum = "0.7.5"
dotenv = "0.15" dotenv = "0.15"
sea-orm-cli = "1.0.1" sea-orm-cli = "1.0.1"
sea-orm = "1.0.1" sea-orm = "1.0.1"
thiserror = "1.0" thiserror = "1.0"
tokio = "1.39.1"

View File

@ -7,11 +7,21 @@ edition = "2021"
anyhow = "1.0.89" anyhow = "1.0.89"
axum = "0.7.6" axum = "0.7.6"
listenfd = "1.0.1" listenfd = "1.0.1"
thiserror.workspace = true
tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] } 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" } utils = { path = "../utils" }
[dev-dependencies] [dev-dependencies]
cargo-watch = "8.5.2" cargo-watch = "8.5.2"
sea-orm-cli.workspace = true
systemfd = "0.4.3" systemfd = "0.4.3"

View File

@ -10,6 +10,15 @@ En développement, le mécanisme de hot-reload nécessite de disposer de `cargo-
cargo install cargo-watch systemfd 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 ## Développement
Pour lancer le serveur en mode développement, exécutez la commande suivante : Pour lancer le serveur en mode développement, exécutez la commande suivante :

View File

@ -2,10 +2,13 @@ use anyhow::Error as AnyError;
use axum::http::{StatusCode, Uri}; use axum::http::{StatusCode, Uri};
use axum::response::{IntoResponse, Response}; use axum::response::{IntoResponse, Response};
use axum::{routing::get, Router}; use axum::{routing::get, Router};
use sea_orm::{DatabaseConnection, DbErr};
use thiserror::Error; use thiserror::Error;
use ::utils::config::{load_config, ConfigError}; use ::utils::config::{load_config, ConfigError};
mod db;
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum InitError { pub enum InitError {
#[error(transparent)] #[error(transparent)]
@ -17,10 +20,17 @@ pub fn init() -> Result<(), InitError> {
Ok(()) Ok(())
} }
pub fn get_router() -> Router { #[derive(Clone)]
Router::new() pub struct AppState {
db_connection: DatabaseConnection,
}
pub async fn get_router() -> Result<Router, DbErr> {
let db_connection = db::get_connection().await?;
Ok(Router::new()
.route("/", get(|| async { "Hello, world!" })) .route("/", get(|| async { "Hello, world!" }))
.fallback(fallback) .fallback(fallback))
} }
async fn fallback(uri: Uri) -> (StatusCode, String) { async fn fallback(uri: Uri) -> (StatusCode, String) {

View File

@ -10,16 +10,17 @@ pub enum BackendError {
ServeTCPListener(#[from] std::io::Error), ServeTCPListener(#[from] std::io::Error),
#[error("Error while initialising the backend")] #[error("Error while initialising the backend")]
InitError(#[from] InitError), InitError(#[from] InitError),
#[error("Error with the database connection")]
DatabaseConnection(#[from] sea_orm::DbErr),
} }
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), BackendError> { async fn main() -> Result<(), BackendError> {
init()?; init()?;
let app = get_router(); let app = get_router().await?;
let mut listenfd = ListenFd::from_env(); let mut listenfd = ListenFd::from_env();
let listener = match listenfd.take_tcp_listener(0)? { let listener = match listenfd.take_tcp_listener(0)? {
// if we are given a tcp listener on listen fd 0, we use that one // if we are given a tcp listener on listen fd 0, we use that one
Some(listener) => { Some(listener) => {

View File

@ -13,16 +13,12 @@ crate-type = ["lib", "cdylib", "staticlib"]
tauri-build = { version = "2.0.0-rc", features = [] } tauri-build = { version = "2.0.0-rc", features = [] }
[dependencies] [dependencies]
axum.workspace = true
bytes = "1.6.1" bytes = "1.6.1"
http = "1.1.0" http = "1.1.0"
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
tauri = { version = "2.0.0-rc", features = [] } tauri = { version = "2.0.0-rc", features = [] }
tauri-plugin-shell = "2.0.0-rc" tauri-plugin-shell = "2.0.0-rc"
thiserror.workspace = true
tower = "0.4.13" tower = "0.4.13"
tokio.workspace = true
app = { path = "../app" }
thiserror.workspace = true