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
6 changed files with 63 additions and 9 deletions
Showing only changes of commit 2ef527fa64 - Show all commits

View File

@ -16,6 +16,8 @@ tokio = { version = "1.39.1", features = ["macros", "rt-multi-thread"] }
tower-http = { version = "0.5.2", features = ["fs"] } tower-http = { version = "0.5.2", features = ["fs"] }
tower-livereload = "0.9.3" tower-livereload = "0.9.3"
utils = { path = "../utils" }
[dev-dependencies] [dev-dependencies]
cargo-watch = "8.5.1" cargo-watch = "8.5.1"
systemfd = "0.4.0" systemfd = "0.4.0"

View File

@ -2,8 +2,13 @@ use std::path::PathBuf;
use axum::http::{StatusCode, Uri}; use axum::http::{StatusCode, Uri};
use axum_htmx::AutoVaryLayer; use axum_htmx::AutoVaryLayer;
use thiserror::Error;
use tower_http::services::ServeDir; use tower_http::services::ServeDir;
use ::utils::config::{load_config, ConfigError};
pub mod db;
mod menu; mod menu;
mod pages; mod pages;
@ -11,6 +16,18 @@ async fn fallback(uri: Uri) -> (StatusCode, String) {
(StatusCode::NOT_FOUND, format!("No route for {uri}")) (StatusCode::NOT_FOUND, format!("No route for {uri}"))
} }
#[derive(Error, Debug)]
pub enum InitError {
#[error(transparent)]
ConfigError(#[from] ConfigError),
}
pub fn init() -> Result<(), InitError> {
load_config(None)?;
Ok(())
}
pub async fn get_router(assets_path: PathBuf) -> axum::Router<()> { pub async fn get_router(assets_path: PathBuf) -> axum::Router<()> {
axum::Router::new() axum::Router::new()
.nest_service("/assets", ServeDir::new(assets_path)) .nest_service("/assets", ServeDir::new(assets_path))

View File

@ -10,7 +10,7 @@ use tokio::net::TcpListener;
use tower_livereload::predicate::Predicate; use tower_livereload::predicate::Predicate;
use tower_livereload::LiveReloadLayer; use tower_livereload::LiveReloadLayer;
use ::app::get_router; use ::app::{get_router, init, InitError};
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum AppError { pub enum AppError {
@ -20,6 +20,8 @@ pub enum AppError {
NotifyWatcher(#[from] notify::Error), NotifyWatcher(#[from] notify::Error),
#[error("Missing environment variable {var}")] #[error("Missing environment variable {var}")]
MissingEnvVar { var: &'static str }, MissingEnvVar { var: &'static str },
#[error("Error while initialising the app")]
Initialisation(#[from] InitError),
} }
/// Nous filtrons les requêtes de `htmx` pour ne pas inclure le script _JS_ qui gère le rechargement /// Nous filtrons les requêtes de `htmx` pour ne pas inclure le script _JS_ qui gère le rechargement
@ -61,6 +63,8 @@ fn get_livereload_layer(
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), AppError> { async fn main() -> Result<(), AppError> {
init()?;
let manifest_dir = env::var("CARGO_MANIFEST_DIR").map_err(|_| AppError::MissingEnvVar { let manifest_dir = env::var("CARGO_MANIFEST_DIR").map_err(|_| AppError::MissingEnvVar {
var: "CARGO_MANIFEST_DIR", var: "CARGO_MANIFEST_DIR",
})?; })?;

View File

@ -7,8 +7,11 @@ 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"] }
utils = { path = "../utils" }
[dev-dependencies] [dev-dependencies]
cargo-watch = "8.5.2" cargo-watch = "8.5.2"
systemfd = "0.4.3" systemfd = "0.4.3"

View File

@ -2,6 +2,20 @@ 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 thiserror::Error;
use ::utils::config::{load_config, ConfigError};
#[derive(Error, Debug)]
pub enum InitError {
#[error(transparent)]
ConfigError(#[from] ConfigError),
}
pub fn init() -> Result<(), InitError> {
load_config(None)?;
Ok(())
}
pub fn get_router() -> Router { pub fn get_router() -> Router {
Router::new() Router::new()

View File

@ -1,24 +1,38 @@
use listenfd::ListenFd; use listenfd::ListenFd;
use thiserror::Error;
use tokio::net::TcpListener; use tokio::net::TcpListener;
use backend::get_router; use backend::{get_router, init, InitError};
#[derive(Error, Debug)]
pub enum BackendError {
#[error("Error while setting up or serving the TCP listener")]
ServeTCPListener(#[from] std::io::Error),
#[error("Error while initialising the backend")]
InitError(#[from] InitError),
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() -> Result<(), BackendError> {
init()?;
let app = get_router(); let app = get_router();
let mut listenfd = ListenFd::from_env(); let mut listenfd = ListenFd::from_env();
let listener = match listenfd.take_tcp_listener(0).unwrap() { 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) => {
listener.set_nonblocking(true).unwrap(); listener.set_nonblocking(true)?;
TcpListener::from_std(listener).unwrap() TcpListener::from_std(listener)?
} }
// otherwise fall back to local listening // otherwise fall back to local listening
None => TcpListener::bind("0.0.0.0:8080").await.unwrap(), None => TcpListener::bind("0.0.0.0:8080").await?,
}; };
println!("Listening on {}", listener.local_addr().unwrap()); let local_addr = listener.local_addr()?;
axum::serve(listener, app).await.unwrap(); println!("Listening on {}", local_addr);
axum::serve(listener, app).await?;
Ok(())
} }